-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.py
228 lines (175 loc) · 5.44 KB
/
day17.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
from enum import Enum
from typing import NamedTuple
def read_input():
with open(sys.argv[1]) as f:
return f.read().strip()
class Registers(NamedTuple):
a: int
b: int
c: int
def write(self, register, value):
match register:
case "a":
return Registers(value, self.b, self.c)
case "b":
return Registers(self.a, value, self.c)
case "c":
return Registers(self.a, self.b, value)
case _:
raise ValueError(f"Invalid register {register}")
class Opcode(Enum):
ADV = 0
BXL = 1
BST = 2
JNZ = 3
BXC = 4
OUT = 5
BDV = 6
CDV = 7
def combo(registers, operand):
match operand:
case 0 | 1 | 2 | 3:
return operand, str(operand)
case 4:
return registers.a, "a"
case 5:
return registers.b, "b"
case 6:
return registers.c, "c"
case _:
raise ValueError(f"Invalid combo operand {operand}")
def _dv(registers, operand):
co, ser = combo(registers, operand)
return registers.a // (2 ** co), ser
def adv(registers, operand):
res, ser = _dv(registers, operand)
return Registers(res, registers.b, registers.c), f"a = a // 2 ** {ser}"
def bxl(registers, operand):
return Registers(registers.a, registers.b ^ operand, registers.c), f"b = b ^ {operand}"
def bst(registers, operand):
co, ser = combo(registers, operand)
return Registers(registers.a, co % 8, registers.c), f"b = {ser} % 8"
def jnz(registers, operand, ip):
return ip + 2 if registers.a == 0 else operand, "JNZ"
def bxc(registers, operand):
return Registers(registers.a, registers.b ^ registers.c, registers.c), "b = b ^ c"
def out(registers, operand):
co, ser = combo(registers, operand)
return co % 8, f"stdout.append({ser} % 8)"
def bdv(registers, operand):
res, ser = _dv(registers, operand)
return Registers(registers.a, res, registers.c), f"b = a // 2 ** {ser}"
def cdv(registers, operand):
res, ser = _dv(registers, operand)
return Registers(registers.a, registers.b, res), f"c = a // 2 ** {ser}"
operations = {
Opcode.ADV: adv,
Opcode.BXL: bxl,
Opcode.BST: bst,
Opcode.BXC: bxc,
Opcode.BDV: bdv,
Opcode.CDV: cdv,
}
def interpret(r, program):
registers = r
stdout = []
ip = 0
while ip < len(program):
opcode = Opcode(program[ip])
operand = program[ip + 1]
# print(ip, opcode, operand, registers, stdout)
if op := operations.get(opcode):
registers, _ = op(registers, operand)
ip += 2
elif opcode == Opcode.JNZ:
ip, _ = jnz(registers, operand, ip)
elif opcode == Opcode.OUT:
o, _ = out(registers, operand)
stdout.append(o)
ip += 2
return stdout
def dump(stdout):
return ",".join(map(str, stdout))
def parse_data(data):
lines = data.splitlines()
def extract_register_value(line):
return int(line.split()[-1])
registers = Registers(*map(extract_register_value, lines[:3]))
instructions = [int(x) for x in lines[-1].split()[-1].split(",")]
return registers, instructions
def part1():
registers, program = parse_data(read_input())
stdout = interpret(registers, program)
return dump(stdout)
def manual_for_a(a):
"""
For my program,
> 2,4,1,5,7,5,1,6,4,1,5,5,0,3,3,0
I can simplify it to these basic commands.
"""
b = c = 0
stdout = []
while a:
b = a % 8
b = b ^ 5
c = a // 2**b
b = b ^ 6
b = b ^ c
stdout.append(b % 8)
a = a >> 8
return stdout
def transpile(registers, program):
stdout = []
ip = 0
pstr = []
while ip < len(program):
opcode = Opcode(program[ip])
operand = program[ip + 1]
if op := operations.get(opcode):
registers, loc = op(registers, operand)
ip += 2
elif opcode == Opcode.JNZ:
ip, loc = jnz(registers, operand, ip)
return pstr
elif opcode == Opcode.OUT:
o, loc = out(registers, operand)
stdout.append(o)
ip += 2
pstr.append(loc)
return pstr
def simplified_for_a(pstr):
"""
Given a simplified program, return a function that computes it for any value of A.
"""
function = """
def p(a):
b = c = 0
stdout = []
while a:
"""
for line in pstr:
function += f" {line}\n"
function += " return stdout\n"
print(function)
exec(function, globals())
return globals()["p"]
def part2():
"""Find a value for A such that program p is a Quine, i.e. p(a) == p()"""
registers, program = parse_data(read_input())
python_program = simplified_for_a(transpile(registers, program))
candidates = {0}
# Starting from the last instruction (since it's the easiest),
# Work backward with greater and greater A values until we have the coplete program.
for instruction in reversed(program):
new_candidates = set()
for a in candidates:
shifted = a * 8
for candidate in range(shifted, shifted+8):
stdout = python_program(candidate)
if stdout and stdout[0] == instruction:
new_candidates.add(candidate)
candidates = new_candidates
return min(candidates)
if __name__ == "__main__":
print(part2())