Advent of Code: Day 5

For Day 5, the challenge is to calculate the number of steps needed to bounce around an array following one of two rulesets. My Python 2 solution is below:

def simple_increment(i):
    return i + 1

def part_2_increment(i):
    return i - 1 if i >= 3 else i + 1

def play_maze(maze, increment=simple_increment):
    maze = maze[:]
    i = 0
    steps = 0
    while i >= 0 and i < len(maze):
        new_i = i + maze[i]
        maze[i] = increment(maze[i])
        i = new_i
        steps += 1
    return steps

with open("input.txt", "r") as f:
    maze = [int(x.strip("\n")) for x in f.readlines()]

print "Part 1: ", play_maze(maze)
print "Part 2: ", play_maze(maze, part_2_increment)

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.