-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvent_of_code_23.py
More file actions
97 lines (83 loc) · 2.09 KB
/
advent_of_code_23.py
File metadata and controls
97 lines (83 loc) · 2.09 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
93
94
95
96
97
import re
class AssemBunnyInterpreter:
def __init__(self, instruction_string, a=0, b=0, c=0, d=0):
self.a = a
self.b = b
self.c = c
self.d = d
self.pointer = 0
ins = instruction_string.split('\n')
self.instructions = [line.split() for line in ins]
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 += self.get_value(steps)
self.pointer -= 1
def tgl(self, reg):
x = getattr(self, reg)
toggle_map = {
"inc": "dec",
"dec": "inc",
"tgl": "inc",
"jnz": "cpy",
"cpy": "jnz"
}
if self.pointer + x < len(self.instructions):
self.instructions[self.pointer + x][0] = toggle_map[self.instructions[self.pointer + x][0]]
def parse(self, params):
getattr(self, params[0])(*params[1:])
self.pointer += 1
def run(self):
while self.pointer < len(self.instructions):
# print(self.instructions[self.pointer])
self.parse(self.instructions[self.pointer])
# print({'a': self.a, 'b': self.b, 'c': self.c, 'd': self.d})
# print(self.instructions)
return {'a': self.a, 'b': self.b, 'c': self.c, 'd': self.d}
test_input = """cpy 2 a
tgl a
tgl a
tgl a
cpy 1 a
dec a
dec a"""
my_input = """cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5
dec b
cpy b c
cpy c d
dec d
inc c
jnz d -2
tgl c
cpy -16 c
jnz 1 c
cpy 83 c
jnz 78 d
inc a
inc d
jnz d -2
inc c
jnz c -5"""
assert AssemBunnyInterpreter(test_input).run()['a'] == 3
# print AssemBunnyInterpreter(my_input, a=7).run()
print AssemBunnyInterpreter(my_input, a=12).run()