1
0
Fork 0

feat: fetch input if not exists when solve

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 42b6917614
commit f770a143eb

View file

@ -1,5 +1,6 @@
defmodule Mix.Tasks.AdventOfCode.Solve do defmodule Mix.Tasks.AdventOfCode.Solve do
use Mix.Task use Mix.Task
import Mix.AdventOfCode
@shortdoc "Runs solution code with problem input" @shortdoc "Runs solution code with problem input"
@ -60,14 +61,31 @@ defmodule Mix.Tasks.AdventOfCode.Solve do
end end
defp stream_for_input(selector, opts) do defp stream_for_input(selector, opts) do
input = Keyword.get(opts, :input, selector) case Keyword.fetch(opts, :input) do
{:ok, "-"} ->
{:indeterminate, IO.stream()}
if input == "-" do {:ok, name} ->
{:indeterminate, IO.stream()} Path.join("priv/inputs", "#{name}.input")
else |> get_size_and_stream()
file = Path.join("priv/inputs", "#{input}.input")
stream = File.stream!(file) :error ->
{Enum.count(stream), stream} [day | _] = String.split(selector, ".")
file = day_input_path(day)
unless File.exists?(file) or
not Mix.shell().yes?("Input missing. Fetch input for day #{day}?") do
Mix.Task.run("advent_of_code.fetch_input", [day])
end
day_input_path(day)
|> get_size_and_stream()
end end
end end
def get_size_and_stream(file) do
stream = File.stream!(file)
{Enum.count(stream), stream}
end
end end