From d200c5b512a0598584ba546515e423660ac8c976 Mon Sep 17 00:00:00 2001 From: "Ruidy (aider)" Date: Mon, 2 Dec 2024 08:11:36 +0100 Subject: [PATCH] feat: Add default input handling and file reading for Day 2 solution fix: Resolve compilation errors in Day2 solution --- lib/advent_code2024/solutions/day02/day2.ex | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/advent_code2024/solutions/day02/day2.ex b/lib/advent_code2024/solutions/day02/day2.ex index 8de64f1..d4aff3c 100644 --- a/lib/advent_code2024/solutions/day02/day2.ex +++ b/lib/advent_code2024/solutions/day02/day2.ex @@ -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)