August 24, 2024

Solution for Advent of Code 2017 - Day 5: A Maze of Twisty Trampolines, All Alike

Link to the puzzle text

Part 1

 In this puzzle we have a list of jump offsets and should follow these jumps until we reach an index out of bounds. After each jump, we should increase the offset by 1. The answer is the number of jumps we need to take until we jump outside.

We first convert the list into a series of integers and init the current position. In a loop, we follow the offsets to the next position and update the current jump offset. Then we check for out of bounds.

while True:
offset = jump_offsets[index]
next_index = index + offset
num_steps += 1
jump_offsets[index] += 1
index = next_index
if index < 0 or index >= len(jump_offsets):
return num_steps

Part 2

In part 2, the updating is changed. Instead of always increasing, it can decrease if the offset was 3 or more.

For solving it, we can copy the code from part 1 and insert the new update rule. The rest of the code was unchanged.

if offset >= 3:
jump_offsets[index] -= 1
else:
jump_offsets[index] += 1 

Link to my solutions

No comments:

Post a Comment