Skip to content

Commit

Permalink
✨ add ability to use example files
Browse files Browse the repository at this point in the history
  • Loading branch information
bucsi committed Jul 9, 2024
1 parent 201de6f commit e83d690
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/gladvent.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
16 changes: 11 additions & 5 deletions src/gladvent/internal/cmd/new.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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")
}
Expand Down Expand Up @@ -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,
]
Expand Down
29 changes: 24 additions & 5 deletions src/gladvent/internal/cmd/run.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -268,13 +268,24 @@ 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))
use _, args, flags <- glint.command()
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(
Expand All @@ -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))) {
Expand Down Expand Up @@ -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, _),
)
}
16 changes: 16 additions & 0 deletions src/gladvent/internal/input.gleam
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit e83d690

Please sign in to comment.