-
Notifications
You must be signed in to change notification settings - Fork 0
/
201821.py
128 lines (117 loc) · 3.38 KB
/
201821.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import re
prog = []
ipr = 0
class OpCode:
ADDR = 1
ADDI = 2
MULR = 3
MULI = 4
BANR = 5
BANI = 6
BORR = 7
BORI = 8
SETR = 9
SETI = 10
GTIR = 11
GTRI = 12
GTRR = 13
EQIR = 14
EQRI = 15
EQRR = 16
with open("201821.txt", "r") as f:
for line in f.readlines():
if line[0] == "#":
ipr = int(line[4])
else:
m = re.match(r"([a-z]+) (\d+) (\d+) (\d+)", line.strip())
assert m is not None
i, a, b, c = m.groups()
if i == "addr":
i = OpCode.ADDR
elif i == "addi":
i = OpCode.ADDI
elif i == "mulr":
i = OpCode.MULR
elif i == "muli":
i = OpCode.MULI
elif i == "banr":
i = OpCode.BANR
elif i == "bani":
i = OpCode.BANI
elif i == "borr":
i = OpCode.BORR
elif i == "bori":
i = OpCode.BORI
elif i == "setr":
i = OpCode.SETR
elif i == "seti":
i = OpCode.SETI
elif i == "gtir":
i = OpCode.GTIR
elif i == "gtri":
i = OpCode.GTRI
elif i == "gtrr":
i = OpCode.GTRR
elif i == "eqir":
i = OpCode.EQIR
elif i == "eqri":
i = OpCode.EQRI
elif i == "eqrr":
i = OpCode.EQRR
else:
raise Exception(f"Unknown opcode: {i}")
prog.append((i, int(a), int(b), int(c)))
def execute(p, ipr, reg=0, part=0):
r = [reg, 0, 0, 0, 0, 0]
seen = set()
while 0 <= r[ipr] < len(p):
if r[ipr] == 28:
assert p[r[ipr]][0] == OpCode.EQRR
if part == 0:
return r[4]
else:
if r[4] not in seen:
print(f"Candidate: {r[4]}")
if len(seen) % 1000 == 0:
print(f"Total Candidates: {len(seen)}")
seen.add(r[4])
else:
return r[4]
i, a, b, c = p[r[ipr]]
if i == OpCode.ADDR:
r[c] = r[a] + r[b]
elif i == OpCode.ADDI:
r[c] = r[a] + b
elif i == OpCode.MULR:
r[c] = r[a] * r[b]
elif i == OpCode.MULI:
r[c] = r[a] * b
elif i == OpCode.BANR:
r[c] = r[a] & r[b]
elif i == OpCode.BANI:
r[c] = r[a] & b
elif i == OpCode.BORR:
r[c] = r[a] | r[b]
elif i == OpCode.BORI:
r[c] = r[a] | b
elif i == OpCode.SETR:
r[c] = r[a]
elif i == OpCode.SETI:
r[c] = a
elif i == OpCode.GTIR:
r[c] = 1 if a > r[b] else 0
elif i == OpCode.GTRI:
r[c] = 1 if r[a] > b else 0
elif i == OpCode.GTRR:
r[c] = 1 if r[a] > r[b] else 0
elif i == OpCode.EQIR:
r[c] = 1 if a == r[b] else 0
elif i == OpCode.EQRI:
r[c] = 1 if r[a] == b else 0
elif i == OpCode.EQRR:
r[c] = 1 if r[a] == r[b] else 0
else:
raise Exception(f"Unknown opcode {i}")
r[ipr] += 1
print(f"Part One: {execute(prog, ipr, reg=0, part=0)}")
print(f"Part Two: {execute(prog, ipr, reg=0, part=1)}")