add 2024 day 3

This commit is contained in:
Caleb Webber 2024-12-03 09:26:30 -05:00
parent cf454dfc22
commit 9cf965cf09

View file

@ -0,0 +1,33 @@
# AOC 2024 Day 3
## Section
```elixir
input = File.read!("/data/input.txt")
```
```elixir
Regex.scan(
~r/mul\((\d+),(\d+)\)/,
input
)
|> Stream.map(fn [_, a, b] -> String.to_integer(a) * String.to_integer(b) end)
|> Enum.sum()
```
```elixir
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
```