Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2017-12-05 - A Maze of Twisty Trampolines, All Alike

(original .ipynb)
puzzle_input_str = open("puzzle_input/day5.txt").read()

def step(mem, pc):
    ret_val = mem[pc]
    mem[pc] += 1
    return ret_val

def part_one(input_str):
    cycles = 0
    pc = 0
    memory = [ int(line.strip()) for line in input_str.split("\n") ]

    while pc >= 0 and pc < len(memory):
        pc += step(memory, pc)
        cycles += 1
    
    return cycles

print("part one:", part_one(puzzle_input_str))
part one: 359348
def step2(mem, pc):
    ret_val = mem[pc]
    if ret_val >= 3:
        mem[pc] -= 1
    else:
        mem[pc] += 1
    return ret_val

def part_two(input_str):
    cycles = 0
    pc = 0
    memory = [ int(line.strip()) for line in input_str.split("\n") ]

    while pc >= 0 and pc < len(memory):
        pc += step2(memory, pc)
        cycles += 1
        
    return cycles


print("part two:", part_two(puzzle_input_str))
part two: 27688760