Advent of Code: Day 4 Solution

This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.

And now, on with the solution:

Day 4, part 1

Simplified problem: You find an Information Kiosk with a list of rooms. The list is encrypted and also includes decoy data. Each room consists of an encrypted name (lowercase letters separated by dashes) followed by a dash, a sector ID, and a checksum in square brackets. A room is real (not a decoy) if the checksum is the five most common letters in the encrypted name, in order, with ties broken by alphabetization.

For example:

Of the real rooms from the list above, the sum of their sector IDs is 1514. What is the sum of the sector IDs of the real rooms?

Solution

Create a Room object:

  • Checksum
  • MostCommonLetters=Calculate frequency of each distinct letter in RoomName, sort by most->least frequent, then sort ties alphabetically
  • IsReal=(Checksum==MostCommonLetters)
  • RoomName
  • SectorId
  1. Set Rooms=new List of Room
  2. Set input=(input)
  3. For each line in input
    1. Use regular expressions to separate the room data:
    2. Set room=new Room(checksum,sectorId,roomName)
    3. Add room to Rooms
  4. End For
  5. Return number of rooms in Rooms where IsReal=true

Day 4, part 2

Using the same input, decrypt the room names by shifting each character one character further in the alphabet checksum number of times. Z shifts back to A and – becomes ” “.

Solution

  1. Add property DecryptedRoomName to Room
  2. Follow solution for Part 1
  3. For each room in Rooms
    1. Set decryptedRoomName=nothing
    2. For each character c in RoomName
      1. For i=1 to checksum
        1. Shift c along the alphabet (a=>b,d=>c,z=>a,etc)
        2. Add c to decryptedRoomName
      2. End For
    3. End For
    4. Set Room.DecryptedRoomName=decryptedRoomName
  4. End For
  5. Return SectorId of Rooms where room.DecryptedRoomName contains “northpole”.

You can find the full solution on Github

Leave a Reply

Your email address will not be published. Required fields are marked *