From 6d46832140a0beb98b01ddb342314057fc922e69 Mon Sep 17 00:00:00 2001 From: Gregor Majcen Date: Wed, 29 Nov 2023 15:32:32 +0100 Subject: [PATCH] update template --- .cargo/config | 7 - .cargo/config.toml | 11 + .github/workflows/ci.yml | 31 ++- .github/workflows/readme-stars.yml | 16 +- .gitignore | 8 +- Cargo.lock | 22 +- Cargo.toml | 10 +- README.md | 146 +++++++----- {src => data}/examples/.keep | 0 {src => data}/examples/01.txt | 0 {src => data}/examples/02.txt | 0 {src => data}/examples/03.txt | 0 {src => data}/examples/04.txt | 0 {src => data}/examples/05.txt | 0 {src => data}/examples/06.txt | 0 {src => data}/examples/07.txt | 0 {src => data}/examples/08.txt | 0 {src => data}/examples/09.txt | 0 {src => data}/examples/10.txt | 0 {src => data}/examples/11.txt | 0 {src => data}/examples/12.txt | 0 {src => data}/examples/13.txt | 0 {src => data}/examples/14.txt | 0 {src => data}/examples/15.txt | 0 {src => data}/examples/16.txt | 0 {src => data}/examples/17.txt | 0 {src => data}/examples/18.txt | 0 {src => data}/examples/19.txt | 0 {src => data}/examples/20.txt | 0 {src => data}/examples/21.txt | 0 {src => data}/examples/22.txt | 0 {src => data}/examples/23.txt | 0 {src => data}/examples/24.txt | 0 {src => data}/examples/25.txt | 0 src/{inputs => bin}/.keep | 0 src/bin/01.rs | 16 +- src/bin/02.rs | 16 +- src/bin/03.rs | 16 +- src/bin/04.rs | 16 +- src/bin/05.rs | 16 +- src/bin/06.rs | 16 +- src/bin/07.rs | 16 +- src/bin/08.rs | 16 +- src/bin/09.rs | 16 +- src/bin/10.rs | 18 +- src/bin/11.rs | 16 +- src/bin/12.rs | 16 +- src/bin/13.rs | 16 +- src/bin/14.rs | 16 +- src/bin/15.rs | 16 +- src/bin/16.rs | 18 +- src/bin/17.rs | 16 +- src/bin/18.rs | 16 +- src/bin/19.rs | 16 +- src/bin/20.rs | 16 +- src/bin/21.rs | 16 +- src/bin/22.rs | 16 +- src/bin/23.rs | 16 +- src/bin/24.rs | 16 +- src/bin/25.rs | 16 +- src/bin/download.rs | 46 ---- src/bin/read.rs | 46 ---- src/day.rs | 172 ++++++++++++++ src/helpers.rs | 4 - src/lib.rs | 236 +------------------ src/main.rs | 119 +++++++--- src/template/aoc_cli.rs | 127 +++++++++++ src/template/commands/all.rs | 254 +++++++++++++++++++++ src/template/commands/download.rs | 15 ++ src/template/commands/mod.rs | 5 + src/template/commands/read.rs | 16 ++ src/{bin => template/commands}/scaffold.rs | 63 ++--- src/template/commands/solve.rs | 31 +++ src/template/mod.rs | 36 +++ src/template/readme_benchmarks.rs | 186 +++++++++++++++ src/template/runner.rs | 167 ++++++++++++++ 76 files changed, 1410 insertions(+), 768 deletions(-) delete mode 100644 .cargo/config create mode 100644 .cargo/config.toml rename {src => data}/examples/.keep (100%) rename {src => data}/examples/01.txt (100%) rename {src => data}/examples/02.txt (100%) rename {src => data}/examples/03.txt (100%) rename {src => data}/examples/04.txt (100%) rename {src => data}/examples/05.txt (100%) rename {src => data}/examples/06.txt (100%) rename {src => data}/examples/07.txt (100%) rename {src => data}/examples/08.txt (100%) rename {src => data}/examples/09.txt (100%) rename {src => data}/examples/10.txt (100%) rename {src => data}/examples/11.txt (100%) rename {src => data}/examples/12.txt (100%) rename {src => data}/examples/13.txt (100%) rename {src => data}/examples/14.txt (100%) rename {src => data}/examples/15.txt (100%) rename {src => data}/examples/16.txt (100%) rename {src => data}/examples/17.txt (100%) rename {src => data}/examples/18.txt (100%) rename {src => data}/examples/19.txt (100%) rename {src => data}/examples/20.txt (100%) rename {src => data}/examples/21.txt (100%) rename {src => data}/examples/22.txt (100%) rename {src => data}/examples/23.txt (100%) rename {src => data}/examples/24.txt (100%) rename {src => data}/examples/25.txt (100%) rename src/{inputs => bin}/.keep (100%) delete mode 100644 src/bin/download.rs delete mode 100644 src/bin/read.rs create mode 100644 src/day.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 (51%) 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 b/.cargo/config deleted file mode 100644 index 03ad300..0000000 --- a/.cargo/config +++ /dev/null @@ -1,7 +0,0 @@ -[alias] -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/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..754f083 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,11 @@ +[alias] +scaffold = "run --quiet --release -- scaffold" +download = "run --quiet --release -- download" +read = "run --quiet --release -- read" + +solve = "run --quiet --release -- solve" +all = "run --quiet --release -- all" +time = "run --quiet --release -- all --release --time" + +[env] +AOC_YEAR = "2022" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc2839b..4e613d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,31 +6,26 @@ 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 - clippy: - runs-on: ubuntu-latest - name: Lint (clippy) - steps: - - uses: actions/checkout@v3 - name: cargo clippy run: cargo clippy -- -D warnings - fmt: - runs-on: ubuntu-latest - name: Format - steps: - - uses: actions/checkout@v3 - name: cargo fmt run: cargo fmt --check diff --git a/.github/workflows/readme-stars.yml b/.github/workflows/readme-stars.yml index 4b943fa..1dec447 100644 --- a/.github/workflows/readme-stars.yml +++ b/.github/workflows/readme-stars.yml @@ -9,22 +9,16 @@ on: jobs: update-readme: runs-on: ubuntu-latest + if: ${{ vars.AOC_ENABLED == 'true' }} + permissions: + contents: write steps: - - uses: actions/checkout@v3 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} + - uses: actions/checkout@v4 - uses: k2bd/advent-readme-stars@v1 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} with: userId: ${{ secrets.AOC_USER_ID }} sessionCookie: ${{ secrets.AOC_SESSION }} year: ${{ secrets.AOC_YEAR }} - - uses: stefanzweifel/git-auto-commit-action@v4 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} + - uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "update readme progess" diff --git a/.gitignore b/.gitignore index f2fd7aa..3d20b31 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +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 -/src/inputs -!/src/inputs/.keep + +data + +!data/inputs/.keep +!data/examples/.keep +!data/puzzles/.keep diff --git a/Cargo.lock b/Cargo.lock index 0fcc4a2..3464488 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.2" dependencies = [ "pico-args", "regex", @@ -12,18 +12,18 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.5" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c378d78423fdad8089616f827526ee33c19f2fddbd5de1629152c9593ba4783" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] [[package]] name = "memchr" -version = "2.6.3" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "pico-args" @@ -33,9 +33,9 @@ checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" [[package]] name = "regex" -version = "1.9.5" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", @@ -45,9 +45,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", @@ -56,6 +56,6 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.5" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" diff --git a/Cargo.toml b/Cargo.toml index b5f0ac7..05390bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,18 @@ [package] name = "advent_of_code" -version = "0.8.0" +version = "0.9.2" authors = ["Felix Spöttel <1682504+fspoettel@users.noreply.github.com>"] edition = "2021" 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" -regex = "1.9.5" +regex = "1.10.2" diff --git a/README.md b/README.md index 217425f..56182c6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,40 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. + +## Benchmarks + +| Day | Part 1 | Part 2 | +| :---: | :---: | :---: | +| [Day 1](./src/bin/01.rs) | `75.2µs` | `73.6µs` | +| [Day 2](./src/bin/02.rs) | `27.2µs` | `27.0µs` | +| [Day 3](./src/bin/03.rs) | `298.3µs` | `417.4µs` | +| [Day 4](./src/bin/04.rs) | `1.7ms` | `1.6ms` | +| [Day 5](./src/bin/05.rs) | `306.1µs` | `266.3µs` | +| [Day 6](./src/bin/06.rs) | `154.4µs` | `881.0µs` | +| [Day 7](./src/bin/07.rs) | `80.0µs` | `87.7µs` | +| [Day 8](./src/bin/08.rs) | `132.2µs` | `327.0µs` | +| [Day 9](./src/bin/09.rs) | `390.9µs` | `498.8µs` | +| [Day 10](./src/bin/10.rs) | `4.9µs` | `4.3µs` | +| [Day 11](./src/bin/11.rs) | `266.4µs` | `12.8ms` | +| [Day 12](./src/bin/12.rs) | `1.1ms` | `26.5ms` | +| [Day 13](./src/bin/13.rs) | `437.2µs` | `1.1ms` | +| [Day 14](./src/bin/14.rs) | `3.5ms` | `171.2ms` | +| [Day 15](./src/bin/15.rs) | `228.1µs` | `832.8µs` | +| [Day 16](./src/bin/16.rs) | `291.6ms` | `72.3ms` | +| [Day 17](./src/bin/17.rs) | `1.9ms` | `7.0ms` | +| [Day 18](./src/bin/18.rs) | `2.7ms` | `14.5ms` | +| [Day 19](./src/bin/19.rs) | `181.2ms` | `96.7ms` | +| [Day 20](./src/bin/20.rs) | `36.6ms` | `429.9ms` | +| [Day 21](./src/bin/21.rs) | `6.7ms` | `11.6ms` | +| [Day 22](./src/bin/22.rs) | `112.2µs` | `143.2µs` | +| [Day 23](./src/bin/23.rs) | `5.6ms` | `395.0ms` | +| [Day 24](./src/bin/24.rs) | `69.4ms` | `626.8ms` | +| [Day 25](./src/bin/25.rs) | `12.1µs` | `19.0ns` | + +**Total: 2475.08ms** + + --- ## Template setup @@ -17,6 +51,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 advent of code, change the `AOC_YEAR` variable in `.cargo/config.toml` to reflect the year you are solving. ### Setup rust 💻 @@ -37,44 +72,38 @@ 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/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. -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 -> **Note** -> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh # example: `cargo download 1` 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". ``` -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 ```sh @@ -82,19 +111,24 @@ 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 against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program. -Displayed _timings_ show the raw execution time of your solution without overhead (e.g. file reads). +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, 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 + +> [!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. ### Run all solutions @@ -106,22 +140,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, the `--release` flag runs an optimized build. + +#### Update readme benchmarks -_Total timing_ is computed from individual solution _timings_ and excludes as much overhead as possible. +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. -### Run all solutions against the example input +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 ```sh cargo test @@ -143,8 +176,8 @@ cargo clippy ### Read puzzle description in terminal -> **Note** -> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ```sh # example: `cargo read 1` @@ -156,24 +189,14 @@ 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` -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 +### Configure aoc-cli integration -Uncomment the `format` job in the `ci.yml` workflow to enable fmt checks 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] -### Enable clippy lints in CI - -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), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions). ### Automatically track ⭐️ progress in the readme @@ -189,13 +212,20 @@ 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. +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. 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. +### 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). 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/examples/01.txt b/data/examples/01.txt similarity index 100% rename from src/examples/01.txt rename to data/examples/01.txt diff --git a/src/examples/02.txt b/data/examples/02.txt similarity index 100% rename from src/examples/02.txt rename to data/examples/02.txt diff --git a/src/examples/03.txt b/data/examples/03.txt similarity index 100% rename from src/examples/03.txt rename to data/examples/03.txt diff --git a/src/examples/04.txt b/data/examples/04.txt similarity index 100% rename from src/examples/04.txt rename to data/examples/04.txt diff --git a/src/examples/05.txt b/data/examples/05.txt similarity index 100% rename from src/examples/05.txt rename to data/examples/05.txt diff --git a/src/examples/06.txt b/data/examples/06.txt similarity index 100% rename from src/examples/06.txt rename to data/examples/06.txt diff --git a/src/examples/07.txt b/data/examples/07.txt similarity index 100% rename from src/examples/07.txt rename to data/examples/07.txt diff --git a/src/examples/08.txt b/data/examples/08.txt similarity index 100% rename from src/examples/08.txt rename to data/examples/08.txt diff --git a/src/examples/09.txt b/data/examples/09.txt similarity index 100% rename from src/examples/09.txt rename to data/examples/09.txt diff --git a/src/examples/10.txt b/data/examples/10.txt similarity index 100% rename from src/examples/10.txt rename to data/examples/10.txt diff --git a/src/examples/11.txt b/data/examples/11.txt similarity index 100% rename from src/examples/11.txt rename to data/examples/11.txt diff --git a/src/examples/12.txt b/data/examples/12.txt similarity index 100% rename from src/examples/12.txt rename to data/examples/12.txt diff --git a/src/examples/13.txt b/data/examples/13.txt similarity index 100% rename from src/examples/13.txt rename to data/examples/13.txt diff --git a/src/examples/14.txt b/data/examples/14.txt similarity index 100% rename from src/examples/14.txt rename to data/examples/14.txt diff --git a/src/examples/15.txt b/data/examples/15.txt similarity index 100% rename from src/examples/15.txt rename to data/examples/15.txt diff --git a/src/examples/16.txt b/data/examples/16.txt similarity index 100% rename from src/examples/16.txt rename to data/examples/16.txt diff --git a/src/examples/17.txt b/data/examples/17.txt similarity index 100% rename from src/examples/17.txt rename to data/examples/17.txt diff --git a/src/examples/18.txt b/data/examples/18.txt similarity index 100% rename from src/examples/18.txt rename to data/examples/18.txt diff --git a/src/examples/19.txt b/data/examples/19.txt similarity index 100% rename from src/examples/19.txt rename to data/examples/19.txt diff --git a/src/examples/20.txt b/data/examples/20.txt similarity index 100% rename from src/examples/20.txt rename to data/examples/20.txt diff --git a/src/examples/21.txt b/data/examples/21.txt similarity index 100% rename from src/examples/21.txt rename to data/examples/21.txt diff --git a/src/examples/22.txt b/data/examples/22.txt similarity index 100% rename from src/examples/22.txt rename to data/examples/22.txt diff --git a/src/examples/23.txt b/data/examples/23.txt similarity index 100% rename from src/examples/23.txt rename to data/examples/23.txt diff --git a/src/examples/24.txt b/data/examples/24.txt similarity index 100% rename from src/examples/24.txt rename to data/examples/24.txt diff --git a/src/examples/25.txt b/data/examples/25.txt similarity index 100% rename from src/examples/25.txt rename to data/examples/25.txt diff --git a/src/inputs/.keep b/src/bin/.keep similarity index 100% rename from src/inputs/.keep rename to src/bin/.keep diff --git a/src/bin/01.rs b/src/bin/01.rs index 3ac830a..074cdd7 100644 --- a/src/bin/01.rs +++ b/src/bin/01.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(1); + fn parse_data(input: &str) -> Vec> { input .split("\n\n") @@ -27,25 +29,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 1); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 1); - assert_eq!(part_one(&input), Some(24000)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(24000)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 1); - assert_eq!(part_two(&input), Some(45000)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(45000)); } } diff --git a/src/bin/02.rs b/src/bin/02.rs index b704523..df8bd42 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(2); + fn parse_data(input: &str) -> Vec<(char, char)> { input .lines() @@ -53,25 +55,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 2); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 2); - assert_eq!(part_one(&input), Some(15)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(15)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 2); - assert_eq!(part_two(&input), Some(12)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(12)); } } diff --git a/src/bin/03.rs b/src/bin/03.rs index f6e9f58..4d486b5 100644 --- a/src/bin/03.rs +++ b/src/bin/03.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(3); + use std::collections::BTreeSet; fn parse_data(input: &str) -> Vec<&[u8]> { @@ -50,25 +52,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 3); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 3); - assert_eq!(part_one(&input), Some(157)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(157)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 3); - assert_eq!(part_two(&input), Some(70)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(70)); } } diff --git a/src/bin/04.rs b/src/bin/04.rs index e26c9d5..0aa072f 100644 --- a/src/bin/04.rs +++ b/src/bin/04.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(4); + use regex::Regex; use std::collections::BTreeSet; use std::ops::RangeInclusive; @@ -44,25 +46,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 4); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 4); - assert_eq!(part_one(&input), Some(2)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(2)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 4); - assert_eq!(part_two(&input), Some(4)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(4)); } } diff --git a/src/bin/05.rs b/src/bin/05.rs index 9ce3e2b..4a44648 100644 --- a/src/bin/05.rs +++ b/src/bin/05.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(5); + use regex::Regex; use advent_of_code::util::parse::ParseRegex; @@ -73,25 +75,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 5); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 5); - assert_eq!(part_one(&input), Some(String::from("CMZ"))); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(String::from("CMZ"))); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 5); - assert_eq!(part_two(&input), Some(String::from("MCD"))); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(String::from("MCD"))); } } diff --git a/src/bin/06.rs b/src/bin/06.rs index d622d21..7923c71 100644 --- a/src/bin/06.rs +++ b/src/bin/06.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(6); + use std::collections::HashSet; fn parse_data(input: &str) -> &[u8] { @@ -27,25 +29,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 6); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 6); - assert_eq!(part_one(&input), Some(11)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(11)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 6); - assert_eq!(part_two(&input), Some(26)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(26)); } } diff --git a/src/bin/07.rs b/src/bin/07.rs index b6c4fd8..5aac2f8 100644 --- a/src/bin/07.rs +++ b/src/bin/07.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(7); + use advent_of_code::util::grid::ArenaTree; enum NodeValueEnum { @@ -78,25 +80,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 7); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 7); - assert_eq!(part_one(&input), Some(95437)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(95437)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 7); - assert_eq!(part_two(&input), Some(24933642)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(24933642)); } } diff --git a/src/bin/08.rs b/src/bin/08.rs index 648085e..89df154 100644 --- a/src/bin/08.rs +++ b/src/bin/08.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(8); + use std::collections::HashSet; use advent_of_code::util::list::Array2D; @@ -123,25 +125,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 8); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 8); - assert_eq!(part_one(&input), Some(21)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(21)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 8); - assert_eq!(part_two(&input), Some(8)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(8)); } } diff --git a/src/bin/09.rs b/src/bin/09.rs index ccc7b02..68d78ba 100644 --- a/src/bin/09.rs +++ b/src/bin/09.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(9); + use std::collections::HashSet; use advent_of_code::util::point::Point; @@ -81,25 +83,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 9); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 9); - assert_eq!(part_one(&input), Some(88)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(88)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 9); - assert_eq!(part_two(&input), Some(36)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(36)); } } diff --git a/src/bin/10.rs b/src/bin/10.rs index a806c7a..6b3ff50 100644 --- a/src/bin/10.rs +++ b/src/bin/10.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(10); + mod interpreter { enum Command { Noop, @@ -106,7 +108,7 @@ pub fn part_two(input: &str) -> Option { let x = p.cycle % 40; let y = p.cycle / 40; - display[y][x] = matches!(p.register_x - x as i32, -1 | 0 | 1); + display[y][x] = matches!(p.register_x - x as i32, -1..=1); p.exec_single_cycle(); } @@ -120,27 +122,21 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 10); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 10); - assert_eq!(part_one(&input), Some(13140)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(13140)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 10); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); assert_eq!( - part_two(&input), + result, Some(String::from( r#" ## ## ## ## ## ## ## ## ## ## diff --git a/src/bin/11.rs b/src/bin/11.rs index c2aa44d..bd89182 100644 --- a/src/bin/11.rs +++ b/src/bin/11.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(11); + use regex::Regex; enum Operation { @@ -115,25 +117,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 11); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 11); - assert_eq!(part_one(&input), Some(10605)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(10605)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 11); - assert_eq!(part_two(&input), Some(2713310158)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(2713310158)); } } diff --git a/src/bin/12.rs b/src/bin/12.rs index 8577d6f..d6e2d95 100644 --- a/src/bin/12.rs +++ b/src/bin/12.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(12); + use std::cmp::Ordering; use std::collections::BinaryHeap; use std::collections::HashMap; @@ -157,25 +159,19 @@ pub fn part_two(input: &str) -> Option { result } -fn main() { - let input = &advent_of_code::read_file("inputs", 12); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 12); - assert_eq!(part_one(&input), Some(31)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(31)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 12); - assert_eq!(part_two(&input), Some(29)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(29)); } } diff --git a/src/bin/13.rs b/src/bin/13.rs index daad7b1..3e85f67 100644 --- a/src/bin/13.rs +++ b/src/bin/13.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(13); + use std::cmp::Ordering; #[derive(PartialEq, Eq)] @@ -147,25 +149,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 13); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 13); - assert_eq!(part_one(&input), Some(13)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(13)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 13); - assert_eq!(part_two(&input), Some(140)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(140)); } } diff --git a/src/bin/14.rs b/src/bin/14.rs index 4e54a80..af542e5 100644 --- a/src/bin/14.rs +++ b/src/bin/14.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(14); + use std::collections::HashSet; use advent_of_code::util::point::Point; @@ -88,25 +90,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 14); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 14); - assert_eq!(part_one(&input), Some(24)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(24)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 14); - assert_eq!(part_two(&input), Some(93)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(93)); } } diff --git a/src/bin/15.rs b/src/bin/15.rs index 4fa58e3..bdcd5ff 100644 --- a/src/bin/15.rs +++ b/src/bin/15.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(15); + use regex::Regex; use std::collections::BTreeSet; @@ -185,25 +187,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 15); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 15); - assert_eq!(part_one(&input), Some(3075235)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(3075235)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 15); - assert_eq!(part_two(&input), Some(2746461376372)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(2746461376372)); } } diff --git a/src/bin/16.rs b/src/bin/16.rs index 642dcb6..a92a4c4 100644 --- a/src/bin/16.rs +++ b/src/bin/16.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(16); + use regex::Regex; use std::collections::HashMap; use std::collections::HashSet; @@ -121,7 +123,7 @@ fn part_x(data: &ValveArray) -> Vec { for (i, v1) in data.values().enumerate() { for (j, v2) in data.values().enumerate() { if i != j && v2.flow_rate > 0 { - let p = valve_paths.entry(v1.name).or_insert(vec![]); + let p = valve_paths.entry(v1.name).or_default(); p.push((v2.name, bfs(data, v1.name, v2.name).unwrap() + 1)); } } @@ -176,25 +178,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 16); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 16); - assert_eq!(part_one(&input), Some(1651)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1651)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 16); - assert_eq!(part_two(&input), Some(1707)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1707)); } } diff --git a/src/bin/17.rs b/src/bin/17.rs index 91d4e8f..9d9dcc5 100644 --- a/src/bin/17.rs +++ b/src/bin/17.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(17); + use std::collections::hash_map::Entry; use std::collections::hash_map::HashMap; @@ -239,25 +241,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 17); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 17); - assert_eq!(part_one(&input), Some(3068)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(3068)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 17); - assert_eq!(part_two(&input), Some(1514285714288)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1514285714288)); } } diff --git a/src/bin/18.rs b/src/bin/18.rs index 946f30b..e806505 100644 --- a/src/bin/18.rs +++ b/src/bin/18.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(18); + use std::collections::hash_map::Entry; use std::collections::HashMap; use std::collections::HashSet; @@ -159,25 +161,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 18); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 18); - assert_eq!(part_one(&input), Some(64)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(64)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 18); - assert_eq!(part_two(&input), Some(58)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(58)); } } diff --git a/src/bin/19.rs b/src/bin/19.rs index fac6ca7..2f6307e 100644 --- a/src/bin/19.rs +++ b/src/bin/19.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(19); + use regex::Regex; use std::collections::HashSet; use std::collections::VecDeque; @@ -264,25 +266,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 19); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 19); - assert_eq!(part_one(&input), Some(23)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(23)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 19); - assert_eq!(part_two(&input), Some(29348)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(29348)); } } diff --git a/src/bin/20.rs b/src/bin/20.rs index a55cd90..4932952 100644 --- a/src/bin/20.rs +++ b/src/bin/20.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(20); + mod list { pub struct CircualList { raw_data: Vec, @@ -200,25 +202,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 20); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 20); - assert_eq!(part_one(&input), Some(3)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(3)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 20); - assert_eq!(part_two(&input), Some(1623178306)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1623178306)); } } diff --git a/src/bin/21.rs b/src/bin/21.rs index a9c6780..3b76741 100644 --- a/src/bin/21.rs +++ b/src/bin/21.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(21); + use regex::Regex; use std::collections::HashMap; @@ -127,25 +129,19 @@ pub fn part_two(input: &str) -> Option { } } -fn main() { - let input = &advent_of_code::read_file("inputs", 21); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 21); - assert_eq!(part_one(&input), Some(152)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(152)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 21); - assert_eq!(part_two(&input), Some(301)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(301)); } } diff --git a/src/bin/22.rs b/src/bin/22.rs index d2de35b..ce8e2e4 100644 --- a/src/bin/22.rs +++ b/src/bin/22.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(22); + use advent_of_code::util::list::Array2D; enum Cell { @@ -220,25 +222,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 22); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 22); - assert_eq!(part_one(&input), Some(135107)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(135107)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 22); - assert_eq!(part_two(&input), Some(27279)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(27279)); } } diff --git a/src/bin/23.rs b/src/bin/23.rs index bafb6b6..0d7ae45 100644 --- a/src/bin/23.rs +++ b/src/bin/23.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(23); + use std::collections::HashMap; use std::collections::HashSet; @@ -141,25 +143,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 23); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 23); - assert_eq!(part_one(&input), Some(110)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(110)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 23); - assert_eq!(part_two(&input), Some(20)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(20)); } } diff --git a/src/bin/24.rs b/src/bin/24.rs index 152dfa1..ce5d482 100644 --- a/src/bin/24.rs +++ b/src/bin/24.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(24); + use std::collections::HashMap; use std::collections::HashSet; use std::collections::VecDeque; @@ -294,25 +296,19 @@ pub fn part_two(input: &str) -> Option { Some(result) } -fn main() { - let input = &advent_of_code::read_file("inputs", 24); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 24); - assert_eq!(part_one(&input), Some(18)); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(18)); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 24); - assert_eq!(part_two(&input), Some(54)); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(54)); } } diff --git a/src/bin/25.rs b/src/bin/25.rs index d400164..299440a 100644 --- a/src/bin/25.rs +++ b/src/bin/25.rs @@ -1,3 +1,5 @@ +advent_of_code::solution!(25); + fn parse_data(input: &str) -> Vec<&[u8]> { input.lines().map(|x| x.as_bytes()).collect() } @@ -47,25 +49,19 @@ pub fn part_two(_: &str) -> Option { Some(String::from("⭐️⭐️")) } -fn main() { - let input = &advent_of_code::read_file("inputs", 25); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_part_one() { - let input = advent_of_code::read_file("examples", 25); - assert_eq!(part_one(&input), Some(String::from("2=-1=0"))); + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(String::from("2=-1=0"))); } #[test] fn test_part_two() { - let input = advent_of_code::read_file("examples", 25); - assert_eq!(part_two(&input), Some(String::from("⭐️⭐️"))); + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(String::from("⭐️⭐️"))); } } diff --git a/src/bin/download.rs b/src/bin/download.rs deleted file mode 100644 index 545eacd..0000000 --- a/src/bin/download.rs +++ /dev/null @@ -1,46 +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, - 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::download(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/bin/read.rs b/src/bin/read.rs deleted file mode 100644 index 7fe21c4..0000000 --- a/src/bin/read.rs +++ /dev/null @@ -1,46 +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, - 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/day.rs b/src/day.rs new file mode 100644 index 0000000..5148797 --- /dev/null +++ b/src/day.rs @@ -0,0 +1,172 @@ +use std::error::Error; +use std::fmt::Display; +use std::str::FromStr; + +/// A valid day number of advent (i.e. an integer in range 1 to 25). +/// +/// # Display +/// This value displays as a two digit number. +/// +/// ``` +/// # use advent_of_code::Day; +/// let day = Day::new(8).unwrap(); +/// assert_eq!(day.to_string(), "08") +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Day(u8); + +impl Day { + /// Creates a [`Day`] from the provided value if it's in the valid range, + /// returns [`None`] otherwise. + pub fn new(day: u8) -> Option { + if day == 0 || day > 25 { + return None; + } + Some(Self(day)) + } + + // Not part of the public API + #[doc(hidden)] + pub const fn __new_unchecked(day: u8) -> Self { + Self(day) + } + + /// Converts the [`Day`] into an [`u8`]. + pub fn into_inner(self) -> u8 { + self.0 + } +} + +impl Display for Day { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:02}", self.0) + } +} + +impl PartialEq for Day { + fn eq(&self, other: &u8) -> bool { + self.0.eq(other) + } +} + +impl PartialOrd for Day { + fn partial_cmp(&self, other: &u8) -> Option { + self.0.partial_cmp(other) + } +} + +/* -------------------------------------------------------------------------- */ + +impl FromStr for Day { + type Err = DayFromStrError; + + fn from_str(s: &str) -> Result { + let day = s.parse().map_err(|_| DayFromStrError)?; + Self::new(day).ok_or(DayFromStrError) + } +} + +/// An error which can be returned when parsing a [`Day`]. +#[derive(Debug)] +pub struct DayFromStrError; + +impl Error for DayFromStrError {} + +impl Display for DayFromStrError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("expecting a day number between 1 and 25") + } +} + +/* -------------------------------------------------------------------------- */ + +/// An iterator that yields every day of advent from the 1st to the 25th. +pub fn all_days() -> AllDays { + AllDays::new() +} + +/// An iterator that yields every day of advent from the 1st to the 25th. +pub struct AllDays { + current: u8, +} + +impl AllDays { + #[allow(clippy::new_without_default)] + pub fn new() -> Self { + Self { current: 1 } + } +} + +impl Iterator for AllDays { + type Item = Day; + + fn next(&mut self) -> Option { + if self.current > 25 { + return None; + } + // NOTE: the iterator starts at 1 and we have verified that the value is not above 25. + let day = Day(self.current); + self.current += 1; + + Some(day) + } +} + +/* -------------------------------------------------------------------------- */ + +/// Creates a [`Day`] value in a const context. +#[macro_export] +macro_rules! day { + ($day:expr) => {{ + const _ASSERT: () = assert!( + $day != 0 && $day <= 25, + concat!( + "invalid day number `", + $day, + "`, expecting a value between 1 and 25" + ), + ); + $crate::Day::__new_unchecked($day) + }}; +} + +/* -------------------------------------------------------------------------- */ + +#[cfg(feature = "test_lib")] +mod tests { + use super::{all_days, Day}; + + #[test] + fn all_days_iterator() { + let mut iter = all_days(); + + assert_eq!(iter.next(), Some(Day(1))); + assert_eq!(iter.next(), Some(Day(2))); + assert_eq!(iter.next(), Some(Day(3))); + assert_eq!(iter.next(), Some(Day(4))); + assert_eq!(iter.next(), Some(Day(5))); + assert_eq!(iter.next(), Some(Day(6))); + assert_eq!(iter.next(), Some(Day(7))); + assert_eq!(iter.next(), Some(Day(8))); + assert_eq!(iter.next(), Some(Day(9))); + assert_eq!(iter.next(), Some(Day(10))); + assert_eq!(iter.next(), Some(Day(11))); + assert_eq!(iter.next(), Some(Day(12))); + assert_eq!(iter.next(), Some(Day(13))); + assert_eq!(iter.next(), Some(Day(14))); + assert_eq!(iter.next(), Some(Day(15))); + assert_eq!(iter.next(), Some(Day(16))); + assert_eq!(iter.next(), Some(Day(17))); + assert_eq!(iter.next(), Some(Day(18))); + assert_eq!(iter.next(), Some(Day(19))); + assert_eq!(iter.next(), Some(Day(20))); + assert_eq!(iter.next(), Some(Day(21))); + assert_eq!(iter.next(), Some(Day(22))); + assert_eq!(iter.next(), Some(Day(23))); + assert_eq!(iter.next(), Some(Day(24))); + assert_eq!(iter.next(), Some(Day(25))); + assert_eq!(iter.next(), None); + } +} + +/* -------------------------------------------------------------------------- */ 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 e7973c6..7f3dbe3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,235 +1,5 @@ -/* - * 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; +mod day; +pub mod template; -pub mod helpers; +pub use day::*; pub mod util; - -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 { - ($part:expr, $solver:ident, $input:expr) => {{ - use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET}; - use std::fmt::Display; - use std::time::Instant; - - 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 - ); - } - 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); - String::from(f.expect("could not open input file").trim_end()) -} - -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, - 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!("{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 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) - } -} diff --git a/src/main.rs b/src/main.rs index 1b6c855..23ba03c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,45 +1,92 @@ -/* - * 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, download, read, scaffold, solve}; +use args::{parse, AppArguments}; -fn main() { - let total: f64 = (1..=25) - .map(|day| { - let day = format!("{day:02}"); +mod args { + use std::process; - let mut args = vec!["run", "--bin", &day]; - if cfg!(not(debug_assertions)) { - args.push("--release"); - } + use advent_of_code::Day; - let cmd = Command::new("cargo").args(&args).output().unwrap(); + pub enum AppArguments { + Download { + day: Day, + }, + Read { + day: Day, + }, + Scaffold { + day: Day, + }, + Solve { + day: Day, + release: bool, + time: bool, + submit: Option, + }, + All { + release: bool, + time: bool, + }, + } - println!("----------"); - println!("{ANSI_BOLD}| Day {day} |{ANSI_RESET}"); - println!("----------"); + pub fn parse() -> 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") => AppArguments::All { + release: args.contains("--release"), + time: args.contains("--time"), + }, + Some("download") => AppArguments::Download { + day: args.free_from_str()?, + }, + Some("read") => AppArguments::Read { + day: args.free_from_str()?, + }, + Some("scaffold") => AppArguments::Scaffold { + day: args.free_from_str()?, + }, + 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}"); + 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() { + Err(err) => { + eprintln!("Error: {err}"); + std::process::exit(1); + } + Ok(args) => match args { + 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::handle(day, release, time, submit), + }, + }; } diff --git a/src/template/aoc_cli.rs b/src/template/aoc_cli.rs new file mode 100644 index 0000000..e7aab8b --- /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}, +}; + +use crate::Day; + +#[derive(Debug)] +pub enum AocCommandError { + CommandNotFound, + CommandNotCallable, + BadExitStatus(Output), + IoError, +} + +impl Display for AocCommandError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + 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.") + } + AocCommandError::IoError => write!(f, "could not write output files to file system."), + } + } +} + +pub fn check() -> Result<(), AocCommandError> { + Command::new("aoc") + .arg("-V") + .output() + .map_err(|_| AocCommandError::CommandNotFound)?; + Ok(()) +} + +pub fn read(day: Day) -> 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: Day) -> 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: Day, 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: Day) -> String { + format!("data/inputs/{day}.txt") +} + +fn get_puzzle_path(day: Day) -> String { + format!("data/puzzles/{day}.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: Day) -> 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(|_| AocCommandError::CommandNotCallable)?; + + if output.status.success() { + Ok(output) + } else { + Err(AocCommandError::BadExitStatus(output)) + } +} diff --git a/src/template/commands/all.rs b/src/template/commands/all.rs new file mode 100644 index 0000000..7443322 --- /dev/null +++ b/src/template/commands/all.rs @@ -0,0 +1,254 @@ +use std::io; + +use crate::template::{ + readme_benchmarks::{self, Timings}, + ANSI_BOLD, ANSI_ITALIC, ANSI_RESET, +}; +use crate::{all_days, Day}; + +pub fn handle(is_release: bool, is_timed: bool) { + let mut timings: Vec = vec![]; + + all_days().for_each(|day| { + if day > 1 { + println!(); + } + + println!("{ANSI_BOLD}Day {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::() / 1_000_000_f64; + + 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."), + 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) + } +} + +#[must_use] +pub fn get_path_for_bin(day: Day) -> String { + format!("./src/bin/{day}.rs") +} + +/// 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 crate::Day; + use std::{ + io::{BufRead, BufReader}, + path::Path, + process::{Command, Stdio}, + thread, + }; + + /// Run the solution bin for a given day + pub fn run_solution(day: Day, is_timed: bool, is_release: bool) -> Result, Error> { + // skip command invocation for days that have not been scaffolded yet. + if !Path::new(&get_path_for_bin(day)).exists() { + return Ok(vec![]); + } + + let day_padded = day.to_string(); + 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: Day) -> 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 Some((timing_str, nanos)) = parse_time(l) else { + 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 * 1_000_000_f64), + s => parse_to_float(s, "s").map(|x| x * 1_000_000_000_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; + + use crate::day; + + #[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(), + ], + day!(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(), + ], + day!(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(), + ], + day!(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..76ad635 --- /dev/null +++ b/src/template/commands/download.rs @@ -0,0 +1,15 @@ +use crate::template::aoc_cli; +use crate::Day; +use std::process; + +pub fn handle(day: Day) { + 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..01316f8 --- /dev/null +++ b/src/template/commands/read.rs @@ -0,0 +1,16 @@ +use std::process; + +use crate::template::aoc_cli; +use crate::Day; + +pub fn handle(day: Day) { + 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 51% rename from src/bin/scaffold.rs rename to src/template/commands/scaffold.rs index 1ee92e0..2a992bc 100644 --- a/src/bin/scaffold.rs +++ b/src/template/commands/scaffold.rs @@ -1,15 +1,14 @@ -/* - * 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, process, }; -#[allow(clippy::needless_raw_string_hashes)] -const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option { +use crate::Day; + +const MODULE_TEMPLATE: &str = r#"advent_of_code::solution!(DAY_NUMBER); + +pub fn part_one(input: &str) -> Option { None } @@ -17,34 +16,23 @@ pub fn part_two(input: &str) -> Option { None } -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); -} - #[cfg(test)] mod tests { use super::*; #[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) @@ -54,20 +42,10 @@ 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}"); - - 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"); +pub fn handle(day: Day) { + let input_path = format!("data/inputs/{day}.txt"); + let example_path = format!("data/examples/{day}.txt"); + let module_path = format!("src/bin/{day}.rs"); let mut file = match safe_create_file(&module_path) { Ok(file) => file, @@ -77,8 +55,12 @@ fn main() { } }; - match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) { - Ok(_) => { + match file.write_all( + MODULE_TEMPLATE + .replace("DAY_NUMBER", &day.into_inner().to_string()) + .as_bytes(), + ) { + Ok(()) => { println!("Created module file \"{}\"", &module_path); } Err(e) => { @@ -108,8 +90,5 @@ fn main() { } println!("---"); - println!( - "🎄 Type `cargo solve {}` to run your solution.", - &day_padded - ); + println!("🎄 Type `cargo solve {}` to run your solution.", day); } diff --git a/src/template/commands/solve.rs b/src/template/commands/solve.rs new file mode 100644 index 0000000..50b7000 --- /dev/null +++ b/src/template/commands/solve.rs @@ -0,0 +1,31 @@ +use std::process::{Command, Stdio}; + +use crate::Day; + +pub fn handle(day: Day, release: bool, time: bool, submit_part: Option) { + let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()]; + + 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..b38102f --- /dev/null +++ b/src/template/mod.rs @@ -0,0 +1,36 @@ +use crate::Day; +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. +#[must_use] +pub fn read_file(folder: &str, day: Day) -> String { + let cwd = env::current_dir().unwrap(); + let filepath = cwd.join("data").join(folder).join(format!("{day}.txt")); + let f = fs::read_to_string(filepath); + String::from(f.expect("could not open input file").trim_end()) +} + +/// Creates the constant `DAY` and sets up the input and runner for each part. +#[macro_export] +macro_rules! solution { + ($day:expr) => { + /// The current day. + const DAY: advent_of_code::Day = advent_of_code::day!($day); + + 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..c564aa4 --- /dev/null +++ b/src/template/readme_benchmarks.rs @@ -0,0 +1,186 @@ +/// 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}; + +use crate::Day; + +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: Day, + pub part_1: Option, + pub part_2: Option, + pub total_nanos: f64, +} + +pub struct TablePosition { + pos_start: usize, + pos_end: usize, +} + +#[must_use] +pub fn get_path_for_bin(day: Day) -> String { + format!("./src/bin/{day}.rs") +} + +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, + String::new(), + "| Day | Part 1 | Part 2 |".into(), + "| :---: | :---: | :---: |".into(), + ]; + + for timing in timings { + let path = get_path_for_bin(timing.day); + lines.push(format!( + "| [Day {}]({}) | `{}` | `{}` |", + timing.day.into_inner(), + path, + timing.part_1.unwrap_or_else(|| "-".into()), + timing.part_2.unwrap_or_else(|| "-".into()) + )); + } + + lines.push(String::new()); + lines.push(format!("**Total: {total_millis:.2}ms**")); + 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}; + use crate::day; + + fn get_mock_timings() -> Vec { + vec![ + Timings { + day: day!(1), + part_1: Some("10ms".into()), + part_2: Some("20ms".into()), + total_nanos: 3e+10, + }, + Timings { + day: day!(2), + part_1: Some("30ms".into()), + part_2: Some("40ms".into()), + total_nanos: 7e+10, + }, + Timings { + day: 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..5e6a9b3 --- /dev/null +++ b/src/template/runner.rs @@ -0,0 +1,167 @@ +/// Encapsulates code that interacts with solution functions. +use crate::template::{aoc_cli, ANSI_ITALIC, ANSI_RESET}; +use crate::Day; +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: Day, 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 = if std::env::args().any(|x| x == "--time") { + bench(func, input, &base_time) + } else { + (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!(" > {ANSI_ITALIC}benching{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()); + } + + ( + #[allow(clippy::cast_possible_truncation)] + Duration::from_nanos(average_duration(&timers) as u64), + bench_iterations, + ) +} + +fn average_duration(numbers: &[Duration]) -> 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!(" ({duration:.1?})") + } else { + format!(" ({duration:.1?} @ {samples} 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: Day, + 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 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 { + 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())) +}