Skip to content

Commit

Permalink
Refactor game set validation logic, fix greater than or equal to bug
Browse files Browse the repository at this point in the history
  • Loading branch information
samjowen committed Dec 28, 2023
1 parent 45cc5a3 commit ba10ddf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
23 changes: 22 additions & 1 deletion lib/day2/day2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ defmodule Day2 do
def validate_set_by_colour_and_amount(game_set_string, cube_map) do
# cube_map will be in the shape of a %{:red => x, :blue => y...}
set_cube_map = parse_game_set_amounts(game_set_string)

IO.inspect(set_cube_map)
cube_map_keys = Map.keys(cube_map)

accumulated_list = []
# Note: this is how we can track the valid/invalid cube numbers in the future,
# we don't have to for now, but we might need to in the future.
# validity_list = Enum.reduce(cube_map_keys, accumulated_list, fn cube_colour, accumulated_list ->
# [{cube_colour, set_cube_map[cube_colour] > cube_map[cube_colour]} | accumulated_list]
# end)

# Thus, for now, I am just going to return a boolean list, if any of them are false, I'm just going to
# say that the set is invalid.

validity_list =
Enum.reduce(cube_map_keys, accumulated_list, fn cube_colour, accumulated_list ->
IO.inspect(cube_colour)
[set_cube_map[cube_colour] >= cube_map[cube_colour] | accumulated_list]
end)

IO.inspect(validity_list)

Enum.all?(validity_list)
end
end
28 changes: 14 additions & 14 deletions test/day2/day2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,24 @@ defmodule Day2Test do
test "it can validate a valid game set by the number of cubes and their colurs" do
thirty_cube_set_str = "7 red, 15 blue, 8 green"

cube_map = %{
:red => 12,
:green => 13,
:blue => 14
max_cubes = %{
:red => 7,
:blue => 15,
:green => 8
}

assert Day2.validate_set_by_colour_and_amount(game_set_string, cube_map) == true
assert Day2.validate_set_by_colour_and_amount(thirty_cube_set_str, max_cubes) == true
end

test "it can validate an invalid game set by the number of cubes and their colurs" do
thirty_cube_set_str = "12 red, 12 blue, 15 green"
# test "it can validate an invalid game set by the number of cubes and their colurs" do
# game_set_string = "12 red, 12 blue, 15 green"

cube_map = %{
:red => 7,
:green => 13,
:blue => 14
}
# cube_map = %{
# :red => 7,
# :green => 13,
# :blue => 14
# }

assert Day2.validate_set_by_colour_and_amount(game_set_string, cube_map) == false
end
# assert Day2.validate_set_by_colour_and_amount(game_set_string, cube_map) == false
# end
end

0 comments on commit ba10ddf

Please sign in to comment.