/
day16.py
155 lines (99 loc) · 2.96 KB
/
day16.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import re
file = open("day16.txt")
tests = []
program = []
digit = re.compile(r"\d+")
while True:
line = file.readline()
if not line:
break
line = line.strip('\n')
if line.startswith("Before"):
test = (
list(map(int, digit.findall(line))),
tuple(map(int, digit.findall(file.readline()))),
list(map(int, digit.findall(file.readline()))))
tests.append(test)
elif line:
program.append(map(int, digit.findall(line)))
def addr(state, a, b, c):
state[c] = state[a]+state[b]
def addi(state, a, b, c):
state[c] = state[a]+b
def mulr(state, a, b, c):
state[c] = state[a]*state[b]
def muli(state, a, b, c):
state[c] = state[a]*b
def banr(state, a, b, c):
state[c] = state[a] & state[b]
def bani(state, a, b, c):
state[c] = state[a] & b
def borr(state, a, b, c):
state[c] = state[a] | state[b]
def bori(state, a, b, c):
state[c] = state[a] | b
def setr(state, a, b, c):
state[c] = state[a]
def seti(state, a, b, c):
state[c] = a
def gtir(state, a, b, c):
state[c] = 1 if a > state[b] else 0
def gtri(state, a, b, c):
state[c] = 1 if state[a] > b else 0
def gtrr(state, a, b, c):
state[c] = 1 if state[a] > state[b] else 0
def eqir(state, a, b, c):
state[c] = 1 if a == state[b] else 0
def eqri(state, a, b, c):
state[c] = 1 if state[a] == b else 0
def eqrr(state, a, b, c):
state[c] = 1 if state[a] == state[b] else 0
opcodes = [addr, addi, mulr, muli, banr, bani,
borr, bori, setr, seti, gtri, gtir, gtrr, eqir, eqri, eqrr]
print("opcodes:", len(opcodes))
# exit()
# run tests
res = []
for state, command, result in tests:
matching = []
# print(state, command, result)
for opcode in opcodes:
teststate = state.copy()
opcode(teststate, *command[1:])
if teststate == result:
matching.append(opcode)
res.append((command[0], matching))
m = list(filter(lambda r: len(r[1]) >= 3, res))
print("solution 1:", len(m))
commands = {k: v[0] for (k, v) in filter(lambda r: len(r[1]) == 1, res)}
nonunique = list(filter(lambda r: len(r[1]) > 1, res))
nonunique.sort(key=lambda p: len(p[1]), reverse=True)
reversemap = dict()
for instr, cmds in nonunique:
for c in cmds:
if c in commands.values():
continue
if c in reversemap:
reversemap[c].add(instr)
else:
reversemap[c] = set()
reversemap[c].add(instr)
while reversemap:
match = None
for cmd in reversemap.keys():
known_ids = commands.keys()
ids = reversemap[cmd]
ids = ids.difference(known_ids)
reversemap[cmd] = ids
if len(ids) == 1:
match = (ids.pop(), cmd)
break
if match:
id, cmd = match
commands[id] = cmd
del reversemap[cmd]
print("instruction set:", commands)
state = [0]*4
for instr, a, b, c in program:
commands[instr](state, a, b, c)
print(state)