Advent of Code 2024 Day 3 Part 2
Mix.install([
{:kino_aoc, "~> 0.1"}
])
Get Inputs
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "3", System.fetch_env!("LB_SESSION"))
My answer
Regex.scan(~r/mul\(\d+,\d+\)|do[n't]*\(\)/, puzzle_input)
Regex.scan(~r/mul\(\d+,\d+\)|do[n't]*\(\)/, puzzle_input)
|> Enum.reduce({true, 0}, fn [instruction], {do_flag, acc} ->
case {do_flag, instruction} do
{_, "do()"} -> {true, acc}
{_, "don't()"} -> {false, acc}
{true, mul} ->
product =
Regex.scan(~r/\d+/, mul)
|> Enum.map(fn [str] ->
String.to_integer(str)
end)
|> Enum.product()
{true, acc + product}
_ ->
{do_flag, acc}
end
end)
|> elem(1)