Skip to content

Instantly share code, notes, and snippets.

@varshneydevansh
Created February 11, 2024 16:11
Show Gist options
  • Save varshneydevansh/73d91bcf931cf1cf11657402aa3d445e to your computer and use it in GitHub Desktop.
Save varshneydevansh/73d91bcf931cf1cf11657402aa3d445e to your computer and use it in GitHub Desktop.
Advent of Code day 2 2023 some problem
defmodule Day2 do
def run do
file_path = Path.expand("./day2_input.txt")
total_red = 12
total_green = 13
total_blue = 14
total_valid_game_ids =
File.stream!(file_path)
|> Enum.reduce([], fn line, acc ->
[game_number_str, colors_str] =
case Regex.run(~r/Game (\d+): (.+)/, line) do
[_, game_number, colors] -> [game_number, colors]
_ -> nil
end
case {String.to_integer(game_number_str), colors_str} do
{game_number, colors_str} ->
IO.inspect({game_number, colors_str}) # Debug print
color_list = colors_str
|> String.split("; ")
|> Enum.map(fn color_str ->
color_str
|> String.split(", ")
|> Enum.map(fn count_color ->
[nos_cube_str, color] = String.split(count_color, " ")
nos_cube = String.to_integer(nos_cube_str)
{nos_cube, color}
end)
end)
IO.inspect("Color List: \n\n")
IO.inspect(color_list) # Debug print
if Enum.all?(color_list, fn
{nos_cube, "red"} -> nos_cube <= total_red;
_ -> true end) &&
Enum.all?(color_list, fn
{nos_cube, "green"} -> nos_cube <= total_green;
_ -> true end) &&
Enum.all?(color_list, fn
{nos_cube, "blue"} -> nos_cube <= total_blue;
_ -> true end)
do
IO.puts("Game Number: #{game_number}\n\n\n") # Debug print
[game_number | acc]
else
acc
end
_ ->
IO.puts("Invalid line format: #{line}")
acc
end
end)
IO.puts("Total Valid Game IDs: #{total_valid_game_ids}") # Debug print
sum_of_valid_game_ids = Enum.sum(total_valid_game_ids)
IO.puts("Sum of Valid Game IDs: #{sum_of_valid_game_ids}")
end
end
Day2.run()
[~/advent-of-code]
devansh   master  elixir day2.exs
warning: this clause cannot match because a previous clause at line 19 always matches
day2.exs:51
{1, "3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green"}
"Color List: \n\n"
[
[{3, "blue"}, {4, "red"}],
[{1, "red"}, {2, "green"}, {6, "blue"}],
[{2, "green"}]
]
Game Number: 1
{2, "1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue"}
"Color List: \n\n"
[
[{1, "blue"}, {2, "green"}],
[{3, "green"}, {4, "blue"}, {1, "red"}],
[{1, "green"}, {1, "blue"}]
]
Game Number: 2
{3, "8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red"}
"Color List: \n\n"
[
[{8, "green"}, {6, "blue"}, {20, "red"}],
[{5, "blue"}, {4, "red"}, {13, "green"}],
[{5, "green"}, {1, "red"}]
]
Game Number: 3
{4, "1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red"}
"Color List: \n\n"
[
[{1, "green"}, {3, "red"}, {6, "blue"}],
[{3, "green"}, {6, "red"}],
[{3, "green"}, {15, "blue"}, {14, "red"}]
]
Game Number: 4
{5, "6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"}
"Color List: \n\n"
[
[{6, "red"}, {1, "blue"}, {3, "green"}],
[{2, "blue"}, {1, "red"}, {2, "green"}]
]
Game Number: 5
Total Valid Game IDs:
Sum of Valid Game IDs: 15
@varshneydevansh
Copy link
Author

This is working -

defmodule Day2 do
  def run do
    file_path = Path.expand("./day2_input.txt")

    total_red   = 12
    total_green = 13
    total_blue  = 14

    valid_game_ids =
      File.stream!(file_path)
      |> Enum.reduce([], fn line, acc ->
        [game_number_str, colors_str] =
          case Regex.run(~r/Game (\d+): (.+)/, line) do
            [_, game_number, colors] -> [game_number, colors]
            _ -> nil
          end

        case {String.to_integer(game_number_str), colors_str} do
          {game_number, colors_str} ->
            color_list = colors_str
                          |> String.split("; ")
                          |> Enum.map(fn color_str ->
                            color_str
                            |> String.split(", ")
                            |> Enum.all?(fn count_color ->
                              [nos_cube_str, color] = String.split(count_color, " ")
                              nos_cube = String.to_integer(nos_cube_str)
                              case color do
                                "red"   -> nos_cube <= total_red
                                "green" -> nos_cube <= total_green
                                "blue"  -> nos_cube <= total_blue
                              end
                            end)
                          end)

            if Enum.all?(color_list, &(&1)) do
              IO.puts("Game Number: #{game_number}") # Debug print
              [game_number | acc]
            else
              acc
            end
          _ ->
            IO.puts("Invalid line format: #{line}")
            acc
        end
      end)

    sum_of_valid_game_ids = Enum.sum(valid_game_ids)

    IO.puts("Sum of Valid Game IDs: #{sum_of_valid_game_ids}")
  end
end

Day2.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment