forked from epsalt/aoc2019
/
day13.py
263 lines (183 loc) · 5.77 KB
/
day13.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from collections import defaultdict, namedtuple
import curses
from itertools import zip_longest
from time import sleep
State = namedtuple("State", ["prog", "loc", "rel_base", "inputs", "output"])
with open("input.txt") as f:
line = f.readline()
prog = [int(n) for n in line.split(",")]
def add(state, resolved, write):
prog = state.prog
prog[write] = sum(resolved)
return State(prog, state.loc + 4, state.rel_base, state.inputs, None)
def mul(state, resolved, write):
a, b = resolved
prog = state.prog
prog[write] = a * b
return State(prog, state.loc + 4, state.rel_base, state.inputs, None)
def _input(state, resolved, write):
prog = state.prog
prog[write] = int(state.inputs.pop())
return State(prog, state.loc + 2, state.rel_base, state.inputs, None)
def _print(state, resolved, write):
output = resolved[0]
loc = state.loc + 2
return State(state.prog, loc, state.rel_base, state.inputs, output)
def jit(state, resolved, write):
a, b = resolved
loc = state.loc
if a:
loc = b
else:
loc = loc + 3
return State(state.prog, loc, state.rel_base, state.inputs, None)
def jif(state, resolved, write):
a, b = resolved
loc = state.loc
if not a:
loc = b
else:
loc = loc + 3
return State(state.prog, loc, state.rel_base, state.inputs, None)
def lt(state, resolved, write):
a, b = resolved
prog = state.prog
prog[write] = a < b
return State(prog, state.loc + 4, state.rel_base, state.inputs, None)
def eq(state, resolved, write):
a, b = resolved
prog = state.prog
prog[write] = a == b
return State(prog, state.loc + 4, state.rel_base, state.inputs, None)
def rel(state, resolved, write):
rel_base = state.rel_base + resolved[0]
return State(state.prog, state.loc + 2, rel_base, state.inputs, None)
def decompose(n):
padded = str(n).zfill(5)
modes = [int(i) for i in padded[:3]]
opcode = int(padded[3:])
return opcode, modes
def resolve_param(state, param, mode):
if mode == 0:
resolved = state.prog[param]
elif mode == 1:
resolved = param
elif mode == 2:
resolved = state.prog[param + state.rel_base]
return resolved
def resolve_write(state, wb, mode):
if mode == 0:
write = state.prog[state.loc + wb]
elif mode == 2:
write = state.prog[state.loc + wb] + state.rel_base
return write
def run(state):
## (function, n_params, write mode bit)
codes = {
1: (add, 2, 3),
2: (mul, 2, 3),
3: (_input, 0, 1),
4: (_print, 1, 0),
5: (jit, 2, 0),
6: (jif, 2, 0),
7: (lt, 2, 3),
8: (eq, 2, 3),
9: (rel, 1, 0),
}
while True:
opcode, modes = decompose(state.prog[state.loc])
if opcode == 99:
return None
modes.reverse()
fun, pcount, wb = codes[opcode]
params = state.prog[state.loc + 1 : state.loc + pcount + 1]
resolved = [
resolve_param(state, param, mode) for param, mode in zip(params, modes)
]
write_mode = modes[wb - 1]
write = resolve_write(state, wb, write_mode)
state = fun(state, resolved, write)
if state.output is not None:
return state
else:
pass
def grouper(iterable, n, fillvalue=None):
# https://docs.python.org/3.8/library/itertools.html#itertools-recipes
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def part1(prog, memory=2 ** 8):
state = State(prog + [0] * memory, 0, 0, None, None)
board_state = {}
output = []
while True:
state = run(state)
if state is None:
break
else:
output.append(state.output)
for x, y, tile_id in grouper(output, 3):
board_state[(x, y)] = tile_id
return sum(1 for tile_id in board_state.values() if tile_id == 2)
def update(stdscr, x, y, tid, score):
tiles = {0: " ", 1: "|", 2: "#", 3: "_", 4: "*"}
glyph = tiles[tid]
stdscr.addstr(y, x, glyph)
stdscr.addstr(25, 0, "Score: {}".format(score))
sleep(0.01)
stdscr.refresh()
def next_move(ball, paddle):
bx, px = ball[0], paddle[0]
if bx == px:
move = 0
elif bx > px:
move = 1
else:
move = -1
return move
def part2(prog, memory=2 ** 8):
prog[0] = 2
state = State(prog + [0] * memory, 0, 0, None, None)
score = 0
board_state = defaultdict(int)
output = []
ball = None
paddle = None
stdscr = curses.initscr()
curses.delay_output(100)
curses.noecho()
curses.cbreak()
while True:
state = run(state)
if state is None:
break
else:
output.append(state.output)
if len(output) == 3:
x, y, tid = output
## Check for score and update screen
if x == -1:
score = tid
else:
board_state[(x, y)] = tid
update(stdscr, x, y, tid, score)
## Check for ball or paddle
if tid == 4:
ball = (x, y)
elif tid == 3:
paddle = (x, y)
if ball is not None and paddle is not None:
move = next_move(ball, paddle)
else:
move = None
## Clear output and update state
output = []
state = State(state.prog, state.loc, state.rel_base, [move], None)
else:
pass
curses.echo()
curses.nocbreak()
curses.endwin()
return board_state, score
if __name__ == "__main__":
board_state, score = part2(prog)
print("Final score {}".format(score))