fix: Improve file input handling in Day03 solution

This commit is contained in:
Ruidy (aider) 2024-12-03 14:05:22 +01:00
parent 70b2cf2086
commit ea60e261ff
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -13,15 +13,19 @@ defmodule AdventCode2024.Solutions.Day03 do
def solve(input \\ @default_input)
def solve(""), do: {:error, :no_input}
def solve(input) when is_binary(input) and input != "" do
if String.contains?(input, "\n") or !String.contains?(input, "/") do
# Input is content
solve_content(input)
else
# Input is file path
case File.read(input) do
{:ok, content} -> solve_content(content)
{:error, reason} -> {:error, reason}
end
cond do
String.contains?(input, "\n") ->
# Input is multiline content
solve_content(input)
String.contains?(input, "/") ->
# Input is file path
case File.read(input) do
{:ok, content} -> solve_content(content)
{:error, reason} -> {:error, reason}
end
true ->
# Input is single line content
solve_content(input)
end
end