Skip to content
advent of code 2016

day 04

security through obscurity

approach

a regex splits each line into the dashed name, the sector id and the checksum. for the checksum, the letters of the name are counted and sorted by (-frequency, letter), which puts the most common first and breaks ties alphabetically; the first five are compared with the checksum.

part 1 sums the sector ids of the rooms whose checksum matches. part 2 shifts every letter of the name forward by the sector id, modulo 26, and returns the id of the room whose decrypted name starts with northpole.

the trick

sorting on (-count, letter) gives the frequency order and the alphabetical tie-break in a single sort.

python
def sort_by_frequency(name):
    count = defaultdict(int)
    for w in name.split("-"):
        for c in w:
            count[c] += 1

    return [c for _, c in sorted((-f, c) for c, f in count.items())[:5]]