-
Notifications
You must be signed in to change notification settings - Fork 3
/
computer_server.ex
75 lines (61 loc) · 1.93 KB
/
computer_server.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
defmodule ElixirDay07.ComputerServer do
use GenServer
alias ElixirDay07.{Computer}
@doc """
start/2: Start a new genserver. Provide it an intcode program to run, with
some inputs. It'll start executing immediately.
program: a list of ints (intcode) to run
input: a list of ints to feed to the computer as input, provide empty list if none
"""
def start(program, input) when is_list(program) and is_list(input) do
GenServer.start(__MODULE__, {program, input})
end
@doc """
outputs/1: Get the current list of outputs. Does not modify.
"""
def outputs(pid) do
GenServer.call(pid, :outputs)
end
@doc """
pop_output/1: Return the oldest output from the computer.
As a side effect, remove it from the computer's list of outputs.
"""
def pop_output(pid) do
GenServer.call(pid, :pop_output)
end
@doc """
add_input/1: Feed an input to the computer. It will continue executing
if able.
"""
def add_input(pid, input) do
GenServer.call(pid, {:add_input, input})
end
def halted?(pid) do
GenServer.call(pid, :halted?)
end
#########
def init({program, input}) when is_list(program) and is_list(input) do
# Note: This forces the caller of init to wait for the
# program to finish executing (they're blocked). Can be fixed
# by having the GenServer send an execute() message to itself.
c = Computer.new(program, input) |> Computer.execute()
{:ok, c}
end
def handle_call(:outputs, _from, state) do
{:reply, Computer.outputs(state), state}
end
def handle_call(:pop_output, _from, state) do
{output, new_state} = Computer.pop_output(state)
{:reply, output, new_state}
end
def handle_call(:halted?, _from, state) do
{:reply, Computer.halted?(state), state}
end
def handle_call({:add_input, input}, _from, state) do
new_state =
state
|> Computer.add_input(input)
|> Computer.execute()
{:reply, :ok, new_state}
end
end