diff --git a/.cargo/config.toml b/.cargo/config.toml index 754f083..3530669 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -8,4 +8,4 @@ all = "run --quiet --release -- all" time = "run --quiet --release -- all --release --time" [env] -AOC_YEAR = "2022" +AOC_YEAR = "2023" diff --git a/.gitignore b/.gitignore index 3d20b31..e3b4aa2 100644 --- a/.gitignore +++ b/.gitignore @@ -17,8 +17,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 -data - +data/inputs/* !data/inputs/.keep -!data/examples/.keep +data/puzzles/* !data/puzzles/.keep diff --git a/Cargo.lock b/Cargo.lock index e152003..ea86ba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "advent_of_code" -version = "0.9.2" +version = "0.9.3" dependencies = [ "pico-args", ] diff --git a/Cargo.toml b/Cargo.toml index ac5c09e..692fc58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent_of_code" -version = "0.9.2" +version = "0.9.3" authors = ["Felix Spöttel <1682504+fspoettel@users.noreply.github.com>"] edition = "2021" default-run = "advent_of_code" diff --git a/README.md b/README.md index 3908e78..85aba56 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,17 @@ cargo scaffold 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 solutions against the example input. +Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template/commands/scaffold.rs#L9-L35) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input. -Tip: when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks. +> [!TIP] +> If a day has different example inputs for both parts, you can use the `read_file_part()` helper in your tests instead of `read_file()`. For example, if this applies to day 1, you can create a second example file `01-2.txt` and invoke the helper like `let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));` to read it in `test_part_two`. + +> [!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 -> **Note** +> [!IMPORTANT] > This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh @@ -92,7 +96,7 @@ For example, running a benchmarked, optimized execution of day 1 would look like #### Submitting solutions -> **Note** +> [!IMPORTANT] > 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. @@ -113,13 +117,13 @@ cargo all # Total: 0.20ms ``` -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. +This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build. #### Update readme benchmarks -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. 😉 +The template can output a table with solution times to your readme. 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 and the readme will be updated. -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. +Please note that these are not "scientific" benchmarks, understand them as a fun approximation. 😉 Timings, especially in the microseconds range, might change a bit between invocations. ### Run all tests @@ -143,7 +147,7 @@ cargo clippy ### Read puzzle description in terminal -> **Note** +> [!IMPORTANT] > This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh @@ -163,7 +167,7 @@ cargo read 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] -Once installed, you can use the [download command](#download-input--description-for-a-day) and automatically submit solutions via the [`--submit` flag](#submitting-solutions). +Once installed, you can use the [download command](#download-input--description-for-a-day), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions). ### Automatically track ⭐️ progress in the readme @@ -185,7 +189,7 @@ Go to the _Secrets_ tab in your repository settings and create the following sec Go to the _Variables_ tab in your repository settings and create the following variable: -- `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker. +- `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker. After you complete AoC or no longer work on it, you can set this to `false` to disable the CI. ✨ 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. diff --git a/src/template/mod.rs b/src/template/mod.rs index 4403b95..d1533e1 100644 --- a/src/template/mod.rs +++ b/src/template/mod.rs @@ -19,6 +19,18 @@ pub fn read_file(folder: &str, day: Day) -> String { f.expect("could not open input file") } +/// Helper function that reads a text file to string, appending a part suffix. E.g. like `01-2.txt`. +#[must_use] +pub fn read_file_part(folder: &str, day: Day, part: u8) -> String { + let cwd = env::current_dir().unwrap(); + let filepath = cwd + .join("data") + .join(folder) + .join(format!("{day}-{part}.txt")); + let f = fs::read_to_string(filepath); + f.expect("could not open input file") +} + /// Creates the constant `DAY` and sets up the input and runner for each part. #[macro_export] macro_rules! solution {