AoC 2017 Day 5: 21 Jump Street

Source: A Maze of Twisty Trampolines, All Alike1

Part 1: Interpret a program made entirely of jump instructions: each instruction is how many steps to jump. Any time you use an instruction to jump, increase the value of that jump by 1 for next time. How many total steps does it take to escape (jump out of bounds)?

offsets = list(map(int, lib.input()))
pc = 0

for step in itertools.count():
    if not 0 <= pc < len(offsets):
        break

    offset = offsets[pc]
    offsets[pc] += 1

    pc += offset

print(step)

We use itertools.count to count how many steps we take (without knowing ahead of time when we’ll stop)2. That’s about it.

Part 2: Rather than always increasing by 1, if the offset was previously 3 or more, instead decrease it by 1. How many steps does it take now?

The part function from my Advent of Code standard lib comes in handy here:

offsets = list(map(int, lib.input()))
pc = 0

for step in itertools.count():
    if not 0 <= pc < len(offsets):
        break

    offset = offsets[pc]

    if lib.part(2) and offset >= 3:
        offsets[pc] -= 1
    else:
        offsets[pc] += 1

    pc += offset

print(step)

Takes quite a bit longer though:

$ python3 run-all.py day-05

day-05  python3 21-jump-street.py input.txt --part 1    0.9574990272521973      394829
day-05  python3 21-jump-street.py input.txt --part 2    76.25577807426453       31150702

That’s technically outside of the 1 minute limit, but I’m not entirely sure how to fix that without special casing the code to my input. If I were to do that, I would probably print out what the current jumps look like and look for loops. I’m sure there is one.


  1. Colossal Cave Adventure3 ↩︎

  2. This would be equivalent:

    step = 1
    while True:
        step += 1
        ...
    
     ↩︎
  3. Again ↩︎