From fddb9ec0421a7d3772b6d8cec101c8a04ef358a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ytterstr=C3=B6m?= Date: Thu, 1 Dec 2022 08:15:39 +0100 Subject: [PATCH] Add solutions for 2022:1 Calorie Counting Lost a couple of minutes due to Elixir lang server in my editor fucked up. --- 2022-elixir/lib/solutions/day_01.ex | 45 ++++++++++++++++++++++ 2022-elixir/test/solutions/day_01_test.exs | 38 ++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 2022-elixir/lib/solutions/day_01.ex create mode 100644 2022-elixir/test/solutions/day_01_test.exs diff --git a/2022-elixir/lib/solutions/day_01.ex b/2022-elixir/lib/solutions/day_01.ex new file mode 100644 index 0000000..d13d44a --- /dev/null +++ b/2022-elixir/lib/solutions/day_01.ex @@ -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 diff --git a/2022-elixir/test/solutions/day_01_test.exs b/2022-elixir/test/solutions/day_01_test.exs new file mode 100644 index 0000000..0afca87 --- /dev/null +++ b/2022-elixir/test/solutions/day_01_test.exs @@ -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