-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvent_of_code_25.py
More file actions
92 lines (79 loc) · 1.78 KB
/
advent_of_code_25.py
File metadata and controls
92 lines (79 loc) · 1.78 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import re
class AssemBunnyInterpreter:
def __init__(self, a=0, b=0, c=0, d=0):
self.a = a
self.b = b
self.c = c
self.d = d
self.pointer = 0
def get_value(self, val):
if val in 'abcd':
return getattr(self, val)
return int(val)
def cpy(self, src, dst):
setattr(self, dst, self.get_value(src))
def inc(self, reg):
current = getattr(self, reg)
setattr(self, reg, current + 1)
def dec(self, reg):
current = getattr(self, reg)
setattr(self, reg, current - 1)
def jnz(self, t, steps):
if self.get_value(t) != 0:
self.pointer += int(steps)
self.pointer -= 1
def out(self, t):
# print("out ", self.get_value(t))
return self.get_value(t)
def parse(self, line):
params = line.split()
ret = getattr(self, params[0])(*params[1:])
self.pointer += 1
return ret
def run(self, instruction_string, max_output_length):
instructions = instruction_string.split('\n')
current_step = 0
output_list = []
while self.pointer < len(instructions):
output = self.parse(instructions[self.pointer])
if output is not None:
output_list.append(output)
if len(output_list) >= max_output_length:
break
return output_list
my_input = """cpy a d
cpy 7 c
cpy 365 b
inc d
dec b
jnz b -2
dec c
jnz c -5
cpy d a
jnz 0 0
cpy a b
cpy 0 a
cpy 2 c
jnz b 2
jnz 1 6
dec b
dec c
jnz c -4
inc a
jnz 1 -7
cpy 2 b
jnz c 2
jnz 1 4
dec b
dec c
jnz 1 -4
jnz 0 0
out b
jnz a -19
jnz 1 -21"""
res = []
a = 0
while not res == list(10 * [0, 1]):
res = AssemBunnyInterpreter(a=a).run(my_input, 20)
a += 1
print a-1, res