day 1 part 2
This commit is contained in:
parent
87fd00fe94
commit
2af0a9f6c1
1 changed files with 13 additions and 4 deletions
17
day1.ex
17
day1.ex
|
@ -7,9 +7,15 @@ defmodule Exercise1 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def init_state() do
|
def get_answer(state) do
|
||||||
{nil, 0, false}
|
elem(state, 1) |>
|
||||||
|
Enum.reduce(0, fn i, acc -> i + acc end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def init_state() do
|
||||||
|
{nil, [0, 0, 0], false}
|
||||||
|
end
|
||||||
|
|
||||||
def apply_command(command, state) do
|
def apply_command(command, state) do
|
||||||
case command do
|
case command do
|
||||||
{:add, n} -> apply_add(n, state)
|
{:add, n} -> apply_add(n, state)
|
||||||
|
@ -20,7 +26,10 @@ defmodule Exercise1 do
|
||||||
defp apply_add(n, state) do
|
defp apply_add(n, state) do
|
||||||
{current, total, exit} = state
|
{current, total, exit} = state
|
||||||
new_current = if is_nil(current) do 0 else current end + n
|
new_current = if is_nil(current) do 0 else current end + n
|
||||||
{new_current, max(total, new_current), exit}
|
new_total = [ new_current | total ] |>
|
||||||
|
Enum.sort(:desc) |>
|
||||||
|
Enum.take(3)
|
||||||
|
{new_current, new_total, exit}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp apply_reset(state) do
|
defp apply_reset(state) do
|
||||||
|
@ -42,4 +51,4 @@ result = IO.stream() |>
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
IO.puts(elem(result, 1))
|
IO.puts(Exercise1.get_answer(result))
|
||||||
|
|
Loading…
Add table
Reference in a new issue