Skip to content
advent of code 2016

day 06

signals and noise

approach

the messages are kept as a list of equal-length strings. for each column, a Counter tallies the characters and they are sorted by how often they appear.

part 1 takes the last character of that ordering, the most common one. part 2 takes the first, the least common. the two parts are the same function called with a different index.

the trick

sort each column's characters by frequency once; the two answers are the two ends of the list.

python
def solve(messages, k):
    n = len(messages[0])
    res = []
    for i in range(n):
        c = Counter(m[i] for m in messages)
        s = sorted((ch for ch in c), key = lambda x: c[x])
        res.append(s[k])
    return "".join(res)