Skip to content
advent of code 2016

day 05

how about a nice game of chess?

approach

next_character hashes the door id followed by an increasing index with md5 until the hex digest starts with five zeros, then returns the digest and the index to resume from.

part 1 calls it eight times and takes the sixth hex character of each hit. part 2 treats the sixth character as a position and the seventh as the value: positions outside 0–7 are skipped, a position that is already filled is left alone, and the search goes on until all eight slots are set.

the trick

the search returns where it stopped, so both parts can keep asking for "the next interesting hash" without starting over.

python
def next_character(k, door_id, start):
    i = start
    while True:
        res = md5(bytes(f"{door_id}{i}", encoding = "ascii"))
        dig = res.digest().hex()
        i += 1
        if all(d == '0' for d in dig[:k]):
            return i, dig