2020-12-15 06:53:41 +00:00
|
|
|
defmodule Day15 do
|
2020-12-15 06:02:47 +00:00
|
|
|
@input [18, 8, 0, 5, 4, 1, 20]
|
|
|
|
|
|
|
|
def run do
|
|
|
|
[stop] = System.argv()
|
|
|
|
|
|
|
|
start(@input, nil, 1, String.to_integer(stop), %{})
|
|
|
|
|> IO.puts()
|
|
|
|
end
|
|
|
|
|
2020-12-16 00:08:31 +00:00
|
|
|
def start([], prev_num, round, stop, state), do: play(prev_num, round, stop, state)
|
2020-12-15 06:02:47 +00:00
|
|
|
|
2020-12-16 00:08:31 +00:00
|
|
|
def start([num | rest], _prev_num, round, stop, state) do
|
2020-12-15 06:02:47 +00:00
|
|
|
start(rest, num, round + 1, stop, Map.put(state, num, {round, round}))
|
|
|
|
end
|
|
|
|
|
2020-12-16 00:08:31 +00:00
|
|
|
def play(prev_num, stop, stop, state), do: next_num(prev_num, state)
|
|
|
|
|
|
|
|
def play(prev_num, round, stop, state) do
|
|
|
|
num = next_num(prev_num, state)
|
|
|
|
state = Map.update(state, num, {round, round}, fn {_a, b} -> {b, round} end)
|
|
|
|
play(num, round + 1, stop, state)
|
2020-12-15 06:02:47 +00:00
|
|
|
end
|
|
|
|
|
2020-12-16 00:08:31 +00:00
|
|
|
def next_num(prev_num, state) do
|
|
|
|
{a, b} = state[prev_num]
|
|
|
|
b - a
|
2020-12-15 06:02:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 06:53:41 +00:00
|
|
|
Day15.run()
|