advent_of_code/elixir/livebook/2024/day3.livemd
2024-12-03 09:26:30 -05:00

653 B

AOC 2024 Day 3

Section

input = File.read!("/data/input.txt")
Regex.scan(
  ~r/mul\((\d+),(\d+)\)/,
  input
)
|> Stream.map(fn [_, a, b] -> String.to_integer(a) * String.to_integer(b) end)
|> Enum.sum()
for instruction <- Regex.scan(
  ~r/(mul\((\d+),(\d+)\)|don't\(\)|do\(\))/,
  input
), reduce: {true, 0} do
  {enabled?, acc} ->
    (fn
      false, ["mul" <> _ | _] -> {false, acc}
      true, ["mul" <> _, _, a, b] -> {true, acc + String.to_integer(a) * String.to_integer(b)}
      _, ["don" <> _ | _] -> {false, acc}
      _, ["do()" | _] -> {true, acc}
    end
    ).(enabled?, instruction)
end