Add solutions for 2022:1 Calorie Counting

Lost a couple of minutes due to Elixir lang server in my editor fucked up.
This commit is contained in:
Anders Englöf Ytterström 2022-12-01 08:15:39 +01:00 committed by Anders Englöf Ytterström
parent 5929ae11f6
commit fddb9ec042
2 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,45 @@
defmodule Aoc.Solution.Day01 do
@name "Day 1: Calorie Counting"
@behaviour Solution
@impl Solution
def get_name, do: @name
@impl Solution
def present(solution), do: "Most carrying elf is carrying #{solution} cal worth of snacks"
@impl Solution
def present_again(solution),
do: "3 most carrying elfes are carrying #{solution} cal worth of snacks"
@impl Solution
def parse!(raw) do
raw
|> Aoc.Utils.parse_values("\n\n")
|> Enum.map(&Aoc.Utils.parse_values/1)
end
@impl Solution
def solve(input) do
input
|> Enum.map(&sum_calories/1)
|> Enum.max()
end
@impl Solution
def solve_again(input) do
[a, b, c | _] =
input
|> Enum.map(&sum_calories/1)
|> Enum.sort()
|> Enum.reverse()
Enum.sum([a, b, c])
end
def sum_calories(rows) do
rows
|> Enum.map(&String.to_integer/1)
|> Enum.sum()
end
end

View file

@ -0,0 +1,38 @@
defmodule Day01Test do
use ExUnit.Case
doctest Aoc.Solution.Day01
import Aoc.Solution.Day01
@input ~s(
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
)
test "01: Calorie Counting, part 1" do
expected = 24000
result = @input |> parse!() |> solve()
assert result == expected
end
test "01: Calorie Counting, part 2" do
expected = 45000
result = @input |> parse!() |> solve_again()
assert result == expected
end
end