Created
December 7, 2023 19:11
-
-
Save manewitz/397a45df8181492053dd26358fe1730d to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Day 2 - Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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