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:
parent
5929ae11f6
commit
fddb9ec042
2 changed files with 83 additions and 0 deletions
45
2022-elixir/lib/solutions/day_01.ex
Normal file
45
2022-elixir/lib/solutions/day_01.ex
Normal 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
|
||||
38
2022-elixir/test/solutions/day_01_test.exs
Normal file
38
2022-elixir/test/solutions/day_01_test.exs
Normal 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
|
||||
Loading…
Add table
Reference in a new issue