Skip to content
advent of code 2017

day 05

a maze of twisty trampolines, all alike

approach

the input is a list of jump offsets and a pointer i. each step reads the offset at i, updates that offset in place, jumps, and counts the step, until the pointer leaves the list.

part 1 always increments the offset it just used. part 2 decrements it instead when it was 3 or more. each part gets its own copy of the list, since the simulation mutates it.

the trick

read the jump before modifying the cell, then move; a plain loop is fast enough.

python
def part2(instructions):
    steps = i = 0
    while 0 <= i < len(instructions):
        jump = instructions[i]
        inc = -1 if jump >= 3 else 1
        instructions[i] += inc
        i += jump
        steps += 1
    return steps