January 18, 2024

Solution for Advent of Code 2016 - Day 4: Security Through Obscurity

Link to the puzzle text

Part 1

In this puzzle we are given a list of rooms containing an encrypted room name, a number and a checksum each. We are asked to validate the encrypted room name with the checksum and sum all valid room numbers. Checksums are valid if they are the 5 most common letters  in the encrypted name.

We check this by first counting all letters in the encrypted room name with Pythons Counter class. The letters and their number of occurences are then sorted with the alphabetical sort the tie breaker. The 5 most common letters are then concatinated and compared to the checksum:

c = Counter(name)
most_common_letters = sorted(c.most_common(), key=lambda x: (-x[1], x[0]))
most_common_letters = "".join([char[0] for char in most_common_letters[:5]])

Part 2

In part 2, we are asked to decrypt the valid rooms and search for the room number of the north pole object storage. The decryption is done via a rotational cipher and the room number as the key. In the rotation function, we convert letters into their numerical position in the alphabet (so 'A' becomes 0, 'B' becomes 1, ..., 'Z' becomes 25). Afterwards the number of rations is added, modulus 26 is applied to get a result between 0 and 25. This position is then converted back to a real character.

def rotate(letter, rot):
shift = ord('a') if letter.islower() else ord('A')
return chr((ord(letter) - shift + rot) % 26 + shift)

After decrypting all room names, we search for the room with the correct name and return its room number.

Bonus

The room names itself are suspicious: why would the Easter Bunny need "biohazardous candy design"  and "top secret basket operations" rooms?

Link to my solutions

No comments:

Post a Comment