Skip to content

Instantly share code, notes, and snippets.

@manewitz
Created December 7, 2023 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manewitz/397a45df8181492053dd26358fe1730d to your computer and use it in GitHub Desktop.
Save manewitz/397a45df8181492053dd26358fe1730d to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Day 2 - Elixir
defmodule Cube.RegEx do
@regex_game_number ~r/Game (\d+)/
@regex_color_thresholds ~r/(1[3-9]|[2-9][0-9]) red|(1[4-9]|[2-9][0-9]) green|(1[5-9]|[2-9][0-9]) blue/
@spec count(binary()) :: any()
def count(filename) do
File.read!(Path.expand(filename))
|> String.split("\n", trim: true)
|> Enum.reject(&match_color_thresholds?/1)
|> Enum.reduce(0, &sum_game_numbers/2)
end
defp match_color_thresholds?(game) do
Regex.match?(@regex_color_thresholds, game)
end
defp sum_game_numbers(game, acc) do
[game_number] = Regex.run(@regex_game_number, game, capture: :all_but_first)
acc + String.to_integer(game_number)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment