Advent of Code 2024 - Day 3
Mix.install([
{:kino_aoc, "~> 0.1"},
])
Section
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "3", System.fetch_env!("LB_AOC_SESSON_2024"))
defmodule Day03 do
def part1(input) do
~r/(mul)\((\d{1,3}),(\d{1,3})\)/
|> Regex.scan(input, capture: :all_but_first)
|> Enum.map(fn [_, a, b] ->
String.to_integer(a) * String.to_integer(b)
end)
|> Enum.sum
end
def part2(input) do
~r/(mul)\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)/
|> Regex.scan(input)
|> Enum.reduce({0, true}, fn
["do()"], {acc, _} -> {acc, true}
["don't()"], {acc, _} -> {acc, false}
[_, "mul", one, two], {acc, do?} ->
if do? do
{acc + String.to_integer(one) * String.to_integer(two), true}
else
{acc, false}
end
end)
end
end
Day03.part1(puzzle_input)
Day03.part2(puzzle_input)