Skip to content

Commit

Permalink
2023 Day 2 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
noahko96 committed Dec 21, 2023
1 parent 4536557 commit 12feb6a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
46 changes: 46 additions & 0 deletions noahover/aoc23/lib/day2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions noahover/aoc23/test/day2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 12feb6a

Please sign in to comment.