Skip to content
advent of code 2015

day 11

corporate policy

approach

the password becomes a list of numbers from 0 to 25, treated as a base-26 counter. increment adds one with carry from the right, and next_valid_word keeps incrementing until the three rules hold: a run of three increasing values, two separate pairs, and none of the banned letters (checked as the indices 8, 11 and 14).

part 1 is the next valid password. part 2 calls the same function again on the result. a comment notes that jumping straight past a banned letter would be an easy optimization, left out because the plain loop was fast enough.

the trick

treat the password as a base-26 number and count up.

python
def increment(arr, c = 1, start_with = None):
    i = len(arr) - 1 if start_with is None else start_with
    while c > 0 and i >= 0:
        c, d = divmod(arr[i] + c, 26)
        arr[i] = d
        i -= 1
    if c > 0 and i < 0:
        arr.insert(0, c - 1)