-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
78 lines (62 loc) · 1.56 KB
/
day8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 8 08:55:48 2020
@author: rundx
"""
import sys
f = open('day8.txt','r')
instructions = []
for line in f:
tempLine = line.rstrip().split()
instructions.append((tempLine[0],int(tempLine[1])))
stepTrace = []
def nop(step):
return step + 1
def acc(step):
return instructions[step][1]
def jmp(step):
return instructions[step][1]
changes = []
accum = 0
step = 0
while step< len(instructions):
#print('step',step)
stepTrace.append(step)
whatDo = instructions[step][0]
if whatDo == 'nop':
lastChange = step
step = nop(step)
elif whatDo == 'acc':
accum += acc(step)
step += 1
else:
lastChange = step
step += jmp(step)
if step in stepTrace:
break
print(accum)
for i in range(len(instructions)):
newInstructions = instructions.copy()
if instructions[i][0] == 'jmp':
newInstructions[i] = ('nop', instructions[i][1])
elif instructions[i][0] == 'nop':
newInstructions[i] = ('jmp', instructions[i][1])
accum = 0
step = 0
stepTrace = []
while step< len(instructions):
#print('step',step)
stepTrace.append(step)
whatDo = newInstructions[step][0]
if whatDo == 'nop':
step = nop(step)
elif whatDo == 'acc':
accum += acc(step)
step += 1
else:
step += jmp(step)
if step in stepTrace:
break
if step == len(instructions):
print(accum)
sys.exit()