Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Feat: add ability to use example files #12

Merged
merged 12 commits into from
Jul 22, 2024
Merged
8 changes: 0 additions & 8 deletions src/gladvent/internal/cmd.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ fn find_root(path: String) -> String {
}
}

pub fn input_root() {
filepath.join(root(), "input")
}

pub fn input_dir(year) {
filepath.join(input_root(), int.to_string(year))
}

pub fn src_root() {
filepath.join(root(), "src")
}
Expand Down
40 changes: 29 additions & 11 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 All @@ -14,7 +15,7 @@ import glint
import simplifile

type Context {
Context(year: Int, day: Day, add_parse: Bool)
Context(year: Int, day: Day, add_parse: Bool, create_example_file: Bool)
}

fn create_src_dir(ctx: Context) {
Expand All @@ -39,18 +40,26 @@ fn create_src_file(ctx: Context) {
}

fn create_input_root(_ctx: Context) {
cmd.input_root()
input.root()
|> create_dir
}

fn create_input_dir(ctx: Context) {
ctx.year
|> cmd.input_dir
|> input.dir
|> create_dir
}

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 @@ -115,6 +120,10 @@ fn do(ctx: Context) -> String {
create_input_file,
create_src_dir,
create_src_file,
..case ctx.create_example_file {
True -> [create_input_example_file]
False -> []
}
]

let successes = fn(good) {
Expand Down Expand Up @@ -185,19 +194,28 @@ fn collect(year: Int, x: #(Day, String)) -> String {
pub fn new_command() {
use <- glint.command_help("Create .gleam and input files")
use <- glint.unnamed_args(glint.MinArgs(1))
use parse <- glint.flag(
use parse_flag <- glint.flag(
glint.bool_flag("parse")
|> glint.flag_default(False)
|> glint.flag_help("Generate day runners with a parse function"),
)
use example_flag <- glint.flag(
glint.bool_flag("example")
|> glint.flag_default(False)
|> glint.flag_help(
"Generate example input files to run your solution against",
),
)
use _, args, flags <- glint.command()
use days <- result.map(parse.days(args))
let assert Ok(year) = glint.get_flag(flags, cmd.year_flag())
let assert Ok(parse) = parse(flags)
let assert Ok(parse) = parse_flag(flags)
let assert Ok(create_example) = example_flag(flags)

cmd.exec(
days,
cmd.Endless,
fn(day) { do(Context(year, day, parse)) },
fn(day) { do(Context(year, day, parse, create_example)) },
collect_async(year, _),
)
}
Expand Down
30 changes: 25 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 @@ -271,10 +271,22 @@ pub fn allow_crash_flag() {
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 example_flag <- glint.flag(
glint.bool_flag("example")
|> glint.flag_default(False)
|> glint.flag_help(
"Run solutions against example inputs (found at input/<year>/<day>.example.txt)",
),
)
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 example_flag(flags) {
Error(a) -> Error(a)
Ok(True) -> Ok(input.Example)
_ -> Ok(input.Puzzle)
}

let spinner =
spinner.new(
Expand All @@ -298,7 +310,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))) {
TanklesXL marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -337,5 +353,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),
TanklesXL marked this conversation as resolved.
Show resolved Hide resolved
collect_async(year, _),
)
}
24 changes: 24 additions & 0 deletions src/gladvent/internal/input.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import filepath
import gladvent/internal/cmd
import gleam/int

pub type Kind {
bucsi marked this conversation as resolved.
Show resolved Hide resolved
Example
Puzzle
}

pub fn get_file_path(year: Int, day: Int, input_kind: Kind) -> String {
filepath.join(dir(year), int.to_string(day))
<> case input_kind {
Example -> ".example.txt"
Puzzle -> ".txt"
}
}

pub fn root() {
filepath.join(cmd.root(), "input")
}

pub fn dir(year) {
filepath.join(root(), int.to_string(year))
}
Loading