You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.2 KiB
Python
67 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from math import floor
|
|
import sys
|
|
|
|
L = []
|
|
with open(sys.argv[1], 'r') as f:
|
|
L = f.readlines()
|
|
L = [x.strip().split() for x in L]
|
|
L = [[x[0], int(x[1])] for x in L]
|
|
|
|
# part 1
|
|
log = []
|
|
accum = 0
|
|
i = 0
|
|
while True:
|
|
if i in log:
|
|
break
|
|
else:
|
|
log.append(i)
|
|
if L[i][0] == 'nop':
|
|
i += 1
|
|
continue
|
|
if L[i][0] == 'acc':
|
|
accum += L[i][1]
|
|
i += 1
|
|
continue
|
|
if L[i][0] == 'jmp':
|
|
i += L[i][1]
|
|
print(accum)
|
|
|
|
# part 2
|
|
accum = 0
|
|
|
|
def infiniteloop(L):
|
|
log = []
|
|
accum = 0
|
|
i = 0
|
|
while True:
|
|
if i in log:
|
|
return
|
|
elif i == len(L):
|
|
print(accum)
|
|
sys.exit(0)
|
|
else:
|
|
log.append(i)
|
|
if L[i][0] == 'nop':
|
|
i += 1
|
|
continue
|
|
if L[i][0] == 'acc':
|
|
accum += L[i][1]
|
|
i += 1
|
|
continue
|
|
if L[i][0] == 'jmp':
|
|
i += L[i][1]
|
|
|
|
for i in range(len(L)):
|
|
if L[i][0] == 'nop':
|
|
L[i][0] = 'jmp'
|
|
infiniteloop(L)
|
|
L[i][0] = 'nop'
|
|
elif L[i][0] == 'jmp':
|
|
L[i][0] = 'nop'
|
|
infiniteloop(L)
|
|
L[i][0] = 'jmp'
|
|
print(accum)
|