From e83d6906e4edffd4f8833de529bb93e594c555fd Mon Sep 17 00:00:00 2001 From: bucsi <11545252+bucsi@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:00:46 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20ability=20to=20use=20example?= =?UTF-8?q?=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gladvent.gleam | 1 + src/gladvent/internal/cmd/new.gleam | 16 +++++++++++----- src/gladvent/internal/cmd/run.gleam | 29 ++++++++++++++++++++++++----- src/gladvent/internal/input.gleam | 16 ++++++++++++++++ 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/gladvent/internal/input.gleam diff --git a/src/gladvent.gleam b/src/gladvent.gleam index 6c8b83c..5dc81e6 100644 --- a/src/gladvent.gleam +++ b/src/gladvent.gleam @@ -24,6 +24,7 @@ pub fn main() { |> glint.add(at: ["new"], do: new.new_command()) |> glint.group_flag(at: ["run"], of: run.timeout_flag()) |> glint.group_flag(at: ["run"], of: run.allow_crash_flag()) + |> glint.group_flag(at: ["run"], of: run.use_example_flag()) |> glint.add(at: ["run"], do: run.run_command()) |> glint.add(at: ["run", "all"], do: run.run_all_command()) diff --git a/src/gladvent/internal/cmd/new.gleam b/src/gladvent/internal/cmd/new.gleam index 930e07b..c7bca23 100644 --- a/src/gladvent/internal/cmd/new.gleam +++ b/src/gladvent/internal/cmd/new.gleam @@ -3,6 +3,7 @@ import file_streams/file_stream import file_streams/file_stream_error import filepath import gladvent/internal/cmd +import gladvent/internal/input import gladvent/internal/parse.{type Day} import gladvent/internal/util import gleam/int @@ -50,7 +51,15 @@ fn create_input_dir(ctx: Context) { } fn create_input_file(ctx: Context) { - let input_path = input_path(ctx.year, ctx.day) + let input_path = input.get_file_path(ctx.year, ctx.day, input.Puzzle) + + do_exclusive(input_path, fn(_) { Nil }) + |> result.map_error(handle_file_open_failure(_, input_path)) + |> result.replace(input_path) +} + +fn create_input_example_file(ctx: Context) { + let input_path = input.get_file_path(ctx.year, ctx.day, input.Example) do_exclusive(input_path, fn(_) { Nil }) |> result.map_error(handle_file_open_failure(_, input_path)) @@ -71,10 +80,6 @@ fn err_to_string(e: Err) -> String { } } -fn input_path(year: Int, day: Day) -> String { - filepath.join(cmd.input_dir(year), int.to_string(day) <> ".txt") -} - fn gleam_src_path(year: Int, day: Day) -> String { filepath.join(cmd.src_dir(year), "day_" <> int.to_string(day) <> ".gleam") } @@ -113,6 +118,7 @@ fn do(ctx: Context) -> String { create_input_root, create_input_dir, create_input_file, + create_input_example_file, create_src_dir, create_src_file, ] diff --git a/src/gladvent/internal/cmd/run.gleam b/src/gladvent/internal/cmd/run.gleam index 5eb0cc8..ff144e3 100644 --- a/src/gladvent/internal/cmd/run.gleam +++ b/src/gladvent/internal/cmd/run.gleam @@ -1,6 +1,6 @@ import decode -import filepath import gladvent/internal/cmd.{Ending, Endless} +import gladvent/internal/input import gladvent/internal/parse.{type Day} import gladvent/internal/runners import gladvent/internal/util @@ -73,14 +73,14 @@ fn do( day: Day, package: package_interface.Package, allow_crash: Bool, + input_kind: input.Kind, ) -> RunResult { use #(pt_1, pt_2, parse) <- result.try( runners.get_day(package, year, day) |> result.map_error(FailedToGetRunner), ) - let input_path = - filepath.join(cmd.input_dir(year), int.to_string(day) <> ".txt") + let input_path = input.get_file_path(year, day, input_kind) use input <- result.try( input_path @@ -268,6 +268,12 @@ pub fn allow_crash_flag() { |> glint.flag_help("Don't catch exceptions thrown by runners") } +pub fn use_example_flag() { + glint.bool_flag("use-example") + |> glint.flag_default(False) + |> glint.flag_help("Use the example as input instead of your puzzle input") +} + pub fn run_command() -> glint.Command(Result(List(String))) { use <- glint.command_help("Run the specified days") use <- glint.unnamed_args(glint.MinArgs(1)) @@ -275,6 +281,11 @@ pub fn run_command() -> glint.Command(Result(List(String))) { use days <- result.then(parse.days(args)) let assert Ok(year) = glint.get_flag(flags, cmd.year_flag()) let assert Ok(allow_crash) = glint.get_flag(flags, allow_crash_flag()) + let assert Ok(use_example) = case glint.get_flag(flags, use_example_flag()) { + Ok(True) -> Ok(input.Puzzle) + Ok(False) -> Ok(input.Example) + Error(a) -> Error(a) + } let spinner = spinner.new( @@ -298,7 +309,11 @@ pub fn run_command() -> glint.Command(Result(List(String))) { ) days - |> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _)) + |> cmd.exec( + timing, + do(year, _, package, allow_crash, use_example), + collect_async(year, _), + ) } pub fn run_all_command() -> glint.Command(Result(List(String))) { @@ -337,5 +352,9 @@ pub fn run_all_command() -> glint.Command(Result(List(String))) { |> result.replace_error(Nil) }) |> list.sort(int.compare) - |> cmd.exec(timing, do(year, _, package, allow_crash), collect_async(year, _)) + |> cmd.exec( + timing, + do(year, _, package, allow_crash, input.Puzzle), + collect_async(year, _), + ) } diff --git a/src/gladvent/internal/input.gleam b/src/gladvent/internal/input.gleam new file mode 100644 index 0000000..f91a8a8 --- /dev/null +++ b/src/gladvent/internal/input.gleam @@ -0,0 +1,16 @@ +import filepath +import gladvent/internal/cmd +import gleam/int + +pub type Kind { + Example + Puzzle +} + +pub fn get_file_path(year: Int, day: Int, input_kind: Kind) -> String { + filepath.join(cmd.input_dir(year), int.to_string(day)) + <> case input_kind { + Example -> ".example.txt" + Puzzle -> ".txt" + } +}