Skip to content
advent of code 2015

day 05

doesn't he have intern-elves for this?

approach

a small window(s, k) generator yields every substring of length k, and both rule sets are written on top of it.

part 1 slides a window of two: it counts vowels, looks for a doubled letter and rejects the forbidden pairs. part 2 first needs a window of three whose ends match, then stores the first index of every pair in a dict and accepts as soon as the same pair shows up again at least two positions later, so the two occurrences don't overlap.

the trick

remember where each pair was first seen; a repeat two or more positions later can't overlap.

python
def is_nice_p2(s):
    prev = None
    rep = set()
    if not any(w[0] == w[-1] for w in window(s, 3)):
        return False
    d = {}
    for i, w in enumerate(window(s, 2)):
        if w in d and i - d[w] >= 2:
            return True
        elif w not in d:
            d[w] = i
    return False