mirror of
https://github.com/rjNemo/ai_advent_code_2024
synced 2026-06-06 02:26:44 +00:00
chore: fix default file for day3 + formatting
This commit is contained in:
parent
34a89c44f7
commit
c2e2b4c31c
1 changed files with 21 additions and 11 deletions
|
|
@ -5,13 +5,14 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
|
||||
@behaviour AdventCode2024.Solution
|
||||
|
||||
@default_input "lib/advent_code2024/solutions/day03/input.txt"
|
||||
@default_input "priv/inputs/day03/input.txt"
|
||||
|
||||
@doc """
|
||||
Solve part 1 of the challenge
|
||||
"""
|
||||
def solve(input \\ @default_input)
|
||||
def solve(""), do: {:error, :no_input}
|
||||
|
||||
def solve(input) when is_binary(input) and input != "" do
|
||||
cond do
|
||||
# Check if it looks like a file path and try to read it
|
||||
|
|
@ -32,6 +33,7 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
"""
|
||||
def solve_part2(input \\ @default_input)
|
||||
def solve_part2(""), do: {:error, :no_input}
|
||||
|
||||
def solve_part2(input) when is_binary(input) and input != "" do
|
||||
cond do
|
||||
# Check if it looks like a file path and try to read it
|
||||
|
|
@ -50,6 +52,7 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
# Private functions
|
||||
|
||||
defp solve_content(""), do: {:error, :no_input}
|
||||
|
||||
defp solve_content(content) when is_binary(content) and content != "" do
|
||||
result =
|
||||
~r/mul\((\d{1,3}),(\d{1,3})\)/
|
||||
|
|
@ -68,6 +71,7 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
end
|
||||
|
||||
defp solve_part2_content(""), do: {:error, :no_input}
|
||||
|
||||
defp solve_part2_content(content) when is_binary(content) and content != "" do
|
||||
{result, _} = process_multiplications(content)
|
||||
{:ok, result}
|
||||
|
|
@ -75,7 +79,8 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
|
||||
defp process_multiplications(content) do
|
||||
# Split content into tokens that match either multiplication or control instructions
|
||||
tokens = Regex.scan(~r/(?:mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\))|(?:do|don't)\(\)/, content)
|
||||
tokens =
|
||||
Regex.scan(~r/(?:mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\))|(?:do|don't)\(\)/, content)
|
||||
|> List.flatten()
|
||||
|> Enum.filter(&(&1 in ["do()", "don't()"] or String.starts_with?(&1, "mul(")))
|
||||
|
||||
|
|
@ -85,12 +90,17 @@ defmodule AdventCode2024.Solutions.Day03 do
|
|||
cond do
|
||||
token == "do()" ->
|
||||
{sum, true}
|
||||
|
||||
token == "don't()" ->
|
||||
{sum, false}
|
||||
|
||||
String.starts_with?(token, "mul") && multiply_enabled ->
|
||||
[n1, n2] = Regex.run(~r/mul\((\d{1,3}),(\d{1,3})\)/, token, capture: :all_but_first)
|
||||
[n1, n2] =
|
||||
Regex.run(~r/mul\((\d{1,3}),(\d{1,3})\)/, token, capture: :all_but_first)
|
||||
|> Enum.map(&String.to_integer/1)
|
||||
|
||||
{sum + n1 * n2, multiply_enabled}
|
||||
|
||||
true ->
|
||||
{sum, multiply_enabled}
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue