From e02d027b1dded94078110ba9f0ff4026c5e5ebeb Mon Sep 17 00:00:00 2001 From: Samuel Owen Date: Fri, 29 Dec 2023 17:38:10 +0000 Subject: [PATCH] Add get_minimal_map function to calculate the minimal map of a game --- lib/day2/day2.ex | 19 +++++++++++++++++++ test/day2/day2_test.exs | 19 +++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/day2/day2.ex b/lib/day2/day2.ex index 7eca4af..68350f3 100644 --- a/lib/day2/day2.ex +++ b/lib/day2/day2.ex @@ -111,4 +111,23 @@ defmodule Day2 do blue = if(cube_map[:blue] == 0, do: 1, else: cube_map[:blue]) red * green * blue end + + def get_minimal_map(game_string) do + game_sets = parse_game_sets(game_string) + + game_set_maps = + Enum.reduce(game_sets, [], fn set, acc -> + [parse_game_set_amounts(set) | acc] + end) + + max_red = get_max_cubes(game_set_maps, :red) + max_green = get_max_cubes(game_set_maps, :green) + max_blue = get_max_cubes(game_set_maps, :blue) + + %{ + :red => max_red, + :green => max_green, + :blue => max_blue + } + end end diff --git a/test/day2/day2_test.exs b/test/day2/day2_test.exs index d39b252..5931b21 100644 --- a/test/day2/day2_test.exs +++ b/test/day2/day2_test.exs @@ -166,14 +166,17 @@ defmodule Day2Test do assert Day2.get_max_cubes(game_1_sets, :green) == 2 end - # test "returns the power of a set of cubes" do - # # The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. - # game_1_string = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green" - # assert Day2.get_power_of_set(game_1_string) == 48 - # end - - # We need a test for a set string, not just the extracted map as we have above. - # ... test here: + test "returns the minimal map of a game" do + # The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. + game_1_string = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green" + + assert Day2.get_minimal_map(game_1_string) == %{ + :red => 4, + :green => 2, + :blue => 6 + } + end + test "returns the power of a set string" do game_set_string = "3 blue, 4 red" assert Day2.get_power_of_set(game_set_string) == 12