-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass-day-8.py
45 lines (34 loc) · 914 Bytes
/
ass-day-8.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
import string
# Read field
f = open("ass-day-8-input.txt", "r")
lines = f.read().split("\n")
program = []
for idx, line in enumerate(lines):
x,y = line.split(" ")
program.append((idx, x, int(y)))
def execute_program(program):
executed = []
accumulator = 0
index = 0
end_reached = False
try:
while index not in executed:
executed.append(index)
linenr, oper, val = program[index]
if oper=='acc':
accumulator += val
index += 1
elif oper=='nop':
index += 1
elif oper=='jmp':
index += val
except IndexError as e:
end_reached = True
finally:
return end_reached, accumulator
end_reached, accumulator = execute_program(program)
if end_reached:
print("REACHED THE END!")
else:
print("DUPLICATE OPER")
print(accumulator)