From 8b6ec2b607f1c1c853a07ccc06d982e297d7e304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 3 Dec 2022 14:15:03 +0100 Subject: [PATCH 01/18] docs: add note about running subset of tests --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f30c9e6..a4a7ce4 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ _Total timing_ is computed from individual solution _timings_ and excludes as mu cargo test ``` +To run tests for a specific day, append `--bin `, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`. + ### Format code ```sh From b8b688f9d86e496ab15d508ca3c80c1c770f9a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sun, 4 Dec 2022 11:56:41 +0100 Subject: [PATCH 02/18] fix: respect `--release` when running `cargo all` --- src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 45701ab..95c690f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,12 @@ fn main() { .map(|day| { let day = format!("{:02}", day); - let cmd = Command::new("cargo") - .args(["run", "--release", "--bin", &day]) - .output() - .unwrap(); + let mut args = vec!["run", "--bin", &day]; + if cfg!(not(debug_assertions)) { + args.push("--release"); + } + + let cmd = Command::new("cargo").args(&args).output().unwrap(); println!("----------"); println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET); From 6379e82105aefefb44659bf285bb2ece70a2123c Mon Sep 17 00:00:00 2001 From: Burkhard Mittelbach Date: Sun, 4 Dec 2022 18:28:49 +0100 Subject: [PATCH 03/18] Add CI hook to check code formatting using `cargo fmt --check` (#18) --- .github/workflows/ci.yml | 8 ++++++++ README.md | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dadca12..ff9de26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,3 +28,11 @@ jobs: # - uses: actions/checkout@v2 # - name: cargo clippy # run: cargo clippy -- -D warnings + # uncoment to enable format checking + # fmt: + # runs-on: ubuntu-latest + # name: Format + # steps: + # - uses: actions/checkout@v3 + # - name: cargo fmt + # run: cargo fmt --check diff --git a/README.md b/README.md index a4a7ce4..cf1b392 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,10 @@ cargo clippy Once installed, you can use the [download command](#download-input-for-a-day). +### Check code formatting in CI + +Uncomment the `format` job in the `ci.yml` workflow to enable fmt checks in CI. + ### Enable clippy lints in CI Uncomment the `clippy` job in the `ci.yml` workflow to enable clippy checks in CI. From 6ae29a25e87681ac83f9e5890938a74226c5641c Mon Sep 17 00:00:00 2001 From: Burkhard Mittelbach Date: Sun, 4 Dec 2022 18:29:42 +0100 Subject: [PATCH 04/18] ci: update actions to use `actions/checkout@v3` (#17) --- .github/workflows/ci.yml | 6 +++--- .github/workflows/readme-stars.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff9de26..7cfbf87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,14 +10,14 @@ jobs: runs-on: ubuntu-latest name: Check steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: cargo check run: cargo check test: runs-on: ubuntu-latest name: Test steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: cargo test run: cargo test # uncomment to enable clippy lints @@ -25,7 +25,7 @@ jobs: # runs-on: ubuntu-latest # name: Lint (clippy) # steps: - # - uses: actions/checkout@v2 + # - uses: actions/checkout@v3 # - name: cargo clippy # run: cargo clippy -- -D warnings # uncoment to enable format checking diff --git a/.github/workflows/readme-stars.yml b/.github/workflows/readme-stars.yml index 026095e..4b943fa 100644 --- a/.github/workflows/readme-stars.yml +++ b/.github/workflows/readme-stars.yml @@ -10,7 +10,7 @@ jobs: update-readme: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 if: ${{ env.AOC_ENABLED }} env: AOC_ENABLED: ${{ secrets.AOC_ENABLED }} From 7ef01ab32f23bcc65538eb6fdb98c3849a5c8b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Mon, 5 Dec 2022 09:25:59 +0100 Subject: [PATCH 05/18] fix(editorconfig): don't trim trailing whitespace in txt files --- .editorconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/.editorconfig b/.editorconfig index c075e7e..560e94b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,6 +11,7 @@ trim_trailing_whitespace = true [*.txt] insert_final_newline = false +trim_trailing_whitespace = false [*.md] trim_trailing_whitespace = false From 4d10812fd3a19140c8fb435e866fb5914d6635f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Tue, 6 Dec 2022 18:22:40 +0100 Subject: [PATCH 06/18] feat: download puzzle descriptions (#21) * add support for downloading puzzle descriptions in `cargo download` * add `cargo read` command to read puzzles in terminal * extract `aoc_cli` module * use `aoc-cli`'s new overwrite option to eliminate temp files --- .cargo/config | 5 +- README.md | 35 ++++++++++---- src/bin/download.rs | 77 ++++--------------------------- src/bin/read.rs | 46 +++++++++++++++++++ src/lib.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+), 78 deletions(-) create mode 100644 src/bin/read.rs diff --git a/.cargo/config b/.cargo/config index 6731f8f..03ad300 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,6 +1,7 @@ [alias] -scaffold = "run --bin scaffold -- " -download = "run --bin download -- " +scaffold = "run --bin scaffold --quiet --release -- " +download = "run --bin download --quiet --release -- " +read = "run --bin read --quiet --release -- " solve = "run --bin" all = "run" diff --git a/README.md b/README.md index cf1b392..869f57d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/ When editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks. -### Download input for a day +### Download input & description for a day > **Note** > This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). @@ -60,18 +60,20 @@ When editing a solution, `rust-analyzer` will display buttons for running / debu cargo download # output: -# Downloading input with aoc-cli... -# Loaded session cookie from "/home/felix/.adventofcode.session". -# Downloading input for day 1, 2021... -# Saving puzzle input to "/tmp/tmp.MBdcAdL9Iw/input"... +# Loaded session cookie from "/Users//.adventofcode.session". +# Fetching puzzle for day 1, 2022... +# Saving puzzle description to "src/puzzles/01.md"... +# Downloading input for day 1, 2022... +# Saving puzzle input to "src/inputs/01.txt"... # Done! # --- -# πŸŽ„ Successfully wrote input to "src/inputs/01.txt"! +# πŸŽ„ Successfully wrote input to "src/inputs/01.txt". +# πŸŽ„ Successfully wrote puzzle to "src/puzzles/01.md". ``` To download inputs for previous years, append the `--year/-y` flag. _(example: `cargo download 1 --year 2020`)_ -Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3). +Puzzle descriptions are stored in `src/puzzles` as markdown files. Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3). ### Run solutions for a day @@ -139,11 +141,28 @@ cargo fmt cargo clippy ``` +### Read puzzle description in terminal + +> **Note** +> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). + +```sh +# example: `cargo read 1` +cargo read + +# output: +# Loaded session cookie from "/Users//.adventofcode.session". +# Fetching puzzle for day 1, 2022... +# ...the input... +``` + +To read inputs for previous years, append the `--year/-y` flag. _(example: `cargo read 1 --year 2020`)_ + ## Optional template features ### Download puzzle inputs via aoc-cli -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.5.0`. +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.7.0` 2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value. Once installed, you can use the [download command](#download-input-for-a-day). diff --git a/src/bin/download.rs b/src/bin/download.rs index edb567d..6632dc6 100644 --- a/src/bin/download.rs +++ b/src/bin/download.rs @@ -2,14 +2,12 @@ * This file contains template code. * There is no need to edit this file unless you want to change template functionality. */ -use std::io::Write; -use std::path::PathBuf; -use std::{env::temp_dir, io, process::Command}; -use std::{fs, process}; +use advent_of_code::aoc_cli; +use std::process; struct Args { day: u8, - year: Option, + year: Option, } fn parse_args() -> Result { @@ -20,86 +18,29 @@ fn parse_args() -> Result { }) } -fn remove_file(path: &PathBuf) { - #[allow(unused_must_use)] - { - fs::remove_file(path); - } -} - -fn exit_with_status(status: i32, path: &PathBuf) -> ! { - remove_file(path); - process::exit(status); -} - fn main() { - // acquire a temp file path to write aoc-cli output to. - // aoc-cli expects this file not to be present - delete just in case. - let mut tmp_file_path = temp_dir(); - tmp_file_path.push("aoc_input_tmp"); - remove_file(&tmp_file_path); - let args = match parse_args() { Ok(args) => args, Err(e) => { eprintln!("Failed to process arguments: {}", e); - exit_with_status(1, &tmp_file_path); + process::exit(1); } }; - let day_padded = format!("{:02}", args.day); - let input_path = format!("src/inputs/{}.txt", day_padded); - - // check if aoc binary exists and is callable. - if Command::new("aoc").arg("-V").output().is_err() { + if aoc_cli::check().is_err() { eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); - exit_with_status(1, &tmp_file_path); + process::exit(1); } - let mut cmd_args = vec![]; - - if let Some(year) = args.year { - cmd_args.push("--year".into()); - cmd_args.push(year.to_string()); - } - - cmd_args.append(&mut vec![ - "--input-file".into(), - tmp_file_path.to_string_lossy().to_string(), - "--day".into(), - args.day.to_string(), - "download".into(), - ]); - - println!("Downloading input with >aoc {}", cmd_args.join(" ")); - - match Command::new("aoc").args(cmd_args).output() { + match aoc_cli::download(args.day, args.year) { Ok(cmd_output) => { - io::stdout() - .write_all(&cmd_output.stdout) - .expect("could not write cmd stdout to pipe."); - io::stderr() - .write_all(&cmd_output.stderr) - .expect("could not write cmd stderr to pipe."); if !cmd_output.status.success() { - exit_with_status(1, &tmp_file_path); + process::exit(1); } } Err(e) => { eprintln!("failed to spawn aoc-cli: {}", e); - exit_with_status(1, &tmp_file_path); - } - } - - match fs::copy(&tmp_file_path, &input_path) { - Ok(_) => { - println!("---"); - println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); - exit_with_status(0, &tmp_file_path); - } - Err(e) => { - eprintln!("could not copy downloaded input to input file: {}", e); - exit_with_status(1, &tmp_file_path); + process::exit(1); } } } diff --git a/src/bin/read.rs b/src/bin/read.rs new file mode 100644 index 0000000..d0a4321 --- /dev/null +++ b/src/bin/read.rs @@ -0,0 +1,46 @@ +/* + * This file contains template code. + * There is no need to edit this file unless you want to change template functionality. + */ +use advent_of_code::aoc_cli; +use std::process; + +struct Args { + day: u8, + year: Option, +} + +fn parse_args() -> Result { + let mut args = pico_args::Arguments::from_env(); + Ok(Args { + day: args.free_from_str()?, + year: args.opt_value_from_str(["-y", "--year"])?, + }) +} + +fn main() { + let args = match parse_args() { + Ok(args) => args, + Err(e) => { + eprintln!("Failed to process arguments: {}", e); + process::exit(1); + } + }; + + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + match aoc_cli::read(args.day, args.year) { + Ok(cmd_output) => { + if !cmd_output.status.success() { + process::exit(1); + } + } + Err(e) => { + eprintln!("failed to spawn aoc-cli: {}", e); + process::exit(1); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 23acd28..09a7ae3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,3 +123,111 @@ mod tests { ); } } + +pub mod aoc_cli { + use std::{ + fmt::Display, + fs::create_dir_all, + process::{Command, Output, Stdio}, + }; + + pub enum AocCliError { + CommandNotFound, + CommandNotCallable, + BadExitStatus(Output), + IoError, + } + + impl Display for AocCliError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), + AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), + AocCliError::BadExitStatus(_) => write!(f, "aoc-cli exited with a non-zero status."), + AocCliError::IoError => write!(f, "could not write output files to file system."), + } + } + } + + pub fn check() -> Result<(), AocCliError> { + Command::new("aoc") + .arg("-V") + .output() + .map_err(|_| AocCliError::CommandNotFound)?; + Ok(()) + } + + pub fn read(day: u8, year: Option) -> Result { + // TODO: output local puzzle if present. + let args = build_args("read", &[], day, year); + call_aoc_cli(&args) + } + + pub fn download(day: u8, year: Option) -> Result { + let input_path = get_input_path(day); + + let puzzle_path = get_puzzle_path(day); + create_dir_all("src/puzzles").map_err(|_| AocCliError::IoError)?; + + let args = build_args( + "download", + &[ + "--overwrite".into(), + "--input-file".into(), + input_path.to_string(), + "--puzzle-file".into(), + puzzle_path.to_string(), + ], + day, + year, + ); + + let output = call_aoc_cli(&args)?; + + if output.status.success() { + println!("---"); + println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); + println!("πŸŽ„ Successfully wrote puzzle to \"{}\".", &puzzle_path); + Ok(output) + } else { + Err(AocCliError::BadExitStatus(output)) + } + + } + + fn get_input_path(day: u8) -> String { + let day_padded = format!("{:02}", day); + format!("src/inputs/{}.txt", day_padded) + } + + fn get_puzzle_path(day: u8) -> String { + let day_padded = format!("{:02}", day); + format!("src/puzzles/{}.md", day_padded) + } + + fn build_args(command: &str, args: &[String], day: u8, year: Option) -> Vec { + let mut cmd_args = args.to_vec(); + + if let Some(year) = year { + cmd_args.push("--year".into()); + cmd_args.push(year.to_string()); + } + + cmd_args.append(&mut vec!["--day".into(), day.to_string(), command.into()]); + + cmd_args + } + + fn call_aoc_cli(args: &[String]) -> Result { + if cfg!(debug_assertions) { + println!("Calling >aoc with: {}", args.join(" ")); + } + + Command::new("aoc") + .args(args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(|_| AocCliError::CommandNotCallable) + } +} From 0908ad602b4aa43c7d717cdeffde3d0200ad7156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Wed, 7 Dec 2022 00:09:33 +0100 Subject: [PATCH 07/18] chore: fmt --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 09a7ae3..266177b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,7 +143,9 @@ pub mod aoc_cli { match self { AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), - AocCliError::BadExitStatus(_) => write!(f, "aoc-cli exited with a non-zero status."), + AocCliError::BadExitStatus(_) => { + write!(f, "aoc-cli exited with a non-zero status.") + } AocCliError::IoError => write!(f, "could not write output files to file system."), } } @@ -192,7 +194,6 @@ pub mod aoc_cli { } else { Err(AocCliError::BadExitStatus(output)) } - } fn get_input_path(day: u8) -> String { From c8c3dc04bebbab6e96b130e023ddf60c796c7a81 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Tue, 27 Dec 2022 05:04:10 -0500 Subject: [PATCH 08/18] docs: fix "download command" link (#27) * fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 869f57d..26851a4 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ To read inputs for previous years, append the `--year/-y` flag. _(example: `carg 1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.7.0` 2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value. -Once installed, you can use the [download command](#download-input-for-a-day). +Once installed, you can use the [download command](#download-input--description-for-a-day). ### Check code formatting in CI From 1c8ea27eaeb3c2d0e12771e68813338df32004cd Mon Sep 17 00:00:00 2001 From: Andy Pymont Date: Sat, 4 Feb 2023 22:17:04 +0000 Subject: [PATCH 09/18] Make the template pass cargo clippy by resolving warnings (#28) * Resolve clippy::uninlined_format_args warnings * See https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args --- src/bin/download.rs | 4 ++-- src/bin/read.rs | 4 ++-- src/bin/scaffold.rs | 16 ++++++++-------- src/lib.rs | 10 +++++----- src/main.rs | 9 +++------ 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/bin/download.rs b/src/bin/download.rs index 6632dc6..545eacd 100644 --- a/src/bin/download.rs +++ b/src/bin/download.rs @@ -22,7 +22,7 @@ fn main() { let args = match parse_args() { Ok(args) => args, Err(e) => { - eprintln!("Failed to process arguments: {}", e); + eprintln!("Failed to process arguments: {e}"); process::exit(1); } }; @@ -39,7 +39,7 @@ fn main() { } } Err(e) => { - eprintln!("failed to spawn aoc-cli: {}", e); + eprintln!("failed to spawn aoc-cli: {e}"); process::exit(1); } } diff --git a/src/bin/read.rs b/src/bin/read.rs index d0a4321..7fe21c4 100644 --- a/src/bin/read.rs +++ b/src/bin/read.rs @@ -22,7 +22,7 @@ fn main() { let args = match parse_args() { Ok(args) => args, Err(e) => { - eprintln!("Failed to process arguments: {}", e); + eprintln!("Failed to process arguments: {e}"); process::exit(1); } }; @@ -39,7 +39,7 @@ fn main() { } } Err(e) => { - eprintln!("failed to spawn aoc-cli: {}", e); + eprintln!("failed to spawn aoc-cli: {e}"); process::exit(1); } } diff --git a/src/bin/scaffold.rs b/src/bin/scaffold.rs index 0c77b4d..2d04737 100644 --- a/src/bin/scaffold.rs +++ b/src/bin/scaffold.rs @@ -62,16 +62,16 @@ fn main() { } }; - let day_padded = format!("{:02}", day); + let day_padded = format!("{day:02}"); - let input_path = format!("src/inputs/{}.txt", day_padded); - let example_path = format!("src/examples/{}.txt", day_padded); - let module_path = format!("src/bin/{}.rs", day_padded); + let input_path = format!("src/inputs/{day_padded}.txt"); + let example_path = format!("src/examples/{day_padded}.txt"); + let module_path = format!("src/bin/{day_padded}.rs"); let mut file = match safe_create_file(&module_path) { Ok(file) => file, Err(e) => { - eprintln!("Failed to create module file: {}", e); + eprintln!("Failed to create module file: {e}"); process::exit(1); } }; @@ -81,7 +81,7 @@ fn main() { println!("Created module file \"{}\"", &module_path); } Err(e) => { - eprintln!("Failed to write module contents: {}", e); + eprintln!("Failed to write module contents: {e}"); process::exit(1); } } @@ -91,7 +91,7 @@ fn main() { println!("Created empty input file \"{}\"", &input_path); } Err(e) => { - eprintln!("Failed to create input file: {}", e); + eprintln!("Failed to create input file: {e}"); process::exit(1); } } @@ -101,7 +101,7 @@ fn main() { println!("Created empty example file \"{}\"", &example_path); } Err(e) => { - eprintln!("Failed to create example file: {}", e); + eprintln!("Failed to create example file: {e}"); process::exit(1); } } diff --git a/src/lib.rs b/src/lib.rs index 266177b..6831a1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ macro_rules! solve { pub fn read_file(folder: &str, day: u8) -> String { let cwd = env::current_dir().unwrap(); - let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day)); + let filepath = cwd.join("src").join(folder).join(format!("{day:02}.txt")); let f = fs::read_to_string(filepath); f.expect("could not open input file") @@ -197,13 +197,13 @@ pub mod aoc_cli { } fn get_input_path(day: u8) -> String { - let day_padded = format!("{:02}", day); - format!("src/inputs/{}.txt", day_padded) + let day_padded = format!("{day:02}"); + format!("src/inputs/{day_padded}.txt") } fn get_puzzle_path(day: u8) -> String { - let day_padded = format!("{:02}", day); - format!("src/puzzles/{}.md", day_padded) + let day_padded = format!("{day:02}"); + format!("src/puzzles/{day_padded}.md") } fn build_args(command: &str, args: &[String], day: u8, year: Option) -> Vec { diff --git a/src/main.rs b/src/main.rs index 95c690f..1b6c855 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::process::Command; fn main() { let total: f64 = (1..=25) .map(|day| { - let day = format!("{:02}", day); + let day = format!("{day:02}"); let mut args = vec!["run", "--bin", &day]; if cfg!(not(debug_assertions)) { @@ -18,7 +18,7 @@ fn main() { let cmd = Command::new("cargo").args(&args).output().unwrap(); println!("----------"); - println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET); + println!("{ANSI_BOLD}| Day {day} |{ANSI_RESET}"); println!("----------"); let output = String::from_utf8(cmd.stdout).unwrap(); @@ -41,8 +41,5 @@ fn main() { }) .sum(); - println!( - "{}Total:{} {}{:.2}ms{}", - ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total, ANSI_RESET - ); + println!("{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total:.2}ms{ANSI_RESET}"); } From d10ec0573ee1b51ddd5a4fb669dd8559611b099d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:46:19 +0200 Subject: [PATCH 10/18] feat: add `--submit ` option to `cargo solve` (#25) * remove `--year` flag in favor of `.config/config.toml` * cleanup option handling for `cargo solve` --- .cargo/{config => config.toml} | 5 ++- README.md | 17 +++++--- src/bin/download.rs | 4 +- src/bin/read.rs | 4 +- src/bin/scaffold.rs | 8 ++-- src/bin/solve.rs | 58 +++++++++++++++++++++++++ src/lib.rs | 78 +++++++++++++++++++++++++++++----- 7 files changed, 145 insertions(+), 29 deletions(-) rename .cargo/{config => config.toml} (70%) create mode 100644 src/bin/solve.rs diff --git a/.cargo/config b/.cargo/config.toml similarity index 70% rename from .cargo/config rename to .cargo/config.toml index 03ad300..205746e 100644 --- a/.cargo/config +++ b/.cargo/config.toml @@ -3,5 +3,8 @@ scaffold = "run --bin scaffold --quiet --release -- " download = "run --bin download --quiet --release -- " read = "run --bin read --quiet --release -- " -solve = "run --bin" +solve = "run --bin solve --quiet --release -- " all = "run" + +[env] +AOC_YEAR = "2022" diff --git a/README.md b/README.md index 26851a4..f13ff7d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ This template supports all major OS (macOS, Linux, Windows). 1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github. 2. Click [Use this template](https://github.com/fspoettel/advent-of-code-rust/generate) and create your repository. 3. Clone your repository to your computer. +4. If you are solving a previous year's aoc and want to use the `aoc-cli` integration, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect that. ### Setup rust πŸ’» @@ -71,8 +72,6 @@ cargo download # πŸŽ„ Successfully wrote puzzle to "src/puzzles/01.md". ``` -To download inputs for previous years, append the `--year/-y` flag. _(example: `cargo download 1 --year 2020`)_ - Puzzle descriptions are stored in `src/puzzles` as markdown files. Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3). ### Run solutions for a day @@ -96,6 +95,13 @@ cargo solve Displayed _timings_ show the raw execution time of your solution without overhead (e.g. file reads). +#### Submitting solutions + +> **Note** +> This requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). + +In order to submit part of a solution for checking, append the `--submit ` option to the `solve` command. + ### Run all solutions ```sh @@ -140,6 +146,7 @@ cargo fmt ```sh cargo clippy ``` +## Optional template features ### Read puzzle description in terminal @@ -156,13 +163,9 @@ cargo read # ...the input... ``` -To read inputs for previous years, append the `--year/-y` flag. _(example: `cargo read 1 --year 2020`)_ - -## Optional template features - ### Download puzzle inputs via aoc-cli -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.7.0` +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` 2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value. Once installed, you can use the [download command](#download-input--description-for-a-day). diff --git a/src/bin/download.rs b/src/bin/download.rs index 545eacd..bca4b64 100644 --- a/src/bin/download.rs +++ b/src/bin/download.rs @@ -7,14 +7,12 @@ use std::process; struct Args { day: u8, - year: Option, } fn parse_args() -> Result { let mut args = pico_args::Arguments::from_env(); Ok(Args { day: args.free_from_str()?, - year: args.opt_value_from_str(["-y", "--year"])?, }) } @@ -32,7 +30,7 @@ fn main() { process::exit(1); } - match aoc_cli::download(args.day, args.year) { + match aoc_cli::download(args.day) { Ok(cmd_output) => { if !cmd_output.status.success() { process::exit(1); diff --git a/src/bin/read.rs b/src/bin/read.rs index 7fe21c4..7988a6a 100644 --- a/src/bin/read.rs +++ b/src/bin/read.rs @@ -7,14 +7,12 @@ use std::process; struct Args { day: u8, - year: Option, } fn parse_args() -> Result { let mut args = pico_args::Arguments::from_env(); Ok(Args { day: args.free_from_str()?, - year: args.opt_value_from_str(["-y", "--year"])?, }) } @@ -32,7 +30,7 @@ fn main() { process::exit(1); } - match aoc_cli::read(args.day, args.year) { + match aoc_cli::read(args.day) { Ok(cmd_output) => { if !cmd_output.status.success() { process::exit(1); diff --git a/src/bin/scaffold.rs b/src/bin/scaffold.rs index 2d04737..348f73b 100644 --- a/src/bin/scaffold.rs +++ b/src/bin/scaffold.rs @@ -8,7 +8,7 @@ use std::{ process, }; -const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option { +const MODULE_TEMPLATE: &str = r#"pub fn part_one(input: &str) -> Option { None } @@ -18,8 +18,8 @@ pub fn part_two(input: &str) -> Option { fn main() { let input = &advent_of_code::read_file("inputs", DAY); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); + advent_of_code::solve!(DAY, 1, part_one, input); + advent_of_code::solve!(DAY, 2, part_two, input); } #[cfg(test)] @@ -38,7 +38,7 @@ mod tests { assert_eq!(part_two(&input), None); } } -"###; +"#; fn parse_args() -> Result { let mut args = pico_args::Arguments::from_env(); diff --git a/src/bin/solve.rs b/src/bin/solve.rs new file mode 100644 index 0000000..4c866b7 --- /dev/null +++ b/src/bin/solve.rs @@ -0,0 +1,58 @@ +/* + * This file contains template code. + * There is no need to edit this file unless you want to change template functionality. + */ + +use std::process::{self, Command, Stdio}; + +struct Args { + day: u8, + release: bool, + submit: Option, +} + +fn parse_args() -> Result { + let mut args = pico_args::Arguments::from_env(); + Ok(Args { + day: args.free_from_str()?, + release: args.contains("--release"), + submit: args.opt_value_from_str("--submit")?, + }) +} + +fn run_solution(day: u8, release: bool, submit_part: Option) -> Result<(), std::io::Error> { + let day_padded = format!("{:02}", day); + + let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded]; + + if release { + cmd_args.push("--release".to_string()); + } + + if let Some(submit_part) = submit_part { + cmd_args.push("--".to_string()); + cmd_args.push("--submit".to_string()); + cmd_args.push(submit_part.to_string()) + } + + let mut cmd = Command::new("cargo") + .args(&cmd_args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .spawn()?; + + cmd.wait()?; + Ok(()) +} + +fn main() { + let args = match parse_args() { + Ok(args) => args, + Err(e) => { + eprintln!("Failed to process arguments: {}", e); + process::exit(1); + } + }; + + run_solution(args.day, args.release, args.submit).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs index 6831a1e..aab0ece 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,10 +14,42 @@ pub const ANSI_RESET: &str = "\x1b[0m"; #[macro_export] macro_rules! solve { - ($part:expr, $solver:ident, $input:expr) => {{ - use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET}; + ($day:expr, $part:expr, $solver:ident, $input:expr) => {{ + use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET, aoc_cli}; use std::fmt::Display; use std::time::Instant; + use std::env; + use std::process; + + fn submit_if_requested(result: T) { + let args: Vec = env::args().collect(); + + if args.contains(&"--submit".into()) { + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + if args.len() < 3 { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + } + + let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1; + let part_submit = match args[part_index].parse::() { + Ok(x) => x, + Err(_) => { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + } + }; + + if part_submit == $part { + println!("Submitting puzzle answer for part {}...", $part); + aoc_cli::submit($day, $part, result).unwrap(); + } + } + } fn print_result(func: impl FnOnce(&str) -> Option, input: &str) { let timer = Instant::now(); @@ -29,6 +61,7 @@ macro_rules! solve { "{} {}(elapsed: {:.2?}){}", result, ANSI_ITALIC, elapsed, ANSI_RESET ); + submit_if_requested(result); } None => { println!("not solved.") @@ -127,10 +160,10 @@ mod tests { pub mod aoc_cli { use std::{ fmt::Display, - fs::create_dir_all, process::{Command, Output, Stdio}, }; + #[derive(Debug)] pub enum AocCliError { CommandNotFound, CommandNotCallable, @@ -159,17 +192,26 @@ pub mod aoc_cli { Ok(()) } - pub fn read(day: u8, year: Option) -> Result { + pub fn read(day: u8) -> Result { // TODO: output local puzzle if present. - let args = build_args("read", &[], day, year); + let puzzle_path = get_puzzle_path(day); + + let args = build_args( + "read", + &[ + "--description-only".into(), + "--puzzle-file".into(), + puzzle_path, + ], + day, + ); + call_aoc_cli(&args) } - pub fn download(day: u8, year: Option) -> Result { + pub fn download(day: u8) -> Result { let input_path = get_input_path(day); - let puzzle_path = get_puzzle_path(day); - create_dir_all("src/puzzles").map_err(|_| AocCliError::IoError)?; let args = build_args( "download", @@ -181,7 +223,6 @@ pub mod aoc_cli { puzzle_path.to_string(), ], day, - year, ); let output = call_aoc_cli(&args)?; @@ -196,6 +237,14 @@ pub mod aoc_cli { } } + pub fn submit(day: u8, part: u8, result: T) -> Result { + // workaround: the argument order is inverted for submit. + let mut args = build_args("submit", &[], day); + args.push(part.to_string()); + args.push(result.to_string()); + call_aoc_cli(&args) + } + fn get_input_path(day: u8) -> String { let day_padded = format!("{day:02}"); format!("src/inputs/{day_padded}.txt") @@ -206,10 +255,17 @@ pub mod aoc_cli { format!("src/puzzles/{day_padded}.md") } - fn build_args(command: &str, args: &[String], day: u8, year: Option) -> Vec { + fn get_year() -> Option { + match std::env::var("AOC_YEAR") { + Ok(x) => x.parse().ok().or(None), + Err(_) => None, + } + } + + fn build_args(command: &str, args: &[String], day: u8) -> Vec { let mut cmd_args = args.to_vec(); - if let Some(year) = year { + if let Some(year) = get_year() { cmd_args.push("--year".into()); cmd_args.push(year.to_string()); } From 70dac9329f127dc977fd8d4bad825597247761aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:08:46 +0200 Subject: [PATCH 11/18] feat: implement benchmarks (#30) --- .cargo/config.toml | 11 +- .gitignore | 6 +- Cargo.toml | 6 + README.md | 80 +++--- src/bin/.keep | 0 src/bin/download.rs | 44 ---- src/bin/read.rs | 44 ---- src/bin/solve.rs | 58 ---- src/helpers.rs | 4 - src/lib.rs | 291 +-------------------- src/main.rs | 122 ++++++--- src/template/aoc_cli.rs | 127 +++++++++ src/template/commands/all.rs | 261 ++++++++++++++++++ src/template/commands/download.rs | 14 + src/template/commands/mod.rs | 5 + src/template/commands/read.rs | 15 ++ src/{bin => template/commands}/scaffold.rs | 49 +--- src/template/commands/solve.rs | 31 +++ src/template/mod.rs | 34 +++ src/template/readme_benchmarks.rs | 183 +++++++++++++ src/template/runner.rs | 165 ++++++++++++ 21 files changed, 988 insertions(+), 562 deletions(-) create mode 100644 src/bin/.keep delete mode 100644 src/bin/download.rs delete mode 100644 src/bin/read.rs delete mode 100644 src/bin/solve.rs delete mode 100644 src/helpers.rs create mode 100644 src/template/aoc_cli.rs create mode 100644 src/template/commands/all.rs create mode 100644 src/template/commands/download.rs create mode 100644 src/template/commands/mod.rs create mode 100644 src/template/commands/read.rs rename src/{bin => template/commands}/scaffold.rs (53%) create mode 100644 src/template/commands/solve.rs create mode 100644 src/template/mod.rs create mode 100644 src/template/readme_benchmarks.rs create mode 100644 src/template/runner.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 205746e..754f083 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,10 +1,11 @@ [alias] -scaffold = "run --bin scaffold --quiet --release -- " -download = "run --bin download --quiet --release -- " -read = "run --bin read --quiet --release -- " +scaffold = "run --quiet --release -- scaffold" +download = "run --quiet --release -- download" +read = "run --quiet --release -- read" -solve = "run --bin solve --quiet --release -- " -all = "run" +solve = "run --quiet --release -- solve" +all = "run --quiet --release -- all" +time = "run --quiet --release -- all --release --time" [env] AOC_YEAR = "2022" diff --git a/.gitignore b/.gitignore index f2fd7aa..db1d6f2 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,7 @@ target/ # Advent of Code # @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3 -/src/inputs -!/src/inputs/.keep +/data +!/data/inputs/.keep +!/data/examples/.keep +!/data/puzzles/.keep diff --git a/Cargo.toml b/Cargo.toml index 3ec21e9..a871f8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,11 @@ default-run = "advent_of_code" publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +doctest = false + +[features] +test_lib = [] + [dependencies] pico-args = "0.5.0" diff --git a/README.md b/README.md index f13ff7d..ec8963c 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. + + --- ## Template setup @@ -17,7 +19,7 @@ This template supports all major OS (macOS, Linux, Windows). 1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github. 2. Click [Use this template](https://github.com/fspoettel/advent-of-code-rust/generate) and create your repository. 3. Clone your repository to your computer. -4. If you are solving a previous year's aoc and want to use the `aoc-cli` integration, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect that. +4. If you are solving a previous year's advent of code, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect the year you are solving. ### Setup rust πŸ’» @@ -38,18 +40,18 @@ This template supports all major OS (macOS, Linux, Windows). cargo scaffold # output: -# Created module "src/bin/01.rs" -# Created empty input file "src/inputs/01.txt" -# Created empty example file "src/examples/01.txt" +# Created module file "src/bin/01.rs" +# Created empty input file "data/inputs/01.txt" +# Created empty example file "data/examples/01.txt" # --- # πŸŽ„ Type `cargo solve 01` to run your solution. ``` -Individual solutions live in the `./src/bin/` directory as separate binaries. +Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory. -Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solution against the example input. For some puzzles, it might be easier to forgo the example file and hardcode inputs into the tests. +Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solutions against the example input. -When editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks. +Tip: when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks. ### Download input & description for a day @@ -61,19 +63,14 @@ When editing a solution, `rust-analyzer` will display buttons for running / debu cargo download # output: -# Loaded session cookie from "/Users//.adventofcode.session". -# Fetching puzzle for day 1, 2022... -# Saving puzzle description to "src/puzzles/01.md"... -# Downloading input for day 1, 2022... -# Saving puzzle input to "src/inputs/01.txt"... -# Done! +# [INFO aoc] πŸŽ„ aoc-cli - Advent of Code command-line tool +# [INFO aoc_client] πŸŽ… Saved puzzle to 'data/puzzles/01.md' +# [INFO aoc_client] πŸŽ… Saved input to 'data/inputs/01.txt' # --- -# πŸŽ„ Successfully wrote input to "src/inputs/01.txt". -# πŸŽ„ Successfully wrote puzzle to "src/puzzles/01.md". +# πŸŽ„ Successfully wrote input to "data/inputs/01.txt". +# πŸŽ„ Successfully wrote puzzle to "data/puzzles/01.md". ``` -Puzzle descriptions are stored in `src/puzzles` as markdown files. Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3). - ### Run solutions for a day ```sh @@ -81,19 +78,17 @@ Puzzle descriptions are stored in `src/puzzles` as markdown files. Puzzle inputs cargo solve # output: +# Finished dev [unoptimized + debuginfo] target(s) in 0.13s # Running `target/debug/01` -# πŸŽ„ Part 1 πŸŽ„ -# -# 6 (elapsed: 37.03Β΅s) -# -# πŸŽ„ Part 2 πŸŽ„ -# -# 9 (elapsed: 33.18Β΅s) +# Part 1: 42 (166.0ns) +# Part 2: 42 (41.0ns) ``` -`solve` is an alias for `cargo run --bin`. To run an optimized version for benchmarking, append the `--release` flag. +The `solve` command runs your solution. If you set the `--release` flag, real puzzle _inputs_ will be passed to your solution, otherwise the _example_ inputs will be used. + +If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times - depending on execution time of first execution - and print the average execution time. -Displayed _timings_ show the raw execution time of your solution without overhead (e.g. file reads). +For example, a benchmarked execution against real inputs of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads. #### Submitting solutions @@ -112,22 +107,21 @@ cargo all # ---------- # | Day 01 | # ---------- -# πŸŽ„ Part 1 πŸŽ„ -# -# 0 (elapsed: 170.00Β΅s) -# -# πŸŽ„ Part 2 πŸŽ„ -# -# 0 (elapsed: 30.00Β΅s) +# Part 1: 42 (19.0ns) +# Part 2: 42 (19.0ns) # <...other days...> # Total: 0.20ms ``` -`all` is an alias for `cargo run`. To run an optimized version for benchmarking, use the `--release` flag. +This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, `--release` controls whether real inputs will be used. -_Total timing_ is computed from individual solution _timings_ and excludes as much overhead as possible. +#### Update readme benchmarks -### Run all solutions against the example input +The template can output a table with solution times to your readme. Please note that these are not "scientific" benchmarks, understand them as a fun approximation. πŸ˜‰ + +In order to generate a benchmarking table, run `cargo all --release --time`. If everything goes well, the command will output "_Successfully updated README with benchmarks._" after the execution finishes. + +### Run all tests ```sh cargo test @@ -148,6 +142,13 @@ cargo clippy ``` ## Optional template features +### Download puzzle inputs via aoc-cli + +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` +2. Create an `.adventofcode.session` file in your home directory and paste your session cookie. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1] + +Once installed, you can use the [download command](#download-input--description-for-a-day). + ### Read puzzle description in terminal > **Note** @@ -163,13 +164,6 @@ cargo read # ...the input... ``` -### Download puzzle inputs via aoc-cli - -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` -2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value. - -Once installed, you can use the [download command](#download-input--description-for-a-day). - ### Check code formatting in CI Uncomment the `format` job in the `ci.yml` workflow to enable fmt checks in CI. diff --git a/src/bin/.keep b/src/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/bin/download.rs b/src/bin/download.rs deleted file mode 100644 index bca4b64..0000000 --- a/src/bin/download.rs +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use advent_of_code::aoc_cli; -use std::process; - -struct Args { - day: u8, -} - -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - Ok(Args { - day: args.free_from_str()?, - }) -} - -fn main() { - let args = match parse_args() { - Ok(args) => args, - Err(e) => { - eprintln!("Failed to process arguments: {e}"); - process::exit(1); - } - }; - - if aoc_cli::check().is_err() { - eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); - process::exit(1); - } - - match aoc_cli::download(args.day) { - Ok(cmd_output) => { - if !cmd_output.status.success() { - process::exit(1); - } - } - Err(e) => { - eprintln!("failed to spawn aoc-cli: {e}"); - process::exit(1); - } - } -} diff --git a/src/bin/read.rs b/src/bin/read.rs deleted file mode 100644 index 7988a6a..0000000 --- a/src/bin/read.rs +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use advent_of_code::aoc_cli; -use std::process; - -struct Args { - day: u8, -} - -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - Ok(Args { - day: args.free_from_str()?, - }) -} - -fn main() { - let args = match parse_args() { - Ok(args) => args, - Err(e) => { - eprintln!("Failed to process arguments: {e}"); - process::exit(1); - } - }; - - if aoc_cli::check().is_err() { - eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); - process::exit(1); - } - - match aoc_cli::read(args.day) { - Ok(cmd_output) => { - if !cmd_output.status.success() { - process::exit(1); - } - } - Err(e) => { - eprintln!("failed to spawn aoc-cli: {e}"); - process::exit(1); - } - } -} diff --git a/src/bin/solve.rs b/src/bin/solve.rs deleted file mode 100644 index 4c866b7..0000000 --- a/src/bin/solve.rs +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ - -use std::process::{self, Command, Stdio}; - -struct Args { - day: u8, - release: bool, - submit: Option, -} - -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - Ok(Args { - day: args.free_from_str()?, - release: args.contains("--release"), - submit: args.opt_value_from_str("--submit")?, - }) -} - -fn run_solution(day: u8, release: bool, submit_part: Option) -> Result<(), std::io::Error> { - let day_padded = format!("{:02}", day); - - let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded]; - - if release { - cmd_args.push("--release".to_string()); - } - - if let Some(submit_part) = submit_part { - cmd_args.push("--".to_string()); - cmd_args.push("--submit".to_string()); - cmd_args.push(submit_part.to_string()) - } - - let mut cmd = Command::new("cargo") - .args(&cmd_args) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .spawn()?; - - cmd.wait()?; - Ok(()) -} - -fn main() { - let args = match parse_args() { - Ok(args) => args, - Err(e) => { - eprintln!("Failed to process arguments: {}", e); - process::exit(1); - } - }; - - run_solution(args.day, args.release, args.submit).unwrap(); -} diff --git a/src/helpers.rs b/src/helpers.rs deleted file mode 100644 index 079071f..0000000 --- a/src/helpers.rs +++ /dev/null @@ -1,4 +0,0 @@ -/* - * Use this file if you want to extract helpers from your solutions. - * Example import from this file: `use advent_of_code::helpers::example_fn;`. - */ diff --git a/src/lib.rs b/src/lib.rs index aab0ece..612b5b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,290 +1 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - * Prefer `./helpers.rs` if you want to extract code from your solutions. - */ -use std::env; -use std::fs; - -pub mod helpers; - -pub const ANSI_ITALIC: &str = "\x1b[3m"; -pub const ANSI_BOLD: &str = "\x1b[1m"; -pub const ANSI_RESET: &str = "\x1b[0m"; - -#[macro_export] -macro_rules! solve { - ($day:expr, $part:expr, $solver:ident, $input:expr) => {{ - use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET, aoc_cli}; - use std::fmt::Display; - use std::time::Instant; - use std::env; - use std::process; - - fn submit_if_requested(result: T) { - let args: Vec = env::args().collect(); - - if args.contains(&"--submit".into()) { - if aoc_cli::check().is_err() { - eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); - process::exit(1); - } - - if args.len() < 3 { - eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); - process::exit(1); - } - - let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1; - let part_submit = match args[part_index].parse::() { - Ok(x) => x, - Err(_) => { - eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); - process::exit(1); - } - }; - - if part_submit == $part { - println!("Submitting puzzle answer for part {}...", $part); - aoc_cli::submit($day, $part, result).unwrap(); - } - } - } - - fn print_result(func: impl FnOnce(&str) -> Option, input: &str) { - let timer = Instant::now(); - let result = func(input); - let elapsed = timer.elapsed(); - match result { - Some(result) => { - println!( - "{} {}(elapsed: {:.2?}){}", - result, ANSI_ITALIC, elapsed, ANSI_RESET - ); - submit_if_requested(result); - } - None => { - println!("not solved.") - } - } - } - - println!("πŸŽ„ {}Part {}{} πŸŽ„", ANSI_BOLD, $part, ANSI_RESET); - print_result($solver, $input); - }}; -} - -pub fn read_file(folder: &str, day: u8) -> String { - let cwd = env::current_dir().unwrap(); - - let filepath = cwd.join("src").join(folder).join(format!("{day:02}.txt")); - - let f = fs::read_to_string(filepath); - f.expect("could not open input file") -} - -fn parse_time(val: &str, postfix: &str) -> f64 { - val.split(postfix).next().unwrap().parse().unwrap() -} - -pub fn parse_exec_time(output: &str) -> f64 { - output.lines().fold(0_f64, |acc, l| { - if !l.contains("elapsed:") { - acc - } else { - let timing = l.split("(elapsed: ").last().unwrap(); - // use `contains` istd. of `ends_with`: string may contain ANSI escape sequences. - // for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200 - if timing.contains("ns)") { - acc // range below rounding precision. - } else if timing.contains("Β΅s)") { - acc + parse_time(timing, "Β΅s") / 1000_f64 - } else if timing.contains("ms)") { - acc + parse_time(timing, "ms") - } else if timing.contains("s)") { - acc + parse_time(timing, "s") * 1000_f64 - } else { - acc - } - } - }) -} - -/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333 -#[cfg(test)] -macro_rules! assert_approx_eq { - ($a:expr, $b:expr) => {{ - let (a, b) = (&$a, &$b); - assert!( - (*a - *b).abs() < 1.0e-6, - "{} is not approximately equal to {}", - *a, - *b - ); - }}; -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_exec_time() { - assert_approx_eq!( - parse_exec_time(&format!( - "πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 74.13ns){}\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 50.00ns){}", - ANSI_RESET, ANSI_RESET - )), - 0_f64 - ); - - assert_approx_eq!( - parse_exec_time("πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 755Β΅s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 700Β΅s)"), - 1.455_f64 - ); - - assert_approx_eq!( - parse_exec_time("πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 70Β΅s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 1.45ms)"), - 1.52_f64 - ); - - assert_approx_eq!( - parse_exec_time( - "πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 10.3s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 100.50ms)" - ), - 10400.50_f64 - ); - } -} - -pub mod aoc_cli { - use std::{ - fmt::Display, - process::{Command, Output, Stdio}, - }; - - #[derive(Debug)] - pub enum AocCliError { - CommandNotFound, - CommandNotCallable, - BadExitStatus(Output), - IoError, - } - - impl Display for AocCliError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), - AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), - AocCliError::BadExitStatus(_) => { - write!(f, "aoc-cli exited with a non-zero status.") - } - AocCliError::IoError => write!(f, "could not write output files to file system."), - } - } - } - - pub fn check() -> Result<(), AocCliError> { - Command::new("aoc") - .arg("-V") - .output() - .map_err(|_| AocCliError::CommandNotFound)?; - Ok(()) - } - - pub fn read(day: u8) -> Result { - // TODO: output local puzzle if present. - let puzzle_path = get_puzzle_path(day); - - let args = build_args( - "read", - &[ - "--description-only".into(), - "--puzzle-file".into(), - puzzle_path, - ], - day, - ); - - call_aoc_cli(&args) - } - - pub fn download(day: u8) -> Result { - let input_path = get_input_path(day); - let puzzle_path = get_puzzle_path(day); - - let args = build_args( - "download", - &[ - "--overwrite".into(), - "--input-file".into(), - input_path.to_string(), - "--puzzle-file".into(), - puzzle_path.to_string(), - ], - day, - ); - - let output = call_aoc_cli(&args)?; - - if output.status.success() { - println!("---"); - println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); - println!("πŸŽ„ Successfully wrote puzzle to \"{}\".", &puzzle_path); - Ok(output) - } else { - Err(AocCliError::BadExitStatus(output)) - } - } - - pub fn submit(day: u8, part: u8, result: T) -> Result { - // workaround: the argument order is inverted for submit. - let mut args = build_args("submit", &[], day); - args.push(part.to_string()); - args.push(result.to_string()); - call_aoc_cli(&args) - } - - fn get_input_path(day: u8) -> String { - let day_padded = format!("{day:02}"); - format!("src/inputs/{day_padded}.txt") - } - - fn get_puzzle_path(day: u8) -> String { - let day_padded = format!("{day:02}"); - format!("src/puzzles/{day_padded}.md") - } - - fn get_year() -> Option { - match std::env::var("AOC_YEAR") { - Ok(x) => x.parse().ok().or(None), - Err(_) => None, - } - } - - fn build_args(command: &str, args: &[String], day: u8) -> Vec { - let mut cmd_args = args.to_vec(); - - if let Some(year) = get_year() { - cmd_args.push("--year".into()); - cmd_args.push(year.to_string()); - } - - cmd_args.append(&mut vec!["--day".into(), day.to_string(), command.into()]); - - cmd_args - } - - fn call_aoc_cli(args: &[String]) -> Result { - if cfg!(debug_assertions) { - println!("Calling >aoc with: {}", args.join(" ")); - } - - Command::new("aoc") - .args(args) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .map_err(|_| AocCliError::CommandNotCallable) - } -} +pub mod template; diff --git a/src/main.rs b/src/main.rs index 1b6c855..e4750f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,45 +1,93 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET}; -use std::process::Command; +use advent_of_code::template::commands::{ + all::all_handler, download::download_handler, read::read_handler, scaffold::scaffold_handler, + solve::solve_handler, +}; +use args::{parse_args, AppArgs}; -fn main() { - let total: f64 = (1..=25) - .map(|day| { - let day = format!("{day:02}"); - - let mut args = vec!["run", "--bin", &day]; - if cfg!(not(debug_assertions)) { - args.push("--release"); - } +mod args { + use std::process; - let cmd = Command::new("cargo").args(&args).output().unwrap(); + pub enum AppArgs { + Download { + day: u8, + }, + Read { + day: u8, + }, + Scaffold { + day: u8, + }, + Solve { + day: u8, + release: bool, + time: bool, + submit: Option, + }, + All { + release: bool, + time: bool, + }, + } - println!("----------"); - println!("{ANSI_BOLD}| Day {day} |{ANSI_RESET}"); - println!("----------"); + pub fn parse_args() -> Result> { + let mut args = pico_args::Arguments::from_env(); - let output = String::from_utf8(cmd.stdout).unwrap(); - let is_empty = output.is_empty(); + let app_args = match args.subcommand()?.as_deref() { + Some("all") => AppArgs::All { + release: args.contains("--release"), + time: args.contains("--time"), + }, + Some("download") => AppArgs::Download { + day: args.free_from_str()?, + }, + Some("read") => AppArgs::Read { + day: args.free_from_str()?, + }, + Some("scaffold") => AppArgs::Scaffold { + day: args.free_from_str()?, + }, + Some("solve") => AppArgs::Solve { + day: args.free_from_str()?, + release: args.contains("--release"), + submit: args.opt_value_from_str("--submit")?, + time: args.contains("--time"), + }, + Some(x) => { + eprintln!("Unknown command: {}", x); + process::exit(1); + } + None => { + eprintln!("No command specified."); + process::exit(1); + } + }; - println!( - "{}", - if is_empty { - "Not solved." - } else { - output.trim() - } - ); + let remaining = args.finish(); + if !remaining.is_empty() { + eprintln!("Warning: unknown argument(s): {:?}.", remaining); + } - if is_empty { - 0_f64 - } else { - advent_of_code::parse_exec_time(&output) - } - }) - .sum(); + Ok(app_args) + } +} - println!("{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total:.2}ms{ANSI_RESET}"); +fn main() { + match parse_args() { + Err(err) => { + eprintln!("Error: {}", err); + std::process::exit(1); + } + Ok(args) => match args { + AppArgs::All { release, time } => all_handler(release, time), + AppArgs::Download { day } => download_handler(day), + AppArgs::Read { day } => read_handler(day), + AppArgs::Scaffold { day } => scaffold_handler(day), + AppArgs::Solve { + day, + release, + time, + submit, + } => solve_handler(day, release, time, submit), + }, + }; } diff --git a/src/template/aoc_cli.rs b/src/template/aoc_cli.rs new file mode 100644 index 0000000..31ac7ac --- /dev/null +++ b/src/template/aoc_cli.rs @@ -0,0 +1,127 @@ +/// Wrapper module around the "aoc-cli" command-line. +use std::{ + fmt::Display, + process::{Command, Output, Stdio}, +}; + +#[derive(Debug)] +pub enum AocCliError { + CommandNotFound, + CommandNotCallable, + BadExitStatus(Output), + IoError, +} + +impl Display for AocCliError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), + AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), + AocCliError::BadExitStatus(_) => { + write!(f, "aoc-cli exited with a non-zero status.") + } + AocCliError::IoError => write!(f, "could not write output files to file system."), + } + } +} + +pub fn check() -> Result<(), AocCliError> { + Command::new("aoc") + .arg("-V") + .output() + .map_err(|_| AocCliError::CommandNotFound)?; + Ok(()) +} + +pub fn read(day: u8) -> Result { + let puzzle_path = get_puzzle_path(day); + + let args = build_args( + "read", + &[ + "--description-only".into(), + "--puzzle-file".into(), + puzzle_path, + ], + day, + ); + + call_aoc_cli(&args) +} + +pub fn download(day: u8) -> Result { + let input_path = get_input_path(day); + let puzzle_path = get_puzzle_path(day); + + let args = build_args( + "download", + &[ + "--overwrite".into(), + "--input-file".into(), + input_path.to_string(), + "--puzzle-file".into(), + puzzle_path.to_string(), + ], + day, + ); + + let output = call_aoc_cli(&args)?; + println!("---"); + println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); + println!("πŸŽ„ Successfully wrote puzzle to \"{}\".", &puzzle_path); + Ok(output) +} + +pub fn submit(day: u8, part: u8, result: &str) -> Result { + // workaround: the argument order is inverted for submit. + let mut args = build_args("submit", &[], day); + args.push(part.to_string()); + args.push(result.to_string()); + call_aoc_cli(&args) +} + +fn get_input_path(day: u8) -> String { + let day_padded = format!("{:02}", day); + format!("data/inputs/{}.txt", day_padded) +} + +fn get_puzzle_path(day: u8) -> String { + let day_padded = format!("{:02}", day); + format!("data/puzzles/{}.md", day_padded) +} + +fn get_year() -> Option { + match std::env::var("AOC_YEAR") { + Ok(x) => x.parse().ok().or(None), + Err(_) => None, + } +} + +fn build_args(command: &str, args: &[String], day: u8) -> Vec { + let mut cmd_args = args.to_vec(); + + if let Some(year) = get_year() { + cmd_args.push("--year".into()); + cmd_args.push(year.to_string()); + } + + cmd_args.append(&mut vec!["--day".into(), day.to_string(), command.into()]); + + cmd_args +} + +fn call_aoc_cli(args: &[String]) -> Result { + // println!("Calling >aoc with: {}", args.join(" ")); + let output = Command::new("aoc") + .args(args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(|_| AocCliError::CommandNotCallable)?; + + if output.status.success() { + Ok(output) + } else { + Err(AocCliError::BadExitStatus(output)) + } +} diff --git a/src/template/commands/all.rs b/src/template/commands/all.rs new file mode 100644 index 0000000..87fc2c9 --- /dev/null +++ b/src/template/commands/all.rs @@ -0,0 +1,261 @@ +use std::io; + +use crate::template::{ + readme_benchmarks::{self, Timings}, + ANSI_BOLD, ANSI_ITALIC, ANSI_RESET, +}; + +pub fn all_handler(is_release: bool, is_timed: bool) { + let mut timings: Vec = vec![]; + + (1..=25).for_each(|day| { + if day > 1 { + println!(); + } + + println!("{}Day {}{}", ANSI_BOLD, day, ANSI_RESET); + println!("------"); + + let output = child_commands::run_solution(day, is_timed, is_release).unwrap(); + + if output.is_empty() { + println!("Not solved."); + } else { + let val = child_commands::parse_exec_time(&output, day); + timings.push(val); + } + }); + + if is_timed { + let total_millis = timings.iter().map(|x| x.total_nanos).sum::() / 1000000_f64; + + println!( + "\n{}Total:{} {}{:.2}ms{}", + ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total_millis, ANSI_RESET + ); + + if is_release { + match readme_benchmarks::update(timings, total_millis) { + Ok(_) => println!("Successfully updated README with benchmarks."), + Err(_) => { + eprintln!("Failed to update readme with benchmarks."); + } + } + } + } +} + +#[derive(Debug)] +pub enum Error { + BrokenPipe, + Parser(String), + IO(io::Error), +} + +impl From for Error { + fn from(e: std::io::Error) -> Self { + Error::IO(e) + } +} + +pub fn get_path_for_bin(day: usize) -> String { + let day_padded = format!("{:02}", day); + format!("./src/bin/{}.rs", day_padded) +} + +/// All solutions live in isolated binaries. +/// This module encapsulates interaction with these binaries, both invoking them as well as parsing the timing output. +mod child_commands { + use super::{get_path_for_bin, Error}; + use std::{ + io::{BufRead, BufReader}, + path::Path, + process::{Command, Stdio}, + thread, + }; + + /// Run the solution bin for a given day + pub fn run_solution( + day: usize, + is_timed: bool, + is_release: bool, + ) -> Result, Error> { + let day_padded = format!("{:02}", day); + + // skip command invocation for days that have not been scaffolded yet. + if !Path::new(&get_path_for_bin(day)).exists() { + return Ok(vec![]); + } + + let mut args = vec!["run", "--quiet", "--bin", &day_padded]; + + if is_release { + args.push("--release"); + } + + if is_timed { + // mirror `--time` flag to child invocations. + args.push("--"); + args.push("--time"); + } + + // spawn child command with piped stdout/stderr. + // forward output to stdout/stderr while grabbing stdout lines. + + let mut cmd = Command::new("cargo") + .args(&args) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + + let stdout = BufReader::new(cmd.stdout.take().ok_or(super::Error::BrokenPipe)?); + let stderr = BufReader::new(cmd.stderr.take().ok_or(super::Error::BrokenPipe)?); + + let mut output = vec![]; + + let thread = thread::spawn(move || { + stderr.lines().for_each(|line| { + eprintln!("{}", line.unwrap()); + }); + }); + + for line in stdout.lines() { + let line = line.unwrap(); + println!("{}", line); + output.push(line); + } + + thread.join().unwrap(); + cmd.wait()?; + + Ok(output) + } + + pub fn parse_exec_time(output: &[String], day: usize) -> super::Timings { + let mut timings = super::Timings { + day, + part_1: None, + part_2: None, + total_nanos: 0_f64, + }; + + output + .iter() + .filter_map(|l| { + if !l.contains(" samples)") { + return None; + } + + let (timing_str, nanos) = match parse_time(l) { + Some(v) => v, + None => { + eprintln!("Could not parse timings from line: {l}"); + return None; + } + }; + + let part = l.split(':').next()?; + Some((part, timing_str, nanos)) + }) + .for_each(|(part, timing_str, nanos)| { + if part.contains("Part 1") { + timings.part_1 = Some(timing_str.into()); + } else if part.contains("Part 2") { + timings.part_2 = Some(timing_str.into()); + } + + timings.total_nanos += nanos; + }); + + timings + } + + fn parse_to_float(s: &str, postfix: &str) -> Option { + s.split(postfix).next()?.parse().ok() + } + + fn parse_time(line: &str) -> Option<(&str, f64)> { + // for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200 + let str_timing = line + .split(" samples)") + .next()? + .split('(') + .last()? + .split('@') + .next()? + .trim(); + + let parsed_timing = match str_timing { + s if s.contains("ns") => s.split("ns").next()?.parse::().ok(), + s if s.contains("Β΅s") => parse_to_float(s, "Β΅s").map(|x| x * 1000_f64), + s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1000000_f64), + s => parse_to_float(s, "s").map(|x| x * 1000000000_f64), + }?; + + Some((str_timing, parsed_timing)) + } + + /// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333 + #[cfg(feature = "test_lib")] + macro_rules! assert_approx_eq { + ($a:expr, $b:expr) => {{ + let (a, b) = (&$a, &$b); + assert!( + (*a - *b).abs() < 1.0e-6, + "{} is not approximately equal to {}", + *a, + *b + ); + }}; + } + + #[cfg(feature = "test_lib")] + mod tests { + use super::parse_exec_time; + + #[test] + fn test_well_formed() { + let res = parse_exec_time( + &[ + "Part 1: 0 (74.13ns @ 100000 samples)".into(), + "Part 2: 10 (74.13ms @ 99999 samples)".into(), + "".into(), + ], + 1, + ); + assert_approx_eq!(res.total_nanos, 74130074.13_f64); + assert_eq!(res.part_1.unwrap(), "74.13ns"); + assert_eq!(res.part_2.unwrap(), "74.13ms"); + } + + #[test] + fn test_patterns_in_input() { + let res = parse_exec_time( + &[ + "Part 1: @ @ @ ( ) ms (2s @ 5 samples)".into(), + "Part 2: 10s (100ms @ 1 samples)".into(), + "".into(), + ], + 1, + ); + assert_approx_eq!(res.total_nanos, 2100000000_f64); + assert_eq!(res.part_1.unwrap(), "2s"); + assert_eq!(res.part_2.unwrap(), "100ms"); + } + + #[test] + fn test_missing_parts() { + let res = parse_exec_time( + &[ + "Part 1: βœ– ".into(), + "Part 2: βœ– ".into(), + "".into(), + ], + 1, + ); + assert_approx_eq!(res.total_nanos, 0_f64); + assert_eq!(res.part_1.is_none(), true); + assert_eq!(res.part_2.is_none(), true); + } + } +} diff --git a/src/template/commands/download.rs b/src/template/commands/download.rs new file mode 100644 index 0000000..bd8e2c7 --- /dev/null +++ b/src/template/commands/download.rs @@ -0,0 +1,14 @@ +use crate::template::aoc_cli; +use std::process; + +pub fn download_handler(day: u8) { + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + if let Err(e) = aoc_cli::download(day) { + eprintln!("failed to call aoc-cli: {}", e); + process::exit(1); + }; +} diff --git a/src/template/commands/mod.rs b/src/template/commands/mod.rs new file mode 100644 index 0000000..88f4696 --- /dev/null +++ b/src/template/commands/mod.rs @@ -0,0 +1,5 @@ +pub mod all; +pub mod download; +pub mod read; +pub mod scaffold; +pub mod solve; diff --git a/src/template/commands/read.rs b/src/template/commands/read.rs new file mode 100644 index 0000000..98810c8 --- /dev/null +++ b/src/template/commands/read.rs @@ -0,0 +1,15 @@ +use std::process; + +use crate::template::aoc_cli; + +pub fn read_handler(day: u8) { + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + if let Err(e) = aoc_cli::read(day) { + eprintln!("failed to call aoc-cli: {}", e); + process::exit(1); + }; +} diff --git a/src/bin/scaffold.rs b/src/template/commands/scaffold.rs similarity index 53% rename from src/bin/scaffold.rs rename to src/template/commands/scaffold.rs index 348f73b..8673a5e 100644 --- a/src/bin/scaffold.rs +++ b/src/template/commands/scaffold.rs @@ -1,7 +1,3 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ use std::{ fs::{File, OpenOptions}, io::Write, @@ -16,11 +12,7 @@ pub fn part_two(input: &str) -> Option { None } -fn main() { - let input = &advent_of_code::read_file("inputs", DAY); - advent_of_code::solve!(DAY, 1, part_one, input); - advent_of_code::solve!(DAY, 2, part_two, input); -} +advent_of_code::main!(DAY); #[cfg(test)] mod tests { @@ -28,23 +20,18 @@ mod tests { #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", DAY); - assert_eq!(part_one(&input), None); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", DAY); - assert_eq!(part_two(&input), None); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); } } "#; -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - args.free_from_str() -} - fn safe_create_file(path: &str) -> Result { OpenOptions::new().write(true).create_new(true).open(path) } @@ -53,25 +40,17 @@ fn create_file(path: &str) -> Result { OpenOptions::new().write(true).create(true).open(path) } -fn main() { - let day = match parse_args() { - Ok(day) => day, - Err(_) => { - eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`"); - process::exit(1); - } - }; - - let day_padded = format!("{day:02}"); +pub fn scaffold_handler(day: u8) { + let day_padded = format!("{:02}", day); - let input_path = format!("src/inputs/{day_padded}.txt"); - let example_path = format!("src/examples/{day_padded}.txt"); - let module_path = format!("src/bin/{day_padded}.rs"); + let input_path = format!("data/inputs/{}.txt", day_padded); + let example_path = format!("data/examples/{}.txt", day_padded); + let module_path = format!("src/bin/{}.rs", day_padded); let mut file = match safe_create_file(&module_path) { Ok(file) => file, Err(e) => { - eprintln!("Failed to create module file: {e}"); + eprintln!("Failed to create module file: {}", e); process::exit(1); } }; @@ -81,7 +60,7 @@ fn main() { println!("Created module file \"{}\"", &module_path); } Err(e) => { - eprintln!("Failed to write module contents: {e}"); + eprintln!("Failed to write module contents: {}", e); process::exit(1); } } @@ -91,7 +70,7 @@ fn main() { println!("Created empty input file \"{}\"", &input_path); } Err(e) => { - eprintln!("Failed to create input file: {e}"); + eprintln!("Failed to create input file: {}", e); process::exit(1); } } @@ -101,7 +80,7 @@ fn main() { println!("Created empty example file \"{}\"", &example_path); } Err(e) => { - eprintln!("Failed to create example file: {e}"); + eprintln!("Failed to create example file: {}", e); process::exit(1); } } diff --git a/src/template/commands/solve.rs b/src/template/commands/solve.rs new file mode 100644 index 0000000..b9a7d6a --- /dev/null +++ b/src/template/commands/solve.rs @@ -0,0 +1,31 @@ +use std::process::{Command, Stdio}; + +pub fn solve_handler(day: u8, release: bool, time: bool, submit_part: Option) { + let day_padded = format!("{:02}", day); + + let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded]; + + if release { + cmd_args.push("--release".to_string()); + } + + cmd_args.push("--".to_string()); + + if let Some(submit_part) = submit_part { + cmd_args.push("--submit".to_string()); + cmd_args.push(submit_part.to_string()) + } + + if time { + cmd_args.push("--time".to_string()); + } + + let mut cmd = Command::new("cargo") + .args(&cmd_args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .spawn() + .unwrap(); + + cmd.wait().unwrap(); +} diff --git a/src/template/mod.rs b/src/template/mod.rs new file mode 100644 index 0000000..16fb741 --- /dev/null +++ b/src/template/mod.rs @@ -0,0 +1,34 @@ +use std::{env, fs}; + +pub mod aoc_cli; +pub mod commands; +pub mod readme_benchmarks; +pub mod runner; + +pub const ANSI_ITALIC: &str = "\x1b[3m"; +pub const ANSI_BOLD: &str = "\x1b[1m"; +pub const ANSI_RESET: &str = "\x1b[0m"; + +/// Helper function that reads a text file to a string. +pub fn read_file(folder: &str, day: u8) -> String { + let cwd = env::current_dir().unwrap(); + let filepath = cwd + .join("data") + .join(folder) + .join(format!("{:02}.txt", day)); + let f = fs::read_to_string(filepath); + f.expect("could not open input file") +} + +/// main! produces a block setting up the input and runner for each part. +#[macro_export] +macro_rules! main { + ($day:expr) => { + fn main() { + use advent_of_code::template::runner::*; + let input = advent_of_code::template::read_file("inputs", $day); + run_part(part_one, &input, $day, 1); + run_part(part_two, &input, $day, 2); + } + }; +} diff --git a/src/template/readme_benchmarks.rs b/src/template/readme_benchmarks.rs new file mode 100644 index 0000000..49750c4 --- /dev/null +++ b/src/template/readme_benchmarks.rs @@ -0,0 +1,183 @@ +/// Module that updates the readme me with timing information. +/// The approach taken is similar to how `aoc-readme-stars` handles this. +use std::{fs, io}; + +static MARKER: &str = ""; + +#[derive(Debug)] +pub enum Error { + Parser(String), + IO(io::Error), +} + +impl From for Error { + fn from(e: std::io::Error) -> Self { + Error::IO(e) + } +} + +#[derive(Clone)] +pub struct Timings { + pub day: usize, + pub part_1: Option, + pub part_2: Option, + pub total_nanos: f64, +} + +pub struct TablePosition { + pos_start: usize, + pos_end: usize, +} + +pub fn get_path_for_bin(day: usize) -> String { + let day_padded = format!("{:02}", day); + format!("./src/bin/{}.rs", day_padded) +} + +fn locate_table(readme: &str) -> Result { + let matches: Vec<_> = readme.match_indices(MARKER).collect(); + + if matches.len() > 2 { + return Err(Error::Parser( + "{}: too many occurences of marker in README.".into(), + )); + } + + let pos_start = matches + .first() + .map(|m| m.0) + .ok_or_else(|| Error::Parser("Could not find table start position.".into()))?; + + let pos_end = matches + .last() + .map(|m| m.0 + m.1.len()) + .ok_or_else(|| Error::Parser("Could not find table end position.".into()))?; + + Ok(TablePosition { pos_start, pos_end }) +} + +fn construct_table(prefix: &str, timings: Vec, total_millis: f64) -> String { + let header = format!("{prefix} Benchmarks"); + + let mut lines: Vec = vec![ + MARKER.into(), + header, + "".into(), + "| Day | Part 1 | Part 2 |".into(), + "| :---: | :---: | :---: |".into(), + ]; + + timings.into_iter().for_each(|timing| { + let path = get_path_for_bin(timing.day); + lines.push(format!( + "| [Day {}]({}) | `{}` | `{}` |", + timing.day, + path, + timing.part_1.unwrap_or_else(|| "-".into()), + timing.part_2.unwrap_or_else(|| "-".into()) + )); + }); + + lines.push("".into()); + lines.push(format!("**Total: {:.2}ms**", total_millis)); + lines.push(MARKER.into()); + + lines.join("\n") +} + +fn update_content(s: &mut String, timings: Vec, total_millis: f64) -> Result<(), Error> { + let positions = locate_table(s)?; + let table = construct_table("##", timings, total_millis); + s.replace_range(positions.pos_start..positions.pos_end, &table); + Ok(()) +} + +pub fn update(timings: Vec, total_millis: f64) -> Result<(), Error> { + let path = "README.md"; + let mut readme = String::from_utf8_lossy(&fs::read(path)?).to_string(); + update_content(&mut readme, timings, total_millis)?; + fs::write(path, &readme)?; + Ok(()) +} + +#[cfg(feature = "test_lib")] +mod tests { + use super::{update_content, Timings, MARKER}; + + fn get_mock_timings() -> Vec { + vec![ + Timings { + day: 1, + part_1: Some("10ms".into()), + part_2: Some("20ms".into()), + total_nanos: 3e+10, + }, + Timings { + day: 2, + part_1: Some("30ms".into()), + part_2: Some("40ms".into()), + total_nanos: 7e+10, + }, + Timings { + day: 4, + part_1: Some("40ms".into()), + part_2: Some("50ms".into()), + total_nanos: 9e+10, + }, + ] + } + + #[test] + #[should_panic] + fn errors_if_marker_not_present() { + let mut s = "# readme".to_string(); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + } + + #[test] + #[should_panic] + fn errors_if_too_many_markers_present() { + let mut s = format!("{} {} {}", MARKER, MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + } + + #[test] + fn updates_empty_benchmarks() { + let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + assert_eq!(s.contains("## Benchmarks"), true); + } + + #[test] + fn updates_existing_benchmarks() { + let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + assert_eq!(s.matches(MARKER).collect::>().len(), 2); + assert_eq!(s.matches("## Benchmarks").collect::>().len(), 1); + } + + #[test] + fn format_benchmarks() { + let mut s = format!("foo\nbar\n{}\n{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + let expected = [ + "foo", + "bar", + "", + "## Benchmarks", + "", + "| Day | Part 1 | Part 2 |", + "| :---: | :---: | :---: |", + "| [Day 1](./src/bin/01.rs) | `10ms` | `20ms` |", + "| [Day 2](./src/bin/02.rs) | `30ms` | `40ms` |", + "| [Day 4](./src/bin/04.rs) | `40ms` | `50ms` |", + "", + "**Total: 190.00ms**", + "", + "baz", + ] + .join("\n"); + assert_eq!(s, expected); + } +} diff --git a/src/template/runner.rs b/src/template/runner.rs new file mode 100644 index 0000000..5e407eb --- /dev/null +++ b/src/template/runner.rs @@ -0,0 +1,165 @@ +/// Encapsulates code that interacts with solution functions. +use crate::template::{aoc_cli, ANSI_ITALIC, ANSI_RESET}; +use std::fmt::Display; +use std::io::{stdout, Write}; +use std::process::Output; +use std::time::{Duration, Instant}; +use std::{cmp, env, process}; + +use super::ANSI_BOLD; + +pub fn run_part(func: impl Fn(I) -> Option, input: I, day: u8, part: u8) { + let part_str = format!("Part {}", part); + + let (result, duration, samples) = + run_timed(func, input, |result| print_result(result, &part_str, "")); + + print_result(&result, &part_str, &format_duration(&duration, samples)); + + if let Some(result) = result { + submit_result(result, day, part); + } +} + +/// Run a solution part. The behavior differs depending on whether we are running a release or debug build: +/// 1. in debug, the function is executed once. +/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.) +fn run_timed( + func: impl Fn(I) -> T, + input: I, + hook: impl Fn(&T), +) -> (T, Duration, u128) { + let timer = Instant::now(); + let result = func(input.clone()); + let base_time = timer.elapsed(); + + hook(&result); + + let run = match std::env::args().any(|x| x == "--time") { + true => bench(func, input, &base_time), + false => (base_time, 1), + }; + + (result, run.0, run.1) +} + +fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) { + let mut stdout = stdout(); + + print!(" > {}benching{}", ANSI_ITALIC, ANSI_RESET); + let _ = stdout.flush(); + + let bench_iterations = cmp::min( + 10000, + cmp::max( + Duration::from_secs(1).as_nanos() / cmp::max(base_time.as_nanos(), 10), + 10, + ), + ); + + let mut timers: Vec = vec![]; + + for _ in 0..bench_iterations { + // need a clone here to make the borrow checker happy. + let cloned = input.clone(); + let timer = Instant::now(); + func(cloned); + timers.push(timer.elapsed()); + } + + ( + Duration::from_nanos(average_duration(&timers) as u64), + bench_iterations as u128, + ) +} + +fn average_duration(numbers: &[Duration]) -> u128 { + numbers.iter().map(|d| d.as_nanos()).sum::() / numbers.len() as u128 +} + +fn format_duration(duration: &Duration, samples: u128) -> String { + if samples == 1 { + format!(" ({:.1?})", duration) + } else { + format!(" ({:.1?} @ {} samples)", duration, samples) + } +} + +fn print_result(result: &Option, part: &str, duration_str: &str) { + let is_intermediate_result = duration_str.is_empty(); + + match result { + Some(result) => { + if result.to_string().contains('\n') { + let str = format!("{}: β–Ό {}", part, duration_str); + if is_intermediate_result { + print!("{}", str); + } else { + print!("\r"); + println!("{}", str); + println!("{}", result); + } + } else { + let str = format!( + "{}: {}{}{}{}", + part, ANSI_BOLD, result, ANSI_RESET, duration_str + ); + if is_intermediate_result { + print!("{}", str); + } else { + print!("\r"); + println!("{}", str); + } + } + } + None => { + if is_intermediate_result { + print!("{}: βœ–", part); + } else { + print!("\r"); + println!("{}: βœ– ", part); + } + } + } +} + +/// Parse the arguments passed to `solve` and try to submit one part of the solution if: +/// 1. we are in `--release` mode. +/// 2. aoc-cli is installed. +fn submit_result( + result: T, + day: u8, + part: u8, +) -> Option> { + let args: Vec = env::args().collect(); + + if !args.contains(&"--submit".into()) { + return None; + } + + if args.len() < 3 { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + } + + let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1; + let part_submit = match args[part_index].parse::() { + Ok(x) => x, + Err(_) => { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + } + }; + + if part_submit != part { + return None; + } + + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + println!("Submitting result via aoc-cli..."); + Some(aoc_cli::submit(day, part, &result.to_string())) +} From 1b10ee20acff1c0e3a3ed23c297f9da5fa049de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:22:31 +0200 Subject: [PATCH 12/18] ci: enable caching for cargo (#31) --- .github/workflows/ci.yml | 43 ++++++++++++++++++---------------------- src/template/runner.rs | 2 +- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cfbf87..5b7b012 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,33 +6,28 @@ env: CARGO_TERM_COLOR: always jobs: - check: - runs-on: ubuntu-latest - name: Check - steps: - - uses: actions/checkout@v3 - - name: cargo check - run: cargo check test: runs-on: ubuntu-latest - name: Test + name: CI steps: - uses: actions/checkout@v3 + - name: Set up cargo cache + uses: actions/cache@v3 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- - name: cargo test run: cargo test - # uncomment to enable clippy lints - # clippy: - # runs-on: ubuntu-latest - # name: Lint (clippy) - # steps: - # - uses: actions/checkout@v3 - # - name: cargo clippy - # run: cargo clippy -- -D warnings - # uncoment to enable format checking - # fmt: - # runs-on: ubuntu-latest - # name: Format - # steps: - # - uses: actions/checkout@v3 - # - name: cargo fmt - # run: cargo fmt --check +# uncomment to enable clippy linter + # - name: cargo clippy + # run: cargo clippy -- -D warnings +# uncomment to enable format linter + # - name: cargo fmt + # run: cargo fmt --check diff --git a/src/template/runner.rs b/src/template/runner.rs index 5e407eb..5ccde99 100644 --- a/src/template/runner.rs +++ b/src/template/runner.rs @@ -69,7 +69,7 @@ fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> ( Duration::from_nanos(average_duration(&timers) as u64), - bench_iterations as u128, + bench_iterations, ) } From c11ac6125221ec4545a78065f27be6bd95263b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:23:46 +0200 Subject: [PATCH 13/18] ci: fixup --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b7b012..9eef218 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,9 +25,9 @@ jobs: restore-keys: ${{ runner.os }}-cargo- - name: cargo test run: cargo test -# uncomment to enable clippy linter + # uncomment to enable clippy linter # - name: cargo clippy # run: cargo clippy -- -D warnings -# uncomment to enable format linter + # uncomment to enable format linter # - name: cargo fmt # run: cargo fmt --check From 83e229c7df315f37134b69f5a74b46c257ed3ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 21 Oct 2023 21:54:22 +0200 Subject: [PATCH 14/18] fix: check in updated file layout --- .gitignore | 10 ++++++---- {src => data}/examples/.keep | 0 {src => data}/inputs/.keep | 0 data/puzzles/.keep | 0 4 files changed, 6 insertions(+), 4 deletions(-) rename {src => data}/examples/.keep (100%) rename {src => data}/inputs/.keep (100%) create mode 100644 data/puzzles/.keep diff --git a/.gitignore b/.gitignore index db1d6f2..3d20b31 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,9 @@ target/ # Advent of Code # @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3 -/data -!/data/inputs/.keep -!/data/examples/.keep -!/data/puzzles/.keep + +data + +!data/inputs/.keep +!data/examples/.keep +!data/puzzles/.keep diff --git a/src/examples/.keep b/data/examples/.keep similarity index 100% rename from src/examples/.keep rename to data/examples/.keep diff --git a/src/inputs/.keep b/data/inputs/.keep similarity index 100% rename from src/inputs/.keep rename to data/inputs/.keep diff --git a/data/puzzles/.keep b/data/puzzles/.keep new file mode 100644 index 0000000..e69de29 From f46d1e209c4770565063938e16437843e4faca4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:03:48 +0100 Subject: [PATCH 15/18] docs: clarify `solve` command * closes #33 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ec8963c..e57d968 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,11 @@ cargo solve # Part 2: 42 (41.0ns) ``` -The `solve` command runs your solution. If you set the `--release` flag, real puzzle _inputs_ will be passed to your solution, otherwise the _example_ inputs will be used. +The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program. -If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times - depending on execution time of first execution - and print the average execution time. +By default, `solve` executes your code once and shows the execution time. If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times (depending on execution time of first execution) and print the average execution time. -For example, a benchmarked execution against real inputs of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads. +For example, running a benchmarked, optimized execution of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads. #### Submitting solutions From d7af3dca9e52f9868b346d4058fe68248fb8950b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:46:31 +0100 Subject: [PATCH 16/18] refactor: fix most pedantic clippy warnings closes #29 Co-authored-by: Andy Pymont --- src/main.rs | 41 +++++++++++------------ src/template/aoc_cli.rs | 36 ++++++++++---------- src/template/commands/all.rs | 35 +++++++++----------- src/template/commands/download.rs | 4 +-- src/template/commands/read.rs | 4 +-- src/template/commands/scaffold.rs | 20 +++++------ src/template/commands/solve.rs | 6 ++-- src/template/mod.rs | 4 +-- src/template/readme_benchmarks.rs | 16 ++++----- src/template/runner.rs | 55 ++++++++++++++++--------------- 10 files changed, 107 insertions(+), 114 deletions(-) diff --git a/src/main.rs b/src/main.rs index e4750f5..91a042c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ -use advent_of_code::template::commands::{ - all::all_handler, download::download_handler, read::read_handler, scaffold::scaffold_handler, - solve::solve_handler, -}; -use args::{parse_args, AppArgs}; +use advent_of_code::template::commands::{all, download, read, scaffold, solve}; +use args::{parse, AppArguments}; mod args { use std::process; - pub enum AppArgs { + pub enum AppArguments { Download { day: u8, }, @@ -29,31 +26,31 @@ mod args { }, } - pub fn parse_args() -> Result> { + pub fn parse() -> Result> { let mut args = pico_args::Arguments::from_env(); let app_args = match args.subcommand()?.as_deref() { - Some("all") => AppArgs::All { + Some("all") => AppArguments::All { release: args.contains("--release"), time: args.contains("--time"), }, - Some("download") => AppArgs::Download { + Some("download") => AppArguments::Download { day: args.free_from_str()?, }, - Some("read") => AppArgs::Read { + Some("read") => AppArguments::Read { day: args.free_from_str()?, }, - Some("scaffold") => AppArgs::Scaffold { + Some("scaffold") => AppArguments::Scaffold { day: args.free_from_str()?, }, - Some("solve") => AppArgs::Solve { + Some("solve") => AppArguments::Solve { day: args.free_from_str()?, release: args.contains("--release"), submit: args.opt_value_from_str("--submit")?, time: args.contains("--time"), }, Some(x) => { - eprintln!("Unknown command: {}", x); + eprintln!("Unknown command: {x}"); process::exit(1); } None => { @@ -64,7 +61,7 @@ mod args { let remaining = args.finish(); if !remaining.is_empty() { - eprintln!("Warning: unknown argument(s): {:?}.", remaining); + eprintln!("Warning: unknown argument(s): {remaining:?}."); } Ok(app_args) @@ -72,22 +69,22 @@ mod args { } fn main() { - match parse_args() { + match parse() { Err(err) => { - eprintln!("Error: {}", err); + eprintln!("Error: {err}"); std::process::exit(1); } Ok(args) => match args { - AppArgs::All { release, time } => all_handler(release, time), - AppArgs::Download { day } => download_handler(day), - AppArgs::Read { day } => read_handler(day), - AppArgs::Scaffold { day } => scaffold_handler(day), - AppArgs::Solve { + AppArguments::All { release, time } => all::handle(release, time), + AppArguments::Download { day } => download::handle(day), + AppArguments::Read { day } => read::handle(day), + AppArguments::Scaffold { day } => scaffold::handle(day), + AppArguments::Solve { day, release, time, submit, - } => solve_handler(day, release, time, submit), + } => solve::handle(day, release, time, submit), }, }; } diff --git a/src/template/aoc_cli.rs b/src/template/aoc_cli.rs index 31ac7ac..08ce9fe 100644 --- a/src/template/aoc_cli.rs +++ b/src/template/aoc_cli.rs @@ -5,35 +5,35 @@ use std::{ }; #[derive(Debug)] -pub enum AocCliError { +pub enum AocCommandError { CommandNotFound, CommandNotCallable, BadExitStatus(Output), IoError, } -impl Display for AocCliError { +impl Display for AocCommandError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), - AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), - AocCliError::BadExitStatus(_) => { + AocCommandError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), + AocCommandError::CommandNotCallable => write!(f, "aoc-cli could not be called."), + AocCommandError::BadExitStatus(_) => { write!(f, "aoc-cli exited with a non-zero status.") } - AocCliError::IoError => write!(f, "could not write output files to file system."), + AocCommandError::IoError => write!(f, "could not write output files to file system."), } } } -pub fn check() -> Result<(), AocCliError> { +pub fn check() -> Result<(), AocCommandError> { Command::new("aoc") .arg("-V") .output() - .map_err(|_| AocCliError::CommandNotFound)?; + .map_err(|_| AocCommandError::CommandNotFound)?; Ok(()) } -pub fn read(day: u8) -> Result { +pub fn read(day: u8) -> Result { let puzzle_path = get_puzzle_path(day); let args = build_args( @@ -49,7 +49,7 @@ pub fn read(day: u8) -> Result { call_aoc_cli(&args) } -pub fn download(day: u8) -> Result { +pub fn download(day: u8) -> Result { let input_path = get_input_path(day); let puzzle_path = get_puzzle_path(day); @@ -72,7 +72,7 @@ pub fn download(day: u8) -> Result { Ok(output) } -pub fn submit(day: u8, part: u8, result: &str) -> Result { +pub fn submit(day: u8, part: u8, result: &str) -> Result { // workaround: the argument order is inverted for submit. let mut args = build_args("submit", &[], day); args.push(part.to_string()); @@ -81,13 +81,13 @@ pub fn submit(day: u8, part: u8, result: &str) -> Result { } fn get_input_path(day: u8) -> String { - let day_padded = format!("{:02}", day); - format!("data/inputs/{}.txt", day_padded) + let day_padded = format!("{day:02}"); + format!("data/inputs/{day_padded}.txt") } fn get_puzzle_path(day: u8) -> String { - let day_padded = format!("{:02}", day); - format!("data/puzzles/{}.md", day_padded) + let day_padded = format!("{day:02}"); + format!("data/puzzles/{day_padded}.md") } fn get_year() -> Option { @@ -110,18 +110,18 @@ fn build_args(command: &str, args: &[String], day: u8) -> Vec { cmd_args } -fn call_aoc_cli(args: &[String]) -> Result { +fn call_aoc_cli(args: &[String]) -> Result { // println!("Calling >aoc with: {}", args.join(" ")); let output = Command::new("aoc") .args(args) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .output() - .map_err(|_| AocCliError::CommandNotCallable)?; + .map_err(|_| AocCommandError::CommandNotCallable)?; if output.status.success() { Ok(output) } else { - Err(AocCliError::BadExitStatus(output)) + Err(AocCommandError::BadExitStatus(output)) } } diff --git a/src/template/commands/all.rs b/src/template/commands/all.rs index 87fc2c9..7214b4a 100644 --- a/src/template/commands/all.rs +++ b/src/template/commands/all.rs @@ -5,7 +5,7 @@ use crate::template::{ ANSI_BOLD, ANSI_ITALIC, ANSI_RESET, }; -pub fn all_handler(is_release: bool, is_timed: bool) { +pub fn handle(is_release: bool, is_timed: bool) { let mut timings: Vec = vec![]; (1..=25).for_each(|day| { @@ -13,7 +13,7 @@ pub fn all_handler(is_release: bool, is_timed: bool) { println!(); } - println!("{}Day {}{}", ANSI_BOLD, day, ANSI_RESET); + println!("{ANSI_BOLD}Day {day}{ANSI_RESET}"); println!("------"); let output = child_commands::run_solution(day, is_timed, is_release).unwrap(); @@ -27,16 +27,13 @@ pub fn all_handler(is_release: bool, is_timed: bool) { }); if is_timed { - let total_millis = timings.iter().map(|x| x.total_nanos).sum::() / 1000000_f64; + let total_millis = timings.iter().map(|x| x.total_nanos).sum::() / 1_000_000_f64; - println!( - "\n{}Total:{} {}{:.2}ms{}", - ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total_millis, ANSI_RESET - ); + println!("\n{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total_millis:.2}ms{ANSI_RESET}"); if is_release { match readme_benchmarks::update(timings, total_millis) { - Ok(_) => println!("Successfully updated README with benchmarks."), + Ok(()) => println!("Successfully updated README with benchmarks."), Err(_) => { eprintln!("Failed to update readme with benchmarks."); } @@ -58,9 +55,10 @@ impl From for Error { } } +#[must_use] pub fn get_path_for_bin(day: usize) -> String { - let day_padded = format!("{:02}", day); - format!("./src/bin/{}.rs", day_padded) + let day_padded = format!("{day:02}"); + format!("./src/bin/{day_padded}.rs") } /// All solutions live in isolated binaries. @@ -80,7 +78,7 @@ mod child_commands { is_timed: bool, is_release: bool, ) -> Result, Error> { - let day_padded = format!("{:02}", day); + let day_padded = format!("{day:02}"); // skip command invocation for days that have not been scaffolded yet. if !Path::new(&get_path_for_bin(day)).exists() { @@ -121,7 +119,7 @@ mod child_commands { for line in stdout.lines() { let line = line.unwrap(); - println!("{}", line); + println!("{line}"); output.push(line); } @@ -146,12 +144,9 @@ mod child_commands { return None; } - let (timing_str, nanos) = match parse_time(l) { - Some(v) => v, - None => { - eprintln!("Could not parse timings from line: {l}"); - return None; - } + let Some((timing_str, nanos)) = parse_time(l) else { + eprintln!("Could not parse timings from line: {l}"); + return None; }; let part = l.split(':').next()?; @@ -188,8 +183,8 @@ mod child_commands { let parsed_timing = match str_timing { s if s.contains("ns") => s.split("ns").next()?.parse::().ok(), s if s.contains("Β΅s") => parse_to_float(s, "Β΅s").map(|x| x * 1000_f64), - s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1000000_f64), - s => parse_to_float(s, "s").map(|x| x * 1000000000_f64), + s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1_000_000_f64), + s => parse_to_float(s, "s").map(|x| x * 1_000_000_000_f64), }?; Some((str_timing, parsed_timing)) diff --git a/src/template/commands/download.rs b/src/template/commands/download.rs index bd8e2c7..56beead 100644 --- a/src/template/commands/download.rs +++ b/src/template/commands/download.rs @@ -1,14 +1,14 @@ use crate::template::aoc_cli; use std::process; -pub fn download_handler(day: u8) { +pub fn handle(day: u8) { if aoc_cli::check().is_err() { eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); process::exit(1); } if let Err(e) = aoc_cli::download(day) { - eprintln!("failed to call aoc-cli: {}", e); + eprintln!("failed to call aoc-cli: {e}"); process::exit(1); }; } diff --git a/src/template/commands/read.rs b/src/template/commands/read.rs index 98810c8..65edcde 100644 --- a/src/template/commands/read.rs +++ b/src/template/commands/read.rs @@ -2,14 +2,14 @@ use std::process; use crate::template::aoc_cli; -pub fn read_handler(day: u8) { +pub fn handle(day: u8) { if aoc_cli::check().is_err() { eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); process::exit(1); } if let Err(e) = aoc_cli::read(day) { - eprintln!("failed to call aoc-cli: {}", e); + eprintln!("failed to call aoc-cli: {e}"); process::exit(1); }; } diff --git a/src/template/commands/scaffold.rs b/src/template/commands/scaffold.rs index 8673a5e..6a3d9a1 100644 --- a/src/template/commands/scaffold.rs +++ b/src/template/commands/scaffold.rs @@ -40,27 +40,27 @@ fn create_file(path: &str) -> Result { OpenOptions::new().write(true).create(true).open(path) } -pub fn scaffold_handler(day: u8) { - let day_padded = format!("{:02}", day); +pub fn handle(day: u8) { + let day_padded = format!("{day:02}"); - let input_path = format!("data/inputs/{}.txt", day_padded); - let example_path = format!("data/examples/{}.txt", day_padded); - let module_path = format!("src/bin/{}.rs", day_padded); + let input_path = format!("data/inputs/{day_padded}.txt"); + let example_path = format!("data/examples/{day_padded}.txt"); + let module_path = format!("src/bin/{day_padded}.rs"); let mut file = match safe_create_file(&module_path) { Ok(file) => file, Err(e) => { - eprintln!("Failed to create module file: {}", e); + eprintln!("Failed to create module file: {e}"); process::exit(1); } }; match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) { - Ok(_) => { + Ok(()) => { println!("Created module file \"{}\"", &module_path); } Err(e) => { - eprintln!("Failed to write module contents: {}", e); + eprintln!("Failed to write module contents: {e}"); process::exit(1); } } @@ -70,7 +70,7 @@ pub fn scaffold_handler(day: u8) { println!("Created empty input file \"{}\"", &input_path); } Err(e) => { - eprintln!("Failed to create input file: {}", e); + eprintln!("Failed to create input file: {e}"); process::exit(1); } } @@ -80,7 +80,7 @@ pub fn scaffold_handler(day: u8) { println!("Created empty example file \"{}\"", &example_path); } Err(e) => { - eprintln!("Failed to create example file: {}", e); + eprintln!("Failed to create example file: {e}"); process::exit(1); } } diff --git a/src/template/commands/solve.rs b/src/template/commands/solve.rs index b9a7d6a..8c65702 100644 --- a/src/template/commands/solve.rs +++ b/src/template/commands/solve.rs @@ -1,7 +1,7 @@ use std::process::{Command, Stdio}; -pub fn solve_handler(day: u8, release: bool, time: bool, submit_part: Option) { - let day_padded = format!("{:02}", day); +pub fn handle(day: u8, release: bool, time: bool, submit_part: Option) { + let day_padded = format!("{day:02}"); let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded]; @@ -13,7 +13,7 @@ pub fn solve_handler(day: u8, release: bool, time: bool, submit_part: Option if let Some(submit_part) = submit_part { cmd_args.push("--submit".to_string()); - cmd_args.push(submit_part.to_string()) + cmd_args.push(submit_part.to_string()); } if time { diff --git a/src/template/mod.rs b/src/template/mod.rs index 16fb741..faddf0a 100644 --- a/src/template/mod.rs +++ b/src/template/mod.rs @@ -10,12 +10,12 @@ pub const ANSI_BOLD: &str = "\x1b[1m"; pub const ANSI_RESET: &str = "\x1b[0m"; /// Helper function that reads a text file to a string. -pub fn read_file(folder: &str, day: u8) -> String { +#[must_use] pub fn read_file(folder: &str, day: u8) -> String { let cwd = env::current_dir().unwrap(); let filepath = cwd .join("data") .join(folder) - .join(format!("{:02}.txt", day)); + .join(format!("{day:02}.txt")); let f = fs::read_to_string(filepath); f.expect("could not open input file") } diff --git a/src/template/readme_benchmarks.rs b/src/template/readme_benchmarks.rs index 49750c4..6f11b5a 100644 --- a/src/template/readme_benchmarks.rs +++ b/src/template/readme_benchmarks.rs @@ -29,9 +29,9 @@ pub struct TablePosition { pos_end: usize, } -pub fn get_path_for_bin(day: usize) -> String { - let day_padded = format!("{:02}", day); - format!("./src/bin/{}.rs", day_padded) +#[must_use] pub fn get_path_for_bin(day: usize) -> String { + let day_padded = format!("{day:02}"); + format!("./src/bin/{day_padded}.rs") } fn locate_table(readme: &str) -> Result { @@ -62,12 +62,12 @@ fn construct_table(prefix: &str, timings: Vec, total_millis: f64) -> St let mut lines: Vec = vec![ MARKER.into(), header, - "".into(), + String::new(), "| Day | Part 1 | Part 2 |".into(), "| :---: | :---: | :---: |".into(), ]; - timings.into_iter().for_each(|timing| { + for timing in timings { let path = get_path_for_bin(timing.day); lines.push(format!( "| [Day {}]({}) | `{}` | `{}` |", @@ -76,10 +76,10 @@ fn construct_table(prefix: &str, timings: Vec, total_millis: f64) -> St timing.part_1.unwrap_or_else(|| "-".into()), timing.part_2.unwrap_or_else(|| "-".into()) )); - }); + } - lines.push("".into()); - lines.push(format!("**Total: {:.2}ms**", total_millis)); + lines.push(String::new()); + lines.push(format!("**Total: {total_millis:.2}ms**")); lines.push(MARKER.into()); lines.join("\n") diff --git a/src/template/runner.rs b/src/template/runner.rs index 5ccde99..6557ee5 100644 --- a/src/template/runner.rs +++ b/src/template/runner.rs @@ -9,7 +9,7 @@ use std::{cmp, env, process}; use super::ANSI_BOLD; pub fn run_part(func: impl Fn(I) -> Option, input: I, day: u8, part: u8) { - let part_str = format!("Part {}", part); + let part_str = format!("Part {part}"); let (result, duration, samples) = run_timed(func, input, |result| print_result(result, &part_str, "")); @@ -35,9 +35,10 @@ fn run_timed( hook(&result); - let run = match std::env::args().any(|x| x == "--time") { - true => bench(func, input, &base_time), - false => (base_time, 1), + let run = if std::env::args().any(|x| x == "--time") { + bench(func, input, &base_time) + } else { + (base_time, 1) }; (result, run.0, run.1) @@ -46,7 +47,7 @@ fn run_timed( fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) { let mut stdout = stdout(); - print!(" > {}benching{}", ANSI_ITALIC, ANSI_RESET); + print!(" > {ANSI_ITALIC}benching{ANSI_RESET}"); let _ = stdout.flush(); let bench_iterations = cmp::min( @@ -68,20 +69,25 @@ fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> } ( + #[allow(clippy::cast_possible_truncation)] Duration::from_nanos(average_duration(&timers) as u64), bench_iterations, ) } fn average_duration(numbers: &[Duration]) -> u128 { - numbers.iter().map(|d| d.as_nanos()).sum::() / numbers.len() as u128 + numbers + .iter() + .map(std::time::Duration::as_nanos) + .sum::() + / numbers.len() as u128 } fn format_duration(duration: &Duration, samples: u128) -> String { if samples == 1 { - format!(" ({:.1?})", duration) + format!(" ({duration:.1?})") } else { - format!(" ({:.1?} @ {} samples)", duration, samples) + format!(" ({duration:.1?} @ {samples} samples)") } } @@ -91,33 +97,30 @@ fn print_result(result: &Option, part: &str, duration_str: &str) match result { Some(result) => { if result.to_string().contains('\n') { - let str = format!("{}: β–Ό {}", part, duration_str); + let str = format!("{part}: β–Ό {duration_str}"); if is_intermediate_result { - print!("{}", str); + print!("{str}"); } else { print!("\r"); - println!("{}", str); - println!("{}", result); + println!("{str}"); + println!("{result}"); } } else { - let str = format!( - "{}: {}{}{}{}", - part, ANSI_BOLD, result, ANSI_RESET, duration_str - ); + let str = format!("{part}: {ANSI_BOLD}{result}{ANSI_RESET}{duration_str}"); if is_intermediate_result { - print!("{}", str); + print!("{str}"); } else { print!("\r"); - println!("{}", str); + println!("{str}"); } } } None => { if is_intermediate_result { - print!("{}: βœ–", part); + print!("{part}: βœ–"); } else { print!("\r"); - println!("{}: βœ– ", part); + println!("{part}: βœ– "); } } } @@ -130,7 +133,7 @@ fn submit_result( result: T, day: u8, part: u8, -) -> Option> { +) -> Option> { let args: Vec = env::args().collect(); if !args.contains(&"--submit".into()) { @@ -143,12 +146,10 @@ fn submit_result( } let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1; - let part_submit = match args[part_index].parse::() { - Ok(x) => x, - Err(_) => { - eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); - process::exit(1); - } + + let Ok(part_submit) = args[part_index].parse::() else { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); }; if part_submit != part { From 6708184f1b10d1080d05aa313e20ad600e686273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Wed, 1 Nov 2023 10:45:55 +0100 Subject: [PATCH 17/18] docs: improve wording (#34) --- README.md | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e57d968..8f3abb1 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Tip: when editing a solution, `rust-analyzer` will display buttons for running / ### Download input & description for a day > **Note** -> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh # example: `cargo download 1` @@ -93,7 +93,7 @@ For example, running a benchmarked, optimized execution of day 1 would look like #### Submitting solutions > **Note** -> This requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). In order to submit part of a solution for checking, append the `--submit ` option to the `solve` command. @@ -140,19 +140,11 @@ cargo fmt ```sh cargo clippy ``` -## Optional template features - -### Download puzzle inputs via aoc-cli - -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` -2. Create an `.adventofcode.session` file in your home directory and paste your session cookie. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1] - -Once installed, you can use the [download command](#download-input--description-for-a-day). ### Read puzzle description in terminal > **Note** -> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh # example: `cargo read 1` @@ -164,13 +156,14 @@ cargo read # ...the input... ``` -### Check code formatting in CI +## Optional template features -Uncomment the `format` job in the `ci.yml` workflow to enable fmt checks in CI. +### Configure aoc-cli integration -### Enable clippy lints in CI +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` +2. Create an `.adventofcode.session` file in your home directory and paste your session cookie. To retrieve the session cookie, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1] -Uncomment the `clippy` job in the `ci.yml` workflow to enable clippy checks in CI. +Once installed, you can use the [download command](#download-input--description-for-a-day) and automatically submit solutions via the [`--submit` flag](#submitting-solutions). ### Automatically track ⭐️ progress in the readme @@ -187,12 +180,16 @@ Go to the leaderboard page of the year you want to track and click _Private Lead Go to the _Secrets_ tab in your repository settings and create the following secrets: - `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker. -- `AOC_USER_ID`: Go to [this page](https://adventofcode.com/settings) and copy your user id. It's the number behind the `#` symbol in the first name option. Example: `3031` -- `AOC_YEAR`: the year you want to track. Example: `2021` +- `AOC_USER_ID`: Go to [this page](https://adventofcode.com/settings) and copy your user id. It's the number behind the `#` symbol in the first name option. Example: `3031`. +- `AOC_YEAR`: the year you want to track. Example: `2021`. - `AOC_SESSION`: an active session[^2] for the advent of code website. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie. ✨ You can now run this action manually via the _Run workflow_ button on the workflow page. If you want the workflow to run automatically, uncomment the `schedule` section in the `readme-stars.yml` workflow file or add a `push` trigger. +### Check code formatting / clippy lints in CI + +Uncomment the respective sections in the `ci.yml` workflow. + ### Use VS Code to debug your code 1. Install [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) and [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb). From 6653e856a6e43f148e8aaf408681cae07d47c5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Wed, 1 Nov 2023 10:48:09 +0100 Subject: [PATCH 18/18] chore(release): bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a53d473..895fa26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "advent_of_code" -version = "0.8.0" +version = "0.9.0" dependencies = [ "pico-args", ] diff --git a/Cargo.toml b/Cargo.toml index a871f8d..fc2d1f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent_of_code" -version = "0.8.0" +version = "0.9.0" authors = ["Felix SpΓΆttel <1682504+fspoettel@users.noreply.github.com>"] edition = "2021" default-run = "advent_of_code"