Slightly cleaner without capture: :all_but_first for part 2

This commit is contained in:
Adam Millerchip 2024-12-03 16:50:47 +09:00
parent 758e5e481e
commit 06ca6eba41

View file

@ -7,12 +7,12 @@ defmodule Day3 do
end end
def part2(input) do def part2(input) do
Regex.scan(~r/mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))/, input, capture: :all_but_first) Regex.scan(~r/mul\((\d+),(\d+)\)|do\(\)|don't\(\)/, input)
|> Enum.reduce({0, :process}, fn |> Enum.reduce({0, :process}, fn
["", "", "do()"], {sum, _} -> {sum, :process} ["do()"], {sum, _} -> {sum, :process}
["", "", "", "don't()"], {sum, _} -> {sum, :skip} ["don't()"], {sum, _} -> {sum, :skip}
_, {sum, :skip} -> {sum, :skip} _, {sum, :skip} -> {sum, :skip}
[a, b], {sum, :process} -> {sum + String.to_integer(a) * String.to_integer(b), :process} [_, a, b], {sum, :process} -> {sum + String.to_integer(a) * String.to_integer(b), :process}
end) end)
|> elem(0) |> elem(0)
end end