-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve-2.exs
59 lines (46 loc) · 1.28 KB
/
solve-2.exs
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
defmodule Solve do
use Agent
def start do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
def simulate([_], days) when days == 0 do
1
end
def simulate([fish], days) do
cached_value = memo(fish, days)
if cached_value do
cached_value
else
value = simulate(fish |> tick(), days - 1)
memo(fish, days, value)
value
end
end
def simulate(fish, days) do
cached_value = memo(fish, days)
if cached_value do
cached_value
else
[head | tail] = fish
value = simulate([head], days) + simulate(tail, days)
memo(fish, days, value)
value
end
end
def tick(fish) when fish == 0, do: [6, 8]
def tick(fish), do: [fish - 1]
def memo(fish, days) do
Agent.get(__MODULE__, &(Map.get(&1, "#{fish}::#{days}")))
end
def memo(fish, days, value) do
Agent.update(__MODULE__, &(Map.put(&1, "#{fish}::#{days}", value)))
end
end
{[input: raw_input, days: days], _, _} = OptionParser.parse(System.argv(), strict: [input: :string, days: :integer])
{:ok, _} = Solve.start
solution = File.stream!(raw_input)
|> Stream.map(&String.trim/1)
|> Stream.map(&(String.split(&1, ",")))
|> Enum.flat_map(fn line -> Enum.map(line, &String.to_integer/1) end)
|> Solve.simulate(days)
IO.inspect(solution)