— Day 3: Mull It Over —
Mix.install([{:kino_aoc, "~> 0.1"}])
Setup
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "3", System.fetch_env!("LB_AOC_SESSION_COOKIE"))
defmodule MullItOver do
def mul_pattern(), do: ~r/mul\(\d+,\d+\)/
def mul_do_dont_pattern(), do: ~r/mul\(\d+,\d+\)|do\(\)|don't\(\)/
def parse(input, pattern) do
pattern
|> Regex.scan(input)
|> List.flatten()
end
def parse_product(command) do
~r/\d+/
|> Regex.scan(command)
|> List.flatten()
|> Enum.map(&String.to_integer/1)
|> Enum.product()
end
def maybe_mull("don't()", {_, acc}), do: {:dont, acc}
def maybe_mull("do()", {_, acc}), do: {:do, acc}
def maybe_mull("mul" <> nums, {:do, acc}), do: {:do, parse_product(nums) + acc}
def maybe_mull("mul" <> _nums, {:dont, acc}), do: {:dont, acc}
end
Part 1
import MullItOver
puzzle_input
|> parse(mul_pattern())
|> Enum.map(&parse_product/1)
|> Enum.sum()
Part 2
import MullItOver
puzzle_input
|> parse(mul_do_dont_pattern())
|> Enum.reduce({:do, 0}, &maybe_mull/2)
|> elem(1)