-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.ex
395 lines (308 loc) · 9.82 KB
/
day17.ex
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
defmodule Amplifier.Block do
defstruct memory: %{}, inputs: [], offset: 0, base_pos: 0
@type t :: %__MODULE__{
memory: map(),
inputs: list(),
offset: non_neg_integer(),
base_pos: integer()
}
@spec update(t(), keyword()) :: t()
def update(block, attrs) do
fields = __MODULE__.__struct__() |> Map.keys() |> Enum.reject(&(&1 == :__struct__))
attrs
|> Enum.filter(fn {k, _} -> k in fields end)
|> Enum.reduce(block, fn {k, v}, acc -> Map.put(acc, k, v) end)
end
end
defmodule Amplifier.Executor do
alias Amplifier.Block
@add 1
@multiply 2
@input 3
@output 4
@jump_if_true 5
@jump_if_false 6
@less_than 7
@equals 8
@relative_base 9
@exit 99
def exec(%Block{memory: memory, offset: offset, inputs: inputs, base_pos: base_pos} = block) do
operation = Map.get(memory, offset)
{operator, param1_mode, param2_mode, param3_mode} = parse_operation(operation)
case operator do
@add ->
new_val =
get_value(memory, offset + 1, param1_mode, base_pos) +
get_value(memory, offset + 2, param2_mode, base_pos)
memory = update_memory(memory, offset + 3, new_val, param3_mode, base_pos)
block
|> Block.update(memory: memory, offset: offset + 4)
|> exec()
@multiply ->
new_val =
get_value(memory, offset + 1, param1_mode, base_pos) *
get_value(memory, offset + 2, param2_mode, base_pos)
memory = update_memory(memory, offset + 3, new_val, param3_mode, base_pos)
block
|> Block.update(memory: memory, offset: offset + 4)
|> exec()
@input when inputs == [] ->
{:input, block}
@input ->
memory = update_memory(memory, offset + 1, hd(inputs), param1_mode, base_pos)
block
|> Block.update(memory: memory, offset: offset + 2, inputs: tl(inputs))
|> exec()
@output ->
{
:output,
Block.update(block, offset: offset + 2),
get_value(memory, offset + 1, param1_mode, base_pos)
}
@jump_if_true ->
next_offset =
if get_value(memory, offset + 1, param1_mode, base_pos) != 0,
do: get_value(memory, offset + 2, param2_mode, base_pos),
else: offset + 3
block
|> Block.update(offset: next_offset)
|> exec()
@jump_if_false ->
next_offset =
if get_value(memory, offset + 1, param1_mode, base_pos) == 0,
do: get_value(memory, offset + 2, param2_mode, base_pos),
else: offset + 3
block
|> Block.update(offset: next_offset)
|> exec()
@less_than ->
param1 = get_value(memory, offset + 1, param1_mode, base_pos)
param2 = get_value(memory, offset + 2, param2_mode, base_pos)
memory =
if param1 < param2 do
update_memory(memory, offset + 3, 1, param3_mode, base_pos)
else
update_memory(memory, offset + 3, 0, param3_mode, base_pos)
end
block
|> Block.update(memory: memory, offset: offset + 4)
|> exec()
@equals ->
param1 = get_value(memory, offset + 1, param1_mode, base_pos)
param2 = get_value(memory, offset + 2, param2_mode, base_pos)
memory =
if param1 == param2 do
update_memory(memory, offset + 3, 1, param3_mode, base_pos)
else
update_memory(memory, offset + 3, 0, param3_mode, base_pos)
end
block
|> Block.update(memory: memory, offset: offset + 4)
|> exec()
@relative_base ->
param1 = get_value(memory, offset + 1, param1_mode, base_pos)
block
|> Block.update(offset: offset + 2, base_pos: base_pos + param1)
|> exec()
@exit ->
:exit
end
end
defp parse_operation(operation) do
operator = rem(operation, 100)
param1_mode = operation |> div(100) |> rem(10)
param2_mode = operation |> div(1000) |> rem(10)
param3_mode = operation |> div(10000)
{operator, param1_mode, param2_mode, param3_mode}
end
defp get_value(memory, offset, 0, _base_pos) do
pos = Map.get(memory, offset, 0)
Map.get(memory, pos, 0)
end
defp get_value(memory, offset, 1, _base_pos), do: Map.get(memory, offset, 0)
defp get_value(memory, offset, 2, base_pos) do
pos = Map.get(memory, offset, 0)
Map.get(memory, base_pos + pos, 0)
end
defp update_memory(memory, offset, value, 0, _base_pos) do
pos = Map.get(memory, offset, 0)
Map.put(memory, pos, value)
end
defp update_memory(memory, offset, value, 2, base_pos) do
pos = Map.get(memory, offset, 0)
Map.put(memory, base_pos + pos, value)
end
end
defmodule Amplifier.Memory do
alias Amplifier.Block
def run(name, %Block{} = block) do
setup_ets(name, block)
end
def get(ets_name) do
:ets.lookup(ets_name, :block) |> Keyword.get(:block)
end
def set(ets_name, %Block{} = block) do
:ets.insert(ets_name, {:block, block})
end
defp setup_ets(name, %Block{} = block) do
ets_name = String.to_atom("ets-#{name}")
:ets.new(ets_name, [:set, :public, :named_table])
:ets.insert(ets_name, {:block, block})
ets_name
end
end
defmodule Amplifier do
use GenServer
alias Amplifier.Executor
alias Amplifier.Memory
alias Amplifier.Block
# Client
def start_link(name: name, memory: memory) do
GenServer.start_link(__MODULE__, {name, memory}, name: name)
end
def exec(name) do
GenServer.call(name, :exec)
end
def push_input(name, input) do
GenServer.cast(name, {:push_input, input})
end
def exit(name) do
GenServer.stop(name)
end
# Server
def init({name, memory}) do
block = %Block{memory: memory}
{:ok, %{ets_name: Memory.run(name, block)}}
end
def handle_call(:exec, _from, %{:ets_name => ets_name} = state) do
ets_name
|> Memory.get()
|> Executor.exec()
|> case do
{:output, block, output} ->
Memory.set(ets_name, block)
{:reply, {:output, output}, state}
{:input, block} ->
Memory.set(ets_name, block)
{:reply, :input, state}
:exit ->
{:reply, :exit, state}
end
end
def handle_cast({:push_input, inputs}, %{:ets_name => ets_name} = state) when is_list(inputs) do
ets_name
|> Memory.get()
|> Map.update(:inputs, inputs, &(&1 ++ inputs))
|> then(&Memory.set(ets_name, &1))
{:noreply, state}
end
def handle_cast({:push_input, input}, %{:ets_name => ets_name} = state) do
ets_name
|> Memory.get()
|> Map.update(:inputs, [input], &(&1 ++ [input]))
|> then(&Memory.set(ets_name, &1))
{:noreply, state}
end
end
defmodule Day17 do
@amplifier :amplifier
@dir [{-1, 0}, {1, 0}, {0, -1}, {0, 1}]
@scaffold ?#
@blank ?.
def part1(input) do
Amplifier.start_link(name: @amplifier, memory: parse_input(input))
run_amplifier()
|> tap(fn _ -> Amplifier.exit(@amplifier) end)
# |> tap(&print/1)
|> get_alignment_parameters()
|> Enum.sum()
|> IO.inspect(label: "part1")
end
def part2(input) do
memory = input |> parse_input() |> Map.put(0, 2)
Amplifier.start_link(name: @amplifier, memory: memory)
continuous? = false
setup_operation(continuous?)
run_amplifier_part_2('', continuous?)
|> tap(fn _ -> Amplifier.exit(@amplifier) end)
|> IO.inspect(label: "part2")
end
defp parse_input(input) do
input
|> String.split(",", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.into(%{}, fn {val, i} -> {i, val} end)
end
defp run_amplifier(grid \\ %{}, {y, x} = pos \\ {0, 0}) do
case Amplifier.exec(@amplifier) do
{:output, ?\n} ->
run_amplifier(grid, {y + 1, 0})
{:output, value} ->
grid
|> Map.put(pos, value)
|> run_amplifier({y, x + 1})
:exit ->
grid
end
end
defp run_amplifier_part_2(str, continuous?) do
case Amplifier.exec(@amplifier) do
{:output, value} when value > 255 ->
value
{:output, ?\n} ->
IO.puts(str)
if continuous? == true and str == '' do
Process.sleep(200)
IO.puts("\e[H\e[2J")
end
run_amplifier_part_2('', continuous?)
{:output, value} ->
run_amplifier_part_2(str ++ [value], continuous?)
end
end
defp print(grid) do
{{min_y, min_x}, {max_y, max_x}} = min_max_pos(grid)
for i <- min_y..max_y, into: <<>> do
line = for j <- min_x..max_x, do: Map.get(grid, {i, j}, '.')
(line ++ ['\n'])
|> List.to_string()
end
|> IO.puts()
end
defp get_alignment_parameters(grid) do
{{min_y, min_x}, _} = min_max_pos(grid)
grid
|> Map.keys()
|> Enum.filter(&intersection?(grid, &1))
|> Enum.map(fn {y, x} -> (y - min_y) * (x - min_x) end)
end
defp intersection?(grid, {y, x} = pos) do
if Map.get(grid, pos) == @scaffold do
Enum.all?(@dir, fn {dy, dx} -> Map.get(grid, {y + dy, x + dx}) == @scaffold end)
else
false
end
end
defp min_max_pos(grid) do
{{min_y, _}, {max_y, _}} = grid |> Map.keys() |> Enum.min_max_by(fn {y, _x} -> y end)
{{_, min_x}, {_, max_x}} = grid |> Map.keys() |> Enum.min_max_by(fn {_y, x} -> x end)
{{min_y, min_x}, {max_y, max_x}}
end
defp setup_operation(continuous?) do
# 손수 구했다 이말이야
# routine: A, B, B, A, C, A, C, A, C, B
# A: L,6,R,12,R,8
# B: R,8,R,12,L,12
# C: R,12,L,12,L,4,L,4
Amplifier.push_input(@amplifier, 'A,B,B,A,C,A,C,A,C,B\n')
Amplifier.push_input(@amplifier, 'L,6,R,12,R,8\n')
Amplifier.push_input(@amplifier, 'R,8,R,12,L,12\n')
Amplifier.push_input(@amplifier, 'R,12,L,12,L,4,L,4\n')
continuous = if continuous?, do: 'y\n', else: 'n\n'
Amplifier.push_input(@amplifier, continuous)
end
end
File.read!("input.txt")
|> tap(&Day17.part1/1)
|> tap(&Day17.part2/1)