-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.exs
40 lines (36 loc) · 770 Bytes
/
app.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
defmodule Solution do
def part1(input) do
calc_elves_total_calories(input)
|> Enum.max
end
def part2(input) do
calc_elves_total_calories(input)
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.sum()
end
defp calc_elves_total_calories(input) do
input
|> Enum.chunk_while(
[],
fn el, acc ->
if el == "" do
{:cont, acc, []}
else
{num, _} = Integer.parse(el)
{:cont, [num | acc]}
end
end,
fn
[] -> {:cont, []}
acc -> {:cont, Enum.reverse(acc), []}
end
)
|> Enum.map(&Enum.sum/1)
end
end
raw_input =
IO.read(:stdio, :all)
|> String.split("\n")
IO.inspect(Solution.part1(raw_input))
IO.inspect(Solution.part2(raw_input))