feat: Add default input handling and file reading for Day 2 solution

fix: Resolve compilation errors in Day2 solution
This commit is contained in:
Ruidy (aider) 2024-12-02 08:11:36 +01:00 committed by Ruidy
parent b6e64a1b37
commit d200c5b512
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -3,12 +3,24 @@ defmodule AdventCode2024.Solutions.Day2 do
Solution for Day 2: Red-Nosed Reports
"""
@default_input "priv/inputs/day02/input.txt"
@doc """
Analyzes reactor reports to count how many are safe according to the rules.
Returns {:ok, count} for valid input or {:error, reason} for invalid input.
"""
def solve(input \\ @default_input)
def solve(""), do: {:error, :no_valid_reports}
def solve(input) when is_binary(input) and input != "" do
lines = String.split(input, "\n", trim: true)
case File.read(input) do
{:ok, content} -> solve_content(content)
{:error, reason} -> {:error, reason}
end
end
defp solve_content(""), do: {:error, :no_valid_reports}
defp solve_content(content) when is_binary(content) and content != "" do
lines = String.split(content, "\n", trim: true)
with {:ok, reports} <- parse_reports(lines) do
safe_count = Enum.count(reports, &safe_report?/1)
@ -16,8 +28,6 @@ defmodule AdventCode2024.Solutions.Day2 do
end
end
def solve(""), do: {:error, :no_valid_reports}
defp parse_reports(lines) do
reports = Enum.map(lines, &parse_line/1)