mirror of
https://github.com/rjNemo/ai_advent_code_2024
synced 2026-06-12 13:36:39 +00:00
feat: Add default input handling and file reading for Day 2 solution
fix: Resolve compilation errors in Day2 solution
This commit is contained in:
parent
b6e64a1b37
commit
d200c5b512
1 changed files with 13 additions and 3 deletions
|
|
@ -3,12 +3,24 @@ defmodule AdventCode2024.Solutions.Day2 do
|
||||||
Solution for Day 2: Red-Nosed Reports
|
Solution for Day 2: Red-Nosed Reports
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@default_input "priv/inputs/day02/input.txt"
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Analyzes reactor reports to count how many are safe according to the rules.
|
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.
|
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
|
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
|
with {:ok, reports} <- parse_reports(lines) do
|
||||||
safe_count = Enum.count(reports, &safe_report?/1)
|
safe_count = Enum.count(reports, &safe_report?/1)
|
||||||
|
|
@ -16,8 +28,6 @@ defmodule AdventCode2024.Solutions.Day2 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def solve(""), do: {:error, :no_valid_reports}
|
|
||||||
|
|
||||||
defp parse_reports(lines) do
|
defp parse_reports(lines) do
|
||||||
reports = Enum.map(lines, &parse_line/1)
|
reports = Enum.map(lines, &parse_line/1)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue