-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08.py
More file actions
59 lines (46 loc) · 1.24 KB
/
08.py
File metadata and controls
59 lines (46 loc) · 1.24 KB
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
with open("inputs/08.txt","r") as f:
data = f.read().splitlines()
class Computer(object):
def __init__(self,program):
self.mem = list(program)
self.memsize = len(self.mem)
self.watchdog = [False]*len(self.mem)
self.accumulator = 0
self.pointer = 0
def run(self):
while True:
# Exit successfully if eof
if self.pointer >= self.memsize:
return 0, self.accumulator
# Watchdog checking if instruction has already excecuted
if self.watchdog[self.pointer]:
return 9, self.accumulator
self.watchdog[self.pointer] = True
op, value = self.mem[self.pointer].split()
self.watchdog[self.pointer] = True
if op == "acc":
self.accumulator += int(value)
self.pointer += 1
continue
if op == "nop":
self.pointer += 1
continue
if op == "jmp":
self.pointer += int(value)
continue
computer = Computer(data)
for i in range(len(data)):
tmpdata = list(data)
if tmpdata[i][:3] == "jmp":
tmpdata[i] = "nop" + tmpdata[i][3:]
elif tmpdata[i][:3] == "nop":
tmpdata[i] = "jmp" + tmpdata[i][3:]
else:
continue
tmpcomputer = Computer(tmpdata)
term, output = tmpcomputer.run()
if term == 0:
break
print("Day 8")
print(" Part 1:", computer.run()[1]) #1939
print(" Part 2:", output) #2212