Advent of Code 2024 - Day 03
Mix.install([
{:req, "~> 0.5"},
{:nimble_parsec, "~> 1.4"}
])
Input
opts = [headers: [{"cookie", "session=#{System.fetch_env!("LB_AOC_SESSION")}"}]]
puzzle_input = Req.get!("https://adventofcode.com/2024/day/3/input", opts).body
Parser
defmodule Parser do
import NimbleParsec
mul =
ignore(string("mul("))
|> integer(min: 1, max: 3)
|> ignore(string(","))
|> integer(min: 1, max: 3)
|> ignore(string(")"))
|> wrap()
defparsec :parse_part1,
mul
|> eventually()
|> repeat()
defparsec :parse_part2,
choice([
mul,
string("do()") |> replace(:do),
string("don't()") |> replace(:dont)
])
|> eventually()
|> repeat()
end
Part 1
puzzle_input
|> Parser.parse_part1()
|> then(fn {:ok, result, _, _, _, _} -> result end)
|> Enum.reduce(0, fn [a, b], sum -> sum + a * b end)
Part 2
puzzle_input
|> Parser.parse_part2()
|> then(fn {:ok, result, _, _, _, _} -> result end)
|> Enum.reduce({:do, 0}, fn
:do, {_, sum} -> {:do, sum}
:dont, {_, sum} -> {:dont, sum}
[a, b], {:do, sum} -> {:do, sum + a * b}
[_a, _b], {:dont, _sum} = acc -> acc
end)
|> then(fn {_current, sum} -> sum end)