Skip to content
advent of code 2017

day 04

high-entropy passphrases

approach

each line is split into words. part 1 counts the lines where turning the words into a set doesn't lose anything, i.e. no word repeats.

part 2 does the same, but first sorts the letters of every word, so two words that are anagrams of each other collapse to the same key. a Counter over those keys having as many entries as there are words means the line is valid.

the trick

sorting a word's letters gives a canonical form that is equal for all of its anagrams.

python
def part2(lines):
    s = 0
    for line in lines:
        split = line.split(" ")
        c = Counter("".join(sorted(w)) for w in split)
        if len(c) == len(split):
            s += 1
    return s