chore: fix default file for day3 + formatting

This commit is contained in:
Ruidy 2024-12-04 13:02:38 +01:00
parent e778f56828
commit a5dccc6323
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -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
@ -20,9 +21,9 @@ defmodule AdventCode2024.Solutions.Day03 do
{:ok, content} -> solve_content(content)
{:error, reason} -> {:error, reason}
end
# Treat as direct content
true ->
true ->
solve_content(input)
end
end
@ -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
@ -40,9 +42,9 @@ defmodule AdventCode2024.Solutions.Day03 do
{:ok, content} -> solve_part2_content(content)
{:error, reason} -> {:error, reason}
end
# Treat as direct content
true ->
true ->
solve_part2_content(input)
end
end
@ -50,11 +52,12 @@ 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})\)/
|> Regex.scan(content, capture: :all_but_first)
|> Enum.map(fn [x, y] ->
|> Enum.map(fn [x, y] ->
with {x_int, ""} <- Integer.parse(x),
{y_int, ""} <- Integer.parse(y) do
x_int * y_int
@ -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,9 +79,10 @@ 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)
|> List.flatten()
|> Enum.filter(&(&1 in ["do()", "don't()"] or String.starts_with?(&1, "mul(")))
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(")))
# Process tokens in order, tracking multiplication state
tokens
@ -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)
|> Enum.map(&String.to_integer/1)
[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