From 2af0a9f6c1b2c6cab7df0be359b7d6359dd8484b Mon Sep 17 00:00:00 2001 From: Caleb Webber Date: Wed, 25 Oct 2023 00:13:37 -0400 Subject: [PATCH] day 1 part 2 --- day1.ex | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/day1.ex b/day1.ex index eb70ae5..92b6605 100644 --- a/day1.ex +++ b/day1.ex @@ -7,9 +7,15 @@ defmodule Exercise1 do end end - def init_state() do - {nil, 0, false} + def get_answer(state) do + elem(state, 1) |> + Enum.reduce(0, fn i, acc -> i + acc end) end + + def init_state() do + {nil, [0, 0, 0], false} + end + def apply_command(command, state) do case command do {:add, n} -> apply_add(n, state) @@ -20,7 +26,10 @@ defmodule Exercise1 do defp apply_add(n, state) do {current, total, exit} = state 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 defp apply_reset(state) do @@ -42,4 +51,4 @@ result = IO.stream() |> end ) - IO.puts(elem(result, 1)) + IO.puts(Exercise1.get_answer(result))