add 2024 day 3
This commit is contained in:
parent
cf454dfc22
commit
9cf965cf09
1 changed files with 33 additions and 0 deletions
33
elixir/livebook/2024/day3.livemd
Normal file
33
elixir/livebook/2024/day3.livemd
Normal 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
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue