Skip to content

Commit

Permalink
Add get_minimal_map function to calculate the minimal map of a game
Browse files Browse the repository at this point in the history
  • Loading branch information
samjowen committed Dec 29, 2023
1 parent 08d2a98 commit e02d027
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
19 changes: 19 additions & 0 deletions lib/day2/day2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 11 additions & 8 deletions test/day2/day2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e02d027

Please sign in to comment.