-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_08b.py
66 lines (51 loc) · 1.54 KB
/
aoc_08b.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
fin = open("input_08.txt")
program = []
for line in fin:
opcode, arg = line.strip().split(" ")
program.append((opcode, arg))
fin.close()
class Comp():
def __init__(self,prog):
self.prog = prog
self.pc = 0
self.acc = 0
self.accesscount = [0]*len(prog)
self.state = "init"
def run(self):
self.state = "run"
while True:
if self.pc == len(self.prog):
print("Normal termination.")
self.state = "stop"
return
opcode = self.prog[self.pc][0]
arg = int(self.prog[self.pc][1])
self.accesscount[self.pc] += 1
if self.accesscount[self.pc] == 2:
print("Loop detected. Accumulator:",self.acc)
self.state = "loop"
return
if opcode == "nop":
self.pc += 1
elif opcode == "acc":
self.acc += arg
self.pc += 1
elif opcode == "jmp":
self.pc += arg
else:
print("Undefined opcode", opcode, "at", self.pc)
for pos,element in enumerate(program):
mutant = program.copy()
if element[0] == "nop":
newelement = ("jmp",element[1])
elif element[0] == "jmp":
newelement = ("nop",element[1])
else:
newelement = element
mutant[pos] = newelement
mycomp = Comp(mutant)
mycomp.run()
print(pos,mycomp.state)
if mycomp.state == "stop":
print(pos, element)
print(mycomp.acc)