diff --git a/.assets/banner.png b/.assets/banner.png new file mode 100644 index 0000000..36ca006 Binary files /dev/null and b/.assets/banner.png differ diff --git a/.assets/christmas_ferris.png b/.assets/christmas_ferris.png new file mode 100644 index 0000000..365527a Binary files /dev/null and b/.assets/christmas_ferris.png differ diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..3530669 --- /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 = "2023" diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..6043626 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,6 @@ +{ + "name": "rust-devcontainer", + "image": "mcr.microsoft.com/devcontainers/rust:latest", + "postCreateCommand": "rustc --version", + "remoteUser": "vscode" +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..560e94b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# EditorConfig is awesome: http://EditorConfig.org +root = true + +[*] +indent_size = 4 +indent_style = space +end_of_line = lf +charset = utf-8 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.txt] +insert_final_newline = false +trim_trailing_whitespace = false + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9eef218 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,33 @@ +name: Continuous Integration + +on: push + +env: + CARGO_TERM_COLOR: always + +jobs: + test: + runs-on: ubuntu-latest + name: CI + steps: + - uses: actions/checkout@v3 + - name: Set up cargo cache + uses: actions/cache@v3 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- + - name: cargo test + run: cargo test + # uncomment to enable clippy linter + # - name: cargo clippy + # run: cargo clippy -- -D warnings + # uncomment to enable format linter + # - name: cargo fmt + # run: cargo fmt --check diff --git a/.github/workflows/readme-stars.yml b/.github/workflows/readme-stars.yml new file mode 100644 index 0000000..3de260e --- /dev/null +++ b/.github/workflows/readme-stars.yml @@ -0,0 +1,24 @@ +name: Update readme ⭐️ progress + +on: + # !Please set a different minute than 51 if you enable this! + # schedule: + # - cron: "51 */6 * * *" # Every 6 hours + workflow_dispatch: + +jobs: + update-readme: + runs-on: ubuntu-latest + if: ${{ vars.AOC_ENABLED == 'true' }} + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - uses: k2bd/advent-readme-stars@v1 + with: + userId: ${{ secrets.AOC_USER_ID }} + sessionCookie: ${{ secrets.AOC_SESSION }} + year: ${{ secrets.AOC_YEAR }} + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "update readme progress" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3b4aa2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + + +# Added by cargo + +/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/inputs/* +!data/inputs/.keep +data/puzzles/* +!data/puzzles/.keep diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..2475a54 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + "recommendations": [ + "vadimcn.vscode-lldb", + "rust-lang.rust-analyzer", + "serayuzgur.crates" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0a9370e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,50 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'advent_of_code'", + "cargo": { + "args": ["test", "--no-run", "--bin=advent_of_code", "--package=advent_of_code"], + "filter": { + "name": "advent_of_code", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'advent_of_code'", + "cargo": { + "args": ["build", "--bin=advent_of_code", "--package=advent_of_code"], + "filter": { + "name": "advent_of_code", + "kind": "bin" + } + }, + "args": ["1"], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in library 'advent_of_code'", + "cargo": { + "args": ["test", "--no-run", "--lib", "--package=advent_of_code"], + "filter": { + "name": "advent_of_code", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ea86ba4 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "advent_of_code" +version = "0.9.3" +dependencies = [ + "pico-args", +] + +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..692fc58 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "advent_of_code" +version = "0.9.3" +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" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b97fd05 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Felix Spoettel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..85aba56 --- /dev/null +++ b/README.md @@ -0,0 +1,231 @@ + + +# πŸŽ„ Advent of Code {year} + +Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/). + + + + + +--- + +## Template setup + +This template supports all major OS (macOS, Linux, Windows). + +### Create your repository πŸ“ + +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 πŸ’» + +1. Install the [Rust toolchain](https://www.rust-lang.org/tools/install). +2. (recommended) Install the [rust-analyzer](https://rust-analyzer.github.io/manual.html) extension for your code editor. +3. (optional) Install a native debugger. If you are using VS Code, [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb) is a good option. + +--- + +✨ You can start solving puzzles now! Head to the [Usage section](#usage) to see how to use this template. If you like, you can configure [some optional features](#optional-template-features). + +## Usage + +### Scaffold a day + +```sh +# example: `cargo scaffold 1` +cargo scaffold + +# output: +# 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. _Inputs_ and _examples_ live in the the `./data` directory. + +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] +> 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 + +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). + +```sh +# example: `cargo download 1` +cargo download + +# output: +# [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 "data/inputs/01.txt". +# πŸŽ„ Successfully wrote puzzle to "data/puzzles/01.md". +``` + +### Run solutions for a day + +```sh +# example: `cargo solve 01` +cargo solve + +# output: +# Finished dev [unoptimized + debuginfo] target(s) in 0.13s +# Running `target/debug/01` +# Part 1: 42 (166.0ns) +# Part 2: 42 (41.0ns) +``` + +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. + +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 + +```sh +cargo all + +# output: +# Running `target/release/advent_of_code` +# ---------- +# | Day 01 | +# ---------- +# Part 1: 42 (19.0ns) +# Part 2: 42 (19.0ns) +# <...other days...> +# Total: 0.20ms +``` + +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. 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. + +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 +``` + +To run tests for a specific day, append `--bin `, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`. + +### Format code + +```sh +cargo fmt +``` + +### Lint code + +```sh +cargo clippy +``` + +### Read puzzle description in terminal + +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). + +```sh +# example: `cargo read 1` +cargo read + +# output: +# Loaded session cookie from "/Users//.adventofcode.session". +# Fetching puzzle for day 1, 2022... +# ...the input... +``` + +## Optional template features + +### Configure aoc-cli integration + +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), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions). + +### Automatically track ⭐️ progress in the readme + +This template includes [a Github action](https://github.com/k2bd/advent-readme-stars) that automatically updates the readme with your advent of code progress. + +To enable it, complete the following steps: + +#### 1. Create a private leaderboard + +Go to the leaderboard page of the year you want to track and click _Private Leaderboard_. If you have not created a leaderboard yet, create one by clicking _Create It_. Your leaderboard should be accessible under `https://adventofcode.com/{year}/leaderboard/private/view/{aoc_user_id}`. + +#### 2. Set repository secrets + +Go to the _Secrets_ tab in your repository settings and create the following secrets: + +- `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). +2. Set breakpoints in your code. [^3] +3. Click _Debug_ next to the unit test or the _main_ function. [^4] +4. The debugger will halt your program at the specific line and allow you to inspect the local stack. [^5] + +## Useful crates + +- [itertools](https://crates.io/crates/itertools): Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles. +- [regex](https://crates.io/crates/regex): Official regular expressions implementation for Rust. + +A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/crates). + +Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md) + +## Common pitfalls + +- **Integer overflows:** This template uses 32-bit integers by default because it is generally faster - for example when packed in large arrays or structs - than using 64-bit integers everywhere. For some problems, solutions for real input might exceed 32-bit integer space. While this is checked and panics in `debug` mode, integers [wrap](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow) in `release` mode, leading to wrong output when running your solution. + +## Footnotes + +[^1]: The session cookie might expire after a while (~1 month) which causes the downloads to fail. To fix this issue, refresh the `.adventofcode.session` file. +[^2]: The session cookie might expire after a while (~1 month) which causes the automated workflow to fail. To fix this issue, refresh the AOC_SESSION secret. +[^3]: + Set a breakpoint + +[^4]: + Run debugger + +[^5]: + Inspect debugger state diff --git a/data/examples/.keep b/data/examples/.keep new file mode 100644 index 0000000..e69de29 diff --git a/data/inputs/.keep b/data/inputs/.keep new file mode 100644 index 0000000..e69de29 diff --git a/data/puzzles/.keep b/data/puzzles/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/bin/.keep b/src/bin/.keep new file mode 100644 index 0000000..e69de29 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/lib.rs b/src/lib.rs new file mode 100644 index 0000000..04f64f0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +mod day; +pub mod template; + +pub use day::*; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..23ba03c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,92 @@ +use advent_of_code::template::commands::{all, download, read, scaffold, solve}; +use args::{parse, AppArguments}; + +mod args { + use std::process; + + use advent_of_code::Day; + + 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, + }, + } + + pub fn parse() -> Result> { + let mut args = pico_args::Arguments::from_env(); + + 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); + } + }; + + let remaining = args.finish(); + if !remaining.is_empty() { + eprintln!("Warning: unknown argument(s): {remaining:?}."); + } + + Ok(app_args) + } +} + +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/template/commands/scaffold.rs b/src/template/commands/scaffold.rs new file mode 100644 index 0000000..2a992bc --- /dev/null +++ b/src/template/commands/scaffold.rs @@ -0,0 +1,94 @@ +use std::{ + fs::{File, OpenOptions}, + io::Write, + process, +}; + +use crate::Day; + +const MODULE_TEMPLATE: &str = r#"advent_of_code::solution!(DAY_NUMBER); + +pub fn part_one(input: &str) -> Option { + None +} + +pub fn part_two(input: &str) -> Option { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +} +"#; + +fn safe_create_file(path: &str) -> Result { + OpenOptions::new().write(true).create_new(true).open(path) +} + +fn create_file(path: &str) -> Result { + OpenOptions::new().write(true).create(true).open(path) +} + +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, + Err(e) => { + eprintln!("Failed to create module file: {e}"); + process::exit(1); + } + }; + + 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) => { + eprintln!("Failed to write module contents: {e}"); + process::exit(1); + } + } + + match create_file(&input_path) { + Ok(_) => { + println!("Created empty input file \"{}\"", &input_path); + } + Err(e) => { + eprintln!("Failed to create input file: {e}"); + process::exit(1); + } + } + + match create_file(&example_path) { + Ok(_) => { + println!("Created empty example file \"{}\"", &example_path); + } + Err(e) => { + eprintln!("Failed to create example file: {e}"); + process::exit(1); + } + } + + println!("---"); + 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..d1533e1 --- /dev/null +++ b/src/template/mod.rs @@ -0,0 +1,48 @@ +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); + 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 { + ($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())) +}