Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.08 KB | None | 0 0
  1. import scala.io.Source
  2.  
  3. val input = Source.fromFile("C:/work/advent06/input4").getLines.toList
  4.  
  5. case class Room(name: String, id: Int, checksum: String)
  6.  
  7. val roomRegex = """((?:[a-z]+-)+)(\d+)\[([a-z]{5})\]""".r
  8.  
  9. val rooms = input.map {
  10.   case roomRegex(name, id, checksum) => Room(name, id.toInt, checksum)
  11. }
  12.  
  13. def isReal(room: Room) = {
  14.   val nameChars = room.name.toList.filter(_.isLower)
  15.   val charsByFrequency = nameChars
  16.     .groupBy(identity)
  17.     .mapValues(_.size)
  18.     .toList
  19.     .sortBy(_._1).sortBy(-_._2)
  20.   val checksum = charsByFrequency.take(5).map(_._1).mkString
  21.   checksum == room.checksum
  22. }
  23.  
  24. val realRooms = rooms.filter(isReal)
  25.  
  26. val sumOfRealRoomIds = realRooms.map(_.id).sum
  27.  
  28. def decrypt(room: Room) = {
  29.   val shift = room.id % 26
  30.  
  31.   def decryptChar(c: Char) = {
  32.     val afterShift = (c + shift).toChar
  33.     if (afterShift > 'z') (afterShift - 26).toChar else afterShift
  34.   }
  35.  
  36.   room.copy(name = room.name.toList.map(decryptChar).mkString)
  37. }
  38.  
  39. val decryptedRooms = realRooms.map(decrypt)
  40.  
  41. val targetRoom = decryptedRooms.find(_.name.startsWith("north"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement