Skip to content

Instantly share code, notes, and snippets.

@joshbduncan
Last active December 9, 2020 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbduncan/3bfded8a5a519bc5e428471570d94941 to your computer and use it in GitHub Desktop.
Save joshbduncan/3bfded8a5a519bc5e428471570d94941 to your computer and use it in GitHub Desktop.
from copy import deepcopy
def solve(instructions):
# setup vars to track
acc = 0
pos = 0
visited = set()
while True:
if pos == len(instructions) or pos in visited:
break
else:
visited.add(pos)
# parse out operation and argument
item = instructions[pos]
op = item.split(' ')[0]
arg = int(item.split(' ')[1])
if op == 'acc':
acc += arg
pos += 1
if op == 'jmp':
pos += arg
if op == 'nop':
pos += 1
# return final position and accumulator
return pos, acc
# read in aoc input
instructions = open("day8.in").read().strip().split('\n')
# solve for part 1
p1 = solve(instructions)
print(f'Part 1: {p1[1]}')
# solve for part 2
for n, item in enumerate(instructions):
op = item.split(' ')[0]
# check to see if change needs to be made
if op == 'nop' or op == 'jmp':
new_instructions = deepcopy(instructions)
# make change to new instruction set
if op == 'nop':
new_instructions[n] = 'jmp' + new_instructions[n][3:]
if op == 'jmp':
new_instructions[n] = 'nop' + new_instructions[n][3:]
# submit new instruction set to solve function
p2 = solve(new_instructions)
# if final position is vlaid end loop
if p2[0] == len(instructions):
break
else:
continue
print(f'Part 2: {p2[1]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment