diff --git a/noahover/aoc23/lib/day2.ex b/noahover/aoc23/lib/day2.ex index b44087c..8aacecb 100644 --- a/noahover/aoc23/lib/day2.ex +++ b/noahover/aoc23/lib/day2.ex @@ -68,4 +68,50 @@ defmodule Day2 do defp id_if_possible_loop(id, [_head | tail]) do id_if_possible_loop(id, tail) end + + @doc """ + Day 2 Part 1 + + Sum the power of the minimum blue, green, and red + needed to make the game possible + """ + def sum_game_powers(games) do + games + |> Enum.map(fn x -> game_power(x) end) + |> Enum.sum() + end + + def game_power(%{rounds: rounds}) do + rounds + |> Enum.reduce(%{blue: 0, green: 0, red: 0}, fn round, %{blue: mb, green: mg, red: mr} -> + rb = Map.get(round, :blue, 0) + rg = Map.get(round, :green, 0) + rr = Map.get(round, :red, 0) + + nb = + if rb > mb do + rb + else + mb + end + + ng = + if rg > mg do + rg + else + mg + end + + nr = + if rr > mr do + rr + else + mr + end + + %{blue: nb, green: ng, red: nr} + end) + |> Map.values() + |> Enum.product() + end end diff --git a/noahover/aoc23/test/day2_test.exs b/noahover/aoc23/test/day2_test.exs index 2638bbc..4fe990d 100644 --- a/noahover/aoc23/test/day2_test.exs +++ b/noahover/aoc23/test/day2_test.exs @@ -21,4 +21,22 @@ defmodule Day2Test do assert answer == 2278 end + + test "day 2 part 2 example 1 - sum game powers" do + answer = + File.read!("input/day2_example.txt") + |> parse_input() + |> sum_game_powers() + + assert answer == 2286 + end + + test "day 2 part 2 - sum game powers" do + answer = + File.read!("input/day2.txt") + |> parse_input() + |> sum_game_powers() + + assert answer == 67953 + end end