-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.ex
237 lines (187 loc) · 6.48 KB
/
day9.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
defmodule Amplifier.Executor do
@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(memory, offset, inputs, base_pos \\ 0) 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)
exec(memory, offset + 4, inputs, base_pos)
@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)
exec(memory, offset + 4, inputs, base_pos)
@input ->
memory = update_memory(memory, offset + 1, hd(inputs), param1_mode, base_pos)
exec(memory, offset + 2, tl(inputs), base_pos)
@output ->
IO.inspect(get_value(memory, offset + 1, param1_mode, base_pos))
exec(memory, offset + 2, inputs, base_pos)
@jump_if_true ->
if get_value(memory, offset + 1, param1_mode, base_pos) != 0 do
jump_offset = get_value(memory, offset + 2, param2_mode, base_pos)
exec(memory, jump_offset, inputs, base_pos)
else
exec(memory, offset + 3, inputs, base_pos)
end
@jump_if_false ->
if get_value(memory, offset + 1, param1_mode, base_pos) == 0 do
jump_offset = get_value(memory, offset + 2, param2_mode, base_pos)
exec(memory, jump_offset, inputs, base_pos)
else
exec(memory, offset + 3, inputs, base_pos)
end
@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
exec(memory, offset + 4, inputs, base_pos)
@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
exec(memory, offset + 4, inputs, base_pos)
@relative_base ->
param1 = get_value(memory, offset + 1, param1_mode, base_pos)
exec(memory, offset + 2, inputs, base_pos + param1)
@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 do
use GenServer
require Amplifier.Executor
alias Amplifier.Executor
# Client
def start_link(name: name, memory: memory) do
GenServer.start_link(__MODULE__, {name, memory}, name: name)
end
def push_input(name, inputs) when is_list(inputs) do
GenServer.call(name, {:push_inputs, inputs})
end
def push_input(name, input) do
GenServer.call(name, {:push_inputs, [input]})
end
def run(name) do
GenServer.call(name, :run)
end
def exit(name) do
GenServer.stop(name)
end
# Server
def init({name, memory}) do
{:ok, %{ets_name: setup_ets(name, memory)}}
end
def handle_call({:push_inputs, inputs}, _from, %{:ets_name => ets_name} = state) do
prev_inputs = get_element_of_ets(ets_name, :inputs)
set_element_of_ets(ets_name, :inputs, prev_inputs ++ inputs)
{:reply, :ok, state}
end
def handle_call(:run, _from, %{:ets_name => ets_name} = state) do
memory = get_element_of_ets(ets_name, :memory)
offset = get_element_of_ets(ets_name, :offset)
inputs = get_element_of_ets(ets_name, :inputs)
case Executor.exec(memory, offset, inputs) do
{:output, output, remain_inputs, updated_memory, next_offset} ->
set_element_of_ets(ets_name, :memory, updated_memory)
set_element_of_ets(ets_name, :inputs, remain_inputs)
set_element_of_ets(ets_name, :offset, next_offset)
{:reply, {:pause, output}, state}
:exit ->
{:reply, :exit, state}
_ ->
{:reply, :error, state}
end
end
defp setup_ets(name, memory) do
ets_name = String.to_atom("ets-#{name}")
:ets.new(ets_name, [:set, :protected, :named_table])
:ets.insert(ets_name, {:memory, memory})
:ets.insert(ets_name, {:offset, 0})
:ets.insert(ets_name, {:inputs, []})
ets_name
end
defp get_element_of_ets(ets_name, key) do
:ets.lookup(ets_name, key) |> Keyword.get(key)
end
defp set_element_of_ets(ets_name, key, value) do
:ets.insert(ets_name, {key, value})
end
end
defmodule Day9 do
def part1(input) do
name = :amplifier1
memory = parse_input(input)
Amplifier.start_link(name: name, memory: memory)
Amplifier.push_input(name, 1)
Amplifier.run(name)
Amplifier.exit(name)
end
def part2(input) do
name = :amplifier2
memory = parse_input(input)
Amplifier.start_link(name: name, memory: memory)
Amplifier.push_input(name, 2)
Amplifier.run(name)
Amplifier.exit(name)
end
defp parse_input(input) do
input
|> String.split(",", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.with_index()
|> Enum.reduce(%{}, fn {val, i}, acc -> Map.put(acc, i, val) end)
end
end
File.read!("input.txt")
|> tap(&Day9.part1/1)
|> tap(&Day9.part2/1)