-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_23.py
114 lines (89 loc) · 2.34 KB
/
day_23.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# https://adventofcode.com/2016/day/23
from helper import get_input, print_result
from day_12 import Ins, InsInc, InsDec, InsCpy, InsJnz
DAY = 23
inp = get_input(DAY)
N = len(inp)
data = []
class InsTgl(Ins):
def __init__(self, x):
super().__init__("tgl", x)
def execute(self, regs, iptr):
global data
i = iptr + self.get_x_val(regs)
if i < 0 or i >= len(data):
return iptr + 1
ins = data[i]
match ins.op:
case "inc":
data[i] = InsDec(ins.x)
case "dec" | "tgl":
data[i] = InsInc(ins.x)
case "jnz":
data[i] = InsCpy(ins.x, ins.y)
case "cpy":
data[i] = InsJnz(ins.x, ins.y)
return iptr + 1
def parse_input(data):
res = []
for ins in data:
ins = ins.split()
match ins[0]:
case "cpy":
res.append(InsCpy(ins[1], ins[2]))
case "inc":
res.append(InsInc(ins[1]))
case "dec":
res.append(InsDec(ins[1]))
case "jnz":
res.append(InsJnz(ins[1], ins[2]))
case "tgl":
res.append(InsTgl(ins[1]))
return res
def get_val(regs, r):
return int(r) if regs.get(r) is None else regs.get(r)
def execute_multi(regs, iptr):
if [x.op for x in data[iptr : iptr + 6]] == [
"cpy",
"inc",
"dec",
"jnz",
"dec",
"jnz",
]:
cpy_x, cpy_y = data[iptr].x, data[iptr].y
inc_x = data[iptr + 1].x
dec2_x = data[iptr + 4].x
regs[inc_x] += get_val(regs, cpy_x) * get_val(regs, dec2_x)
regs[cpy_y] = 0
regs[dec2_x] = 0
return iptr + 6
return 0
def help(regs):
iptr = 0
total_instructions = len(data)
while iptr >= 0 and iptr < total_instructions:
iptr = execute_multi(regs, iptr) or data[iptr].execute(regs, iptr)
return regs["a"]
def p1():
global data
data = parse_input(inp)
regs = {
"a": 7,
"b": 0,
"c": 0,
"d": 0,
}
return help(regs)
def p2():
global data
data = parse_input(inp)
regs = {
"a": 12,
"b": 0,
"c": 0,
"d": 0,
}
return help(regs)
if __name__ == "__main__":
print_result(DAY, p1(), p2())