-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintcode.ex
178 lines (147 loc) · 4.47 KB
/
intcode.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
defmodule AdventOfCode.Y2019.IntCode do
@moduledoc """
The IntCode computer.
"""
use GenServer
@type instruction :: non_neg_integer()
@type padded_instruction() :: binary()
@type memory :: list(integer())
@type state :: %{
data: memory(),
iptr: non_neg_integer() | :halt,
next: list(binary()),
output: list(any()),
start_val: any()
}
# ------------------------- COMMANDS
@store "00003"
# Binary operations
@bins [3, 4, 104]
@halt "00099"
@doc """
Starts an IntCode computer by taking in a list of integers as instructionset.
"""
@spec start_link(memory()) :: GenServer.on_start()
def start_link(opts), do: GenServer.start_link(__MODULE__, opts)
@doc """
Returns the current state of the server
"""
@spec get_state(pid()) :: state()
def get_state(pid), do: GenServer.call(pid, :state)
@doc """
Returns the output of the run
"""
def get_output(pid), do: GenServer.call(pid, :output)
@doc """
Resets the computer with new memory feed
"""
def reset(pid, data), do: GenServer.call(pid, {:reset, data})
@spec run(pid()) :: state()
def run(pid) do
case get_state(pid) do
%{iptr: :halt} ->
get_state(pid)
%{next: next} ->
GenServer.call(pid, {:cmd, next})
run(pid)
end
end
@impl true
def init(data) do
{:ok,
%{
data: data,
next: tokenize(data),
iptr: 0,
output: [],
start_val: 1
}}
end
@impl true
def handle_call(:state, _from, state), do: {:reply, state, state}
@impl true
def handle_call(:output, _from, %{data: [out | _]} = state), do: {:reply, out, state}
def handle_call(:output, _from, state), do: {:reply, nil, state}
@impl true
def handle_call({:reset, data}, _from, _) do
{:ok, state} = init(data)
{:reply, state, state}
end
@impl true
def handle_call({:cmd, [@halt | _]}, _from, state) do
state = Map.put(state, :iptr, :halt)
{:reply, state, state}
end
@impl true
def handle_call(
{:cmd, [@store, addr]},
_from,
%{start_val: start_val, data: data} = state
) do
data = List.replace_at(data, addr, start_val)
state =
state
|> Map.put(:data, data)
|> Map.update(:iptr, 0, fn iptr -> iptr + 2 end)
|> Map.put(:next, tokenize(Enum.drop(data, 2)))
{:reply, state, state}
end
@impl true
def handle_call(
{:cmd, vector},
_from,
%{data: data, output: output, iptr: iptr} = state
) do
{data, output} = execute(vector, data, output)
ptr_incr = iptr + length(vector)
state =
state
|> Map.merge(%{
data: data,
iptr: ptr_incr,
output: output,
next: tokenize(Enum.drop(data, ptr_incr))
})
{:reply, state, state}
end
# Pads the int-command with zeroes
@spec pad(instruction()) :: binary()
defp pad(instruction) do
instruction
|> to_string()
|> String.pad_leading(5, "0")
end
# Extracts command and parameter from a frame of memory
@spec tokenize(memory()) :: [padded_instruction() | memory()]
defp tokenize([99 | _]), do: List.wrap(pad(99))
defp tokenize([x, a | _]) when x in @bins, do: [pad(x), a]
defp tokenize([x, a, b, c | _]), do: [pad(x), a, b, c]
defp tokenize(_), do: raise("Invalid opcode")
# Exexcutes a command from a `instruction_vector`
defp execute(["00001", a, b, x], state, output) do
{List.replace_at(state, x, Enum.at(state, a) + Enum.at(state, b)), output}
end
defp execute(["00101", a, b, x], state, output) do
{List.replace_at(state, x, a + Enum.at(state, b)), output}
end
defp execute(["01001", a, b, x], state, output) do
{List.replace_at(state, x, Enum.at(state, a) + b), output}
end
defp execute(["01101", a, b, x], state, output) do
{List.replace_at(state, x, a + b), output}
end
defp execute(["00002", a, b, x], state, output) do
{List.replace_at(state, x, Enum.at(state, a) * Enum.at(state, b)), output}
end
defp execute(["00102", a, b, x], state, output) do
{List.replace_at(state, x, a * Enum.at(state, b)), output}
end
defp execute(["01002", a, b, x], state, output) do
{List.replace_at(state, x, Enum.at(state, a) * b), output}
end
defp execute(["01102", a, b, x], state, output) do
{List.replace_at(state, x, a * b), output}
end
defp execute(["00004", x], state, output), do: {state, [Enum.at(state, x) | output]}
defp execute(["00104", x], state, output), do: {state, [x | output]}
end