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