This commit is contained in:
RussellChamp committed 2024-12-20 09:25:11 -05:00
commit 37ff7ac40b
1 file changed
+353 -350
+353 -350
View File
@@ -1,350 +1,353 @@
[![Continuous Integration](https://github.com/RussellChamp/advent-of-code-2024-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/RussellChamp/advent-of-code-2024-rust/actions/workflows/ci.yml) [![Continuous Integration](https://github.com/RussellChamp/advent-of-code-2024-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/RussellChamp/advent-of-code-2024-rust/actions/workflows/ci.yml)
<img src="./.assets/christmas_ferris.png" width="164"> <img src="./.assets/christmas_ferris.png" width="164">
# 🎄 Advent of Code {year} # 🎄 Advent of Code {year}
Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/). Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/).
<!--- advent_readme_stars table ---> <!--- advent_readme_stars table --->
## 2024 Results ## 2024 Results
| Day | Part 1 | Part 2 | | Day | Part 1 | Part 2 |
| :---: | :---: | :---: | | :---: | :---: | :---: |
| [Day 1](https://adventofcode.com/2024/day/1) | ⭐ | ⭐ | | [Day 1](https://adventofcode.com/2024/day/1) | ⭐ | ⭐ |
| [Day 2](https://adventofcode.com/2024/day/2) | ⭐ | ⭐ | | [Day 2](https://adventofcode.com/2024/day/2) | ⭐ | ⭐ |
| [Day 3](https://adventofcode.com/2024/day/3) | ⭐ | ⭐ | | [Day 3](https://adventofcode.com/2024/day/3) | ⭐ | ⭐ |
| [Day 4](https://adventofcode.com/2024/day/4) | ⭐ | ⭐ | | [Day 4](https://adventofcode.com/2024/day/4) | ⭐ | ⭐ |
| [Day 5](https://adventofcode.com/2024/day/5) | ⭐ | | | [Day 5](https://adventofcode.com/2024/day/5) | ⭐ | ⭐ |
| [Day 6](https://adventofcode.com/2024/day/6) | ⭐ | | | [Day 6](https://adventofcode.com/2024/day/6) | ⭐ | ⭐ |
| [Day 7](https://adventofcode.com/2024/day/7) | ⭐ | | | [Day 7](https://adventofcode.com/2024/day/7) | ⭐ | ⭐ |
| [Day 8](https://adventofcode.com/2024/day/8) | ⭐ | | | [Day 8](https://adventofcode.com/2024/day/8) | ⭐ | |
| [Day 10](https://adventofcode.com/2024/day/10) | ⭐ | | | [Day 9](https://adventofcode.com/2024/day/9) | ⭐ | |
| [Day 11](https://adventofcode.com/2024/day/11) | ⭐ | | | [Day 10](https://adventofcode.com/2024/day/10) | ⭐ | |
| [Day 12](https://adventofcode.com/2024/day/12) | ⭐ | | | [Day 11](https://adventofcode.com/2024/day/11) | ⭐ | |
| [Day 13](https://adventofcode.com/2024/day/13) | ⭐ | ⭐ | | [Day 12](https://adventofcode.com/2024/day/12) | ⭐ | |
| [Day 14](https://adventofcode.com/2024/day/14) | ⭐ | | | [Day 13](https://adventofcode.com/2024/day/13) | ⭐ | ⭐ |
| [Day 15](https://adventofcode.com/2024/day/15) | ⭐ | | | [Day 14](https://adventofcode.com/2024/day/14) | ⭐ | |
| [Day 16](https://adventofcode.com/2024/day/16) | ⭐ | | | [Day 15](https://adventofcode.com/2024/day/15) | ⭐ | |
| [Day 17](https://adventofcode.com/2024/day/17) | ⭐ | | | [Day 16](https://adventofcode.com/2024/day/16) | ⭐ | |
<!--- advent_readme_stars table ---> | [Day 17](https://adventofcode.com/2024/day/17) | ⭐ | |
| [Day 18](https://adventofcode.com/2024/day/18) | ⭐ | ⭐ |
<!--- benchmarking table ---> | [Day 19](https://adventofcode.com/2024/day/19) | ⭐ | ⭐ |
## Benchmarks <!--- advent_readme_stars table --->
| Day | Part 1 | Part 2 | <!--- benchmarking table --->
| :---: | :---: | :---: | ## Benchmarks
| [Day 1](./src/bin/01.rs) | `126.9µs` | `218.4µs` |
| [Day 2](./src/bin/02.rs) | `190.5µs` | `487.3µs` | | Day | Part 1 | Part 2 |
| [Day 3](./src/bin/03.rs) | `524.3µs` | `678.5µs` | | :---: | :---: | :---: |
| [Day 4](./src/bin/04.rs) | `3.7ms` | `495.5µs` | | [Day 1](./src/bin/01.rs) | `126.9µs` | `218.4µs` |
| [Day 5](./src/bin/05.rs) | `4.9ms` | `190.3ms` | | [Day 2](./src/bin/02.rs) | `190.5µs` | `487.3µs` |
| [Day 6](./src/bin/06.rs) | `59.8µs` | `-` | | [Day 3](./src/bin/03.rs) | `524.3µs` | `678.5µs` |
| [Day 7](./src/bin/07.rs) | `11.9ms` | `-` | | [Day 4](./src/bin/04.rs) | `3.7ms` | `495.5µs` |
| [Day 8](./src/bin/08.rs) | `106.2µs` | `-` | | [Day 5](./src/bin/05.rs) | `4.9ms` | `190.3ms` |
| [Day 9](./src/bin/09.rs) | `1.0ms` | `-` | | [Day 6](./src/bin/06.rs) | `59.8µs` | `-` |
| [Day 10](./src/bin/10.rs) | `10.1ms` | `-` | | [Day 7](./src/bin/07.rs) | `11.9ms` | `-` |
| [Day 11](./src/bin/11.rs) | `1.9s` | `-` | | [Day 8](./src/bin/08.rs) | `106.2µs` | `-` |
| [Day 12](./src/bin/12.rs) | `4.5ms` | `-` | | [Day 9](./src/bin/09.rs) | `1.0ms` | `-` |
| [Day 13](./src/bin/13.rs) | `2.7ms` | `719.8µs` | | [Day 10](./src/bin/10.rs) | `10.1ms` | `-` |
| [Day 14](./src/bin/14.rs) | `430.5µs` | `-` | | [Day 11](./src/bin/11.rs) | `1.9s` | `-` |
| [Day 15](./src/bin/15.rs) | `265.0µs` | `-` | | [Day 12](./src/bin/12.rs) | `4.5ms` | `-` |
| [Day 16](./src/bin/16.rs) | `38.0ms` | `-` | | [Day 13](./src/bin/13.rs) | `2.7ms` | `719.8µs` |
| [Day 14](./src/bin/14.rs) | `430.5µs` | `-` |
**Total: 2171.40ms** | [Day 15](./src/bin/15.rs) | `265.0µs` | `-` |
<!--- benchmarking table ---> | [Day 16](./src/bin/16.rs) | `38.0ms` | `-` |
--- **Total: 2171.40ms**
<!--- benchmarking table --->
## Template setup
---
This template supports all major OS (macOS, Linux, Windows).
## Template setup
### 📝 Create your repository
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. ### 📝 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. 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.
### 💻 Setup rust 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.
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. ### 💻 Setup rust
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.
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
✨ 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).
### ➡️ Scaffold a day
## Usage
```sh
# example: `cargo scaffold 1` ### ➡️ Scaffold a day
cargo scaffold <day>
```sh
# output: # example: `cargo scaffold 1`
# Created module file "src/bin/01.rs" cargo scaffold <day>
# Created empty input file "data/inputs/01.txt"
# Created empty example file "data/examples/01.txt" # output:
# --- # Created module file "src/bin/01.rs"
# 🎄 Type `cargo solve 01` to run your solution. # Created empty input file "data/inputs/01.txt"
``` # Created empty example file "data/examples/01.txt"
# ---
Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory. # 🎄 Type `cargo solve 01` to run your solution.
```
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template.txt) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input. In VS Code, `rust-analyzer` will display buttons for running / debugging these unit tests above the unit test blocks.
Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory.
> [!TIP]
> If a day has multiple example inputs, you can use the `read_file_part()` helper in your tests instead of `read_file()`. If this e.g. 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));`. This supports an arbitrary number of example files. Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template.txt) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input. In VS Code, `rust-analyzer` will display buttons for running / debugging these unit tests above the unit test blocks.
### ➡️ Download input for a day > [!TIP]
> If a day has multiple example inputs, you can use the `read_file_part()` helper in your tests instead of `read_file()`. If this e.g. 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));`. This supports an arbitrary number of example files.
> [!IMPORTANT]
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ### ➡️ Download input for a day
You can automatically download puzzle input and description by either appending the `--download` flag to `scaffold` (e.g. `cargo scaffold 4 --download`) or with the separate `download` command: > [!IMPORTANT]
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
```sh
# example: `cargo download 1` You can automatically download puzzle input and description by either appending the `--download` flag to `scaffold` (e.g. `cargo scaffold 4 --download`) or with the separate `download` command:
cargo download <day>
```sh
# output: # example: `cargo download 1`
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool cargo download <day>
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt' # output:
# --- # [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
# 🎄 Successfully wrote input to "data/inputs/01.txt". # [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md". # [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
``` # ---
# 🎄 Successfully wrote input to "data/inputs/01.txt".
### ➡️ Run solutions for a day # 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
```
```sh
# example: `cargo solve 01` ### ➡️ Run solutions for a day
cargo solve <day>
```sh
# output: # example: `cargo solve 01`
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s cargo solve <day>
# Running `target/debug/01`
# Part 1: 42 (166.0ns) # output:
# Part 2: 42 (41.0ns) # Finished dev [unoptimized + debuginfo] target(s) in 0.13s
``` # Running `target/debug/01`
# Part 1: 42 (166.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. # Part 2: 42 (41.0ns)
```
#### Submitting solutions
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.
> [!IMPORTANT]
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration). #### Submitting solutions
Append the `--submit <part>` option to the `solve` command to submit your solution for checking. > [!IMPORTANT]
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
### ➡️ Run all solutions
Append the `--submit <part>` option to the `solve` command to submit your solution for checking.
```sh
cargo all ### ➡️ Run all solutions
# output: ```sh
# Running `target/release/advent_of_code` cargo all
# ----------
# | Day 01 | # output:
# ---------- # Running `target/release/advent_of_code`
# Part 1: 42 (19.0ns) # ----------
# Part 2: 42 (19.0ns) # | Day 01 |
# <...other days...> # ----------
# Total: 0.20ms # Part 1: 42 (19.0ns)
``` # Part 2: 42 (19.0ns)
# <...other days...>
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. # Total: 0.20ms
```
### ➡️ Benchmark your solutions
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.
```sh
# example: `cargo time 8 --store` ### ➡️ Benchmark your solutions
cargo time <day> [--all] [--store]
```sh
# output: # example: `cargo time 8 --store`
# Day 08 cargo time <day> [--all] [--store]
# ------
# Part 1: 1 (39.0ns @ 10000 samples) # output:
# Part 2: 2 (39.0ns @ 10000 samples) # Day 08
# # ------
# Total (Run): 0.00ms # Part 1: 1 (39.0ns @ 10000 samples)
# # Part 2: 2 (39.0ns @ 10000 samples)
# Stored updated benchmarks. #
``` # Total (Run): 0.00ms
#
The `cargo time` command allows you to benchmark your code and store timings in the readme. When benching, 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. # Stored updated benchmarks.
```
`cargo time` has three modes of execution:
The `cargo time` command allows you to benchmark your code and store timings in the readme. When benching, 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.
1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest.
2. `cargo time <day>` benches a single solution. `cargo time` has three modes of execution:
3. `cargo time --all` benches all solutions.
1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest.
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`. 2. `cargo time <day>` benches a single solution.
3. `cargo time --all` benches all solutions.
> 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.
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`.
### ➡️ Run all tests
> 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.
```sh
cargo test ### ➡️ Run all tests
```
```sh
To run tests for a specific day, append `--bin <day>`, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`. cargo test
```
### ➡️ Read puzzle description
To run tests for a specific day, append `--bin <day>`, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`.
> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ### ➡️ Read puzzle description
```sh > [!IMPORTANT]
# example: `cargo read 1` > This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
cargo read <day>
```sh
# output: # example: `cargo read 1`
# Loaded session cookie from "/Users/<snip>/.adventofcode.session". cargo read <day>
# Fetching puzzle for day 1, 2022...
# ...the input... # output:
``` # Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
### ➡️ Scaffold, download & read the current aoc day # ...the input...
```
> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). ### ➡️ Scaffold, download & read the current aoc day
During december, the `today` shorthand command can be used to: > [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
- scaffold a solution for the current day
- download its input During december, the `today` shorthand command can be used to:
- and read the puzzle
- scaffold a solution for the current day
in one go. - download its input
- and read the puzzle
```sh
# example: `cargo today` on December 1st in one go.
cargo today
```sh
# output: # example: `cargo today` on December 1st
# Created module file "src/bin/01.rs" cargo today
# Created empty input file "data/inputs/01.txt"
# Created empty example file "data/examples/01.txt" # output:
# --- # Created module file "src/bin/01.rs"
# 🎄 Type `cargo solve 01` to run your solution. # Created empty input file "data/inputs/01.txt"
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool # Created empty example file "data/examples/01.txt"
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md' # ---
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt' # 🎄 Type `cargo solve 01` to run your solution.
# --- # [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
# 🎄 Successfully wrote input to "data/inputs/01.txt". # [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md". # [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
# # ---
# Loaded session cookie from "/Users/<snip>/.adventofcode.session". # 🎄 Successfully wrote input to "data/inputs/01.txt".
# Fetching puzzle for day 1, 2022... # 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
# ...the input... #
``` # Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
### ➡️ Format code # ...the input...
```
```sh
cargo fmt ### ➡️ Format code
```
```sh
### ➡️ Lint code cargo fmt
```
```sh
cargo clippy ### ➡️ Lint code
```
```sh
## Optional template features cargo clippy
```
### Configure aoc-cli integration
## Optional template features
1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
2. Create the file `<home_directory>/.adventofcode.session` and paste your session cookie into it. 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] ### Configure aoc-cli integration
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). 1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
2. Create the file `<home_directory>/.adventofcode.session` and paste your session cookie into it. 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]
### Automatically track ⭐️ progress in the readme
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).
This template includes [a Github action](https://github.com/k2bd/advent-readme-stars) that automatically updates the readme with your advent of code progress.
### Automatically track ⭐️ progress in the readme
To enable it, complete the following steps:
This template includes [a Github action](https://github.com/k2bd/advent-readme-stars) that automatically updates the readme with your advent of code progress.
#### 1. Create a private leaderboard
To enable it, complete the following steps:
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}`.
#### 1. Create a private leaderboard
#### 2. Set repository secrets
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}`.
Go to the _Secrets_ tab in your repository settings and create the following secrets:
#### 2. Set repository 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`. Go to the _Secrets_ tab in your repository settings and create the following secrets:
- `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.
- `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`.
Go to the _Variables_ tab in your repository settings and create the following variable: - `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.
- `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.
Go to the _Variables_ tab in your repository settings and create the following variable:
✨ 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.
- `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.
### Enable code formatting / clippy checks in 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.
Uncomment the respective sections in the `ci.yml` workflow.
### Enable code formatting / clippy checks in the CI
### Use DHAT to profile heap allocations
Uncomment the respective sections in the `ci.yml` workflow.
If you are not only interested in the runtime of your solution, but also its memory allocation profile, you can use the template's [DHAT](https://valgrind.org/docs/manual/dh-manual.html) integration to analyze it. In order to activate DHAT, call the `solve` command with the `--dhat` flag.
### Use DHAT to profile heap allocations
```sh
cargo solve 1 --dhat If you are not only interested in the runtime of your solution, but also its memory allocation profile, you can use the template's [DHAT](https://valgrind.org/docs/manual/dh-manual.html) integration to analyze it. In order to activate DHAT, call the `solve` command with the `--dhat` flag.
# output: ```sh
# Running `target/dhat/1` cargo solve 1 --dhat
# dhat: Total: 276 bytes in 3 blocks
# dhat: At t-gmax: 232 bytes in 2 blocks # output:
# dhat: At t-end: 0 bytes in 0 blocks # Running `target/dhat/1`
# dhat: The data has been saved to dhat-heap.json, and is viewable with dhat/dh_view.html # dhat: Total: 276 bytes in 3 blocks
# Part 1: 9001 (4.1ms) # dhat: At t-gmax: 232 bytes in 2 blocks
``` # dhat: At t-end: 0 bytes in 0 blocks
# dhat: The data has been saved to dhat-heap.json, and is viewable with dhat/dh_view.html
The command will output some basic stats to the command-line and generate a `dhat-heap.json` report in the repo root directory. # Part 1: 9001 (4.1ms)
```
You can pass the report a tool like [dh-view](https://nnethercote.github.io/dh_view/dh_view.html) to view a detailed breakdown of heap allocations.
The command will output some basic stats to the command-line and generate a `dhat-heap.json` report in the repo root directory.
### Use VS Code to debug your code
You can pass the report a tool like [dh-view](https://nnethercote.github.io/dh_view/dh_view.html) to view a detailed breakdown of heap allocations.
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] ### Use VS Code to debug your code
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] 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]
## Useful crates 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]
- [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. ## Useful crates
A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/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.
Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md)
A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/crates).
## Common pitfalls
Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md)
- **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.
## Common pitfalls
## Footnotes
- **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.
[^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. ## Footnotes
[^3]:
<img src="https://user-images.githubusercontent.com/1682504/198838369-453dc22c-c645-4803-afe0-fc50d5a3f00c.png" alt="Set a breakpoint" width="450" /> [^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.
[^4]: [^3]:
<img alt="Run debugger" src="https://user-images.githubusercontent.com/1682504/198838372-c89369f6-0d05-462e-a4c7-8cd97b0912e6.png" width="450" /> <img src="https://user-images.githubusercontent.com/1682504/198838369-453dc22c-c645-4803-afe0-fc50d5a3f00c.png" alt="Set a breakpoint" width="450" />
[^5]: [^4]:
<img alt="Inspect debugger state" src="https://user-images.githubusercontent.com/1682504/198838373-36df6996-23bf-4757-9335-0bc4c1db0276.png" width="450" /> <img alt="Run debugger" src="https://user-images.githubusercontent.com/1682504/198838372-c89369f6-0d05-462e-a4c7-8cd97b0912e6.png" width="450" />
[^5]:
<img alt="Inspect debugger state" src="https://user-images.githubusercontent.com/1682504/198838373-36df6996-23bf-4757-9335-0bc4c1db0276.png" width="450" />