advent_of_code/elixir/lib/Y2023/2023_day2_2.exs
2024-11-15 21:03:49 -05:00

24 lines
690 B
Elixir

max = %{"red" => 12, "green" => 13, "blue" => 14}
is_valid_pull = fn {count, color} ->
count <= max[color]
end
for {id, pulls} <-
(for line <- IO.stream(),
String.trim(line) != "",
"Game " <> game = line,
{id, ": " <> rest} = Integer.parse(game) do
{id,
for pull <- String.split(rest, "; ") |> Enum.map(&String.trim/1),
cube <- String.split(pull, ", ") |> Enum.map(&String.trim/1),
{count, color} = Integer.parse(cube),
color = String.trim(color) do
{count, color}
end}
end),
Enum.all?(pulls, &is_valid_pull.(&1)) do
id
end
|> Enum.sum()
|> IO.puts()