Solve day 3
This commit is contained in:
+20
-7
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user