Solve day 3

This commit is contained in:
Aurélien Geron
2024-12-07 16:32:10 +13:00
parent 788e01b7bb
commit 005fe680cf
2 changed files with 40 additions and 7 deletions
+20 -7
View File
@@ -1,13 +1,26 @@
defmodule Aoc2024.Day03 do
def parse(input) do
input |> String.split("\n", trim: true)
def part1(instructions) do
~r/mul\(([0-9]+),([0-9]+)\)/
|> Regex.scan(instructions)
|> Enum.map(fn [_all, num1_str, num2_str] ->
case {Integer.parse(num1_str), Integer.parse(num2_str)} do
{{num1, ""}, {num2, ""}} -> num1 * num2
_ -> 0
end
end)
|> Enum.sum()
end
def part1(_lines) do
:todo1
end
def part2(instructions) do
conditional_instructions =
~r/don't\(\).*?do\(\)/s
|> Regex.replace(instructions, "|")
def part2(_lines) do
:todo2
# handle possible case where the instructions end with don't() and more mul
conditional_instructions2 =
~r/don't\(\).*$/s
|> Regex.replace(conditional_instructions, "|")
part1(conditional_instructions2)
end
end