From f770a143ebd58a8ab082e5e58532c5c7b75c8617 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] feat: fetch input if not exists when `solve` --- 2021/lib/mix/tasks/advent_of_code.solve.ex | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/2021/lib/mix/tasks/advent_of_code.solve.ex b/2021/lib/mix/tasks/advent_of_code.solve.ex index 5c506c5..01026a5 100644 --- a/2021/lib/mix/tasks/advent_of_code.solve.ex +++ b/2021/lib/mix/tasks/advent_of_code.solve.ex @@ -1,5 +1,6 @@ defmodule Mix.Tasks.AdventOfCode.Solve do use Mix.Task + import Mix.AdventOfCode @shortdoc "Runs solution code with problem input" @@ -60,14 +61,31 @@ defmodule Mix.Tasks.AdventOfCode.Solve do end 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 - {:indeterminate, IO.stream()} - else - file = Path.join("priv/inputs", "#{input}.input") - stream = File.stream!(file) - {Enum.count(stream), stream} + {:ok, name} -> + Path.join("priv/inputs", "#{name}.input") + |> get_size_and_stream() + + :error -> + [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 + + def get_size_and_stream(file) do + stream = File.stream!(file) + {Enum.count(stream), stream} + end end