mirror of
https://github.com/RussellChamp/advent-of-code-2024-rust.git
synced 2026-10-27 23:52:29 +00:00
day 11 pt1
This commit is contained in:
1 parent
5b54368137
commit
012eefa9d8
3 files changed
+386
-355
No files matched your search
@@ -1,329 +1,329 @@
|
|||||||
[](https://github.com/RussellChamp/advent-of-code-2024-rust/actions/workflows/ci.yml)
|
[](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) | ⭐ | |
|
||||||
<!--- advent_readme_stars table --->
|
<!--- advent_readme_stars table --->
|
||||||
|
|
||||||
<!--- benchmarking table --->
|
<!--- benchmarking table --->
|
||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
| Day | Part 1 | Part 2 |
|
| Day | Part 1 | Part 2 |
|
||||||
| :---: | :---: | :---: |
|
| :---: | :---: | :---: |
|
||||||
| [Day 1](./src/bin/01.rs) | `153.0µs` | `237.3µs` |
|
| [Day 1](./src/bin/01.rs) | `153.0µs` | `237.3µs` |
|
||||||
| [Day 2](./src/bin/02.rs) | `205.7µs` | `530.7µs` |
|
| [Day 2](./src/bin/02.rs) | `205.7µs` | `530.7µs` |
|
||||||
| [Day 3](./src/bin/03.rs) | `615.8µs` | `743.8µs` |
|
| [Day 3](./src/bin/03.rs) | `615.8µs` | `743.8µs` |
|
||||||
|
|
||||||
**Total: 2.49ms**
|
**Total: 2.49ms**
|
||||||
<!--- benchmarking table --->
|
<!--- benchmarking table --->
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Template setup
|
## Template setup
|
||||||
|
|
||||||
This template supports all major OS (macOS, Linux, Windows).
|
This template supports all major OS (macOS, Linux, Windows).
|
||||||
|
|
||||||
### 📝 Create your repository
|
### 📝 Create your repository
|
||||||
|
|
||||||
1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github.
|
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.
|
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.
|
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.
|
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
|
### 💻 Setup rust
|
||||||
|
|
||||||
1. Install the [Rust toolchain](https://www.rust-lang.org/tools/install).
|
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.
|
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.
|
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).
|
✨ 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
|
## Usage
|
||||||
|
|
||||||
### ➡️ Scaffold a day
|
### ➡️ Scaffold a day
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo scaffold 1`
|
# example: `cargo scaffold 1`
|
||||||
cargo scaffold <day>
|
cargo scaffold <day>
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Created module file "src/bin/01.rs"
|
# Created module file "src/bin/01.rs"
|
||||||
# Created empty input file "data/inputs/01.txt"
|
# Created empty input file "data/inputs/01.txt"
|
||||||
# Created empty example file "data/examples/01.txt"
|
# Created empty example file "data/examples/01.txt"
|
||||||
# ---
|
# ---
|
||||||
# 🎄 Type `cargo solve 01` to run your solution.
|
# 🎄 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.
|
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.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.
|
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.
|
||||||
|
|
||||||
> [!TIP]
|
> [!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.
|
> 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.
|
||||||
|
|
||||||
### ➡️ Download input for a day
|
### ➡️ Download input for a day
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||||
|
|
||||||
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:
|
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:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo download 1`
|
# example: `cargo download 1`
|
||||||
cargo download <day>
|
cargo download <day>
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
||||||
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
||||||
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
||||||
# ---
|
# ---
|
||||||
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
||||||
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
||||||
```
|
```
|
||||||
|
|
||||||
### ➡️ Run solutions for a day
|
### ➡️ Run solutions for a day
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo solve 01`
|
# example: `cargo solve 01`
|
||||||
cargo solve <day>
|
cargo solve <day>
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s
|
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s
|
||||||
# Running `target/debug/01`
|
# Running `target/debug/01`
|
||||||
# Part 1: 42 (166.0ns)
|
# Part 1: 42 (166.0ns)
|
||||||
# Part 2: 42 (41.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.
|
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.
|
||||||
|
|
||||||
#### Submitting solutions
|
#### Submitting solutions
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||||
|
|
||||||
Append the `--submit <part>` option to the `solve` command to submit your solution for checking.
|
Append the `--submit <part>` option to the `solve` command to submit your solution for checking.
|
||||||
|
|
||||||
### ➡️ Run all solutions
|
### ➡️ Run all solutions
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo all
|
cargo all
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Running `target/release/advent_of_code`
|
# Running `target/release/advent_of_code`
|
||||||
# ----------
|
# ----------
|
||||||
# | Day 01 |
|
# | Day 01 |
|
||||||
# ----------
|
# ----------
|
||||||
# Part 1: 42 (19.0ns)
|
# Part 1: 42 (19.0ns)
|
||||||
# Part 2: 42 (19.0ns)
|
# Part 2: 42 (19.0ns)
|
||||||
# <...other days...>
|
# <...other days...>
|
||||||
# Total: 0.20ms
|
# 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.
|
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.
|
||||||
|
|
||||||
### ➡️ Benchmark your solutions
|
### ➡️ Benchmark your solutions
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo time 8 --store`
|
# example: `cargo time 8 --store`
|
||||||
cargo time <day> [--all] [--store]
|
cargo time <day> [--all] [--store]
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Day 08
|
# Day 08
|
||||||
# ------
|
# ------
|
||||||
# Part 1: 1 (39.0ns @ 10000 samples)
|
# Part 1: 1 (39.0ns @ 10000 samples)
|
||||||
# Part 2: 2 (39.0ns @ 10000 samples)
|
# Part 2: 2 (39.0ns @ 10000 samples)
|
||||||
#
|
#
|
||||||
# Total (Run): 0.00ms
|
# Total (Run): 0.00ms
|
||||||
#
|
#
|
||||||
# Stored updated benchmarks.
|
# Stored updated benchmarks.
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
`cargo time` has three modes of execution:
|
`cargo time` has three modes of execution:
|
||||||
|
|
||||||
1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest.
|
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.
|
2. `cargo time <day>` benches a single solution.
|
||||||
3. `cargo time --all` benches all solutions.
|
3. `cargo time --all` benches all solutions.
|
||||||
|
|
||||||
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`.
|
By default, `cargo time` does not write to the readme. In order to do so, append the `--store` flag: `cargo time --store`.
|
||||||
|
|
||||||
> 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.
|
> 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
|
### ➡️ Run all tests
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo test
|
cargo test
|
||||||
```
|
```
|
||||||
|
|
||||||
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`.
|
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`.
|
||||||
|
|
||||||
### ➡️ Read puzzle description
|
### ➡️ Read puzzle description
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo read 1`
|
# example: `cargo read 1`
|
||||||
cargo read <day>
|
cargo read <day>
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
||||||
# Fetching puzzle for day 1, 2022...
|
# Fetching puzzle for day 1, 2022...
|
||||||
# ...the input...
|
# ...the input...
|
||||||
```
|
```
|
||||||
|
|
||||||
### ➡️ Scaffold, download & read the current aoc day
|
### ➡️ Scaffold, download & read the current aoc day
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||||
|
|
||||||
During december, the `today` shorthand command can be used to:
|
During december, the `today` shorthand command can be used to:
|
||||||
|
|
||||||
- scaffold a solution for the current day
|
- scaffold a solution for the current day
|
||||||
- download its input
|
- download its input
|
||||||
- and read the puzzle
|
- and read the puzzle
|
||||||
|
|
||||||
in one go.
|
in one go.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# example: `cargo today` on December 1st
|
# example: `cargo today` on December 1st
|
||||||
cargo today
|
cargo today
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Created module file "src/bin/01.rs"
|
# Created module file "src/bin/01.rs"
|
||||||
# Created empty input file "data/inputs/01.txt"
|
# Created empty input file "data/inputs/01.txt"
|
||||||
# Created empty example file "data/examples/01.txt"
|
# Created empty example file "data/examples/01.txt"
|
||||||
# ---
|
# ---
|
||||||
# 🎄 Type `cargo solve 01` to run your solution.
|
# 🎄 Type `cargo solve 01` to run your solution.
|
||||||
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
||||||
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
||||||
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
||||||
# ---
|
# ---
|
||||||
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
||||||
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
||||||
#
|
#
|
||||||
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
||||||
# Fetching puzzle for day 1, 2022...
|
# Fetching puzzle for day 1, 2022...
|
||||||
# ...the input...
|
# ...the input...
|
||||||
```
|
```
|
||||||
|
|
||||||
### ➡️ Format code
|
### ➡️ Format code
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo fmt
|
cargo fmt
|
||||||
```
|
```
|
||||||
|
|
||||||
### ➡️ Lint code
|
### ➡️ Lint code
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo clippy
|
cargo clippy
|
||||||
```
|
```
|
||||||
|
|
||||||
## Optional template features
|
## Optional template features
|
||||||
|
|
||||||
### Configure aoc-cli integration
|
### Configure aoc-cli integration
|
||||||
|
|
||||||
1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
|
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]
|
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]
|
||||||
|
|
||||||
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).
|
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
|
### 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.
|
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:
|
To enable it, complete the following steps:
|
||||||
|
|
||||||
#### 1. Create a private leaderboard
|
#### 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}`.
|
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
|
#### 2. Set repository secrets
|
||||||
|
|
||||||
Go to the _Secrets_ tab in your repository settings and create the following 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_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_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_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:
|
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.
|
- `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.
|
✨ 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.
|
||||||
|
|
||||||
### Enable code formatting / clippy checks in the CI
|
### Enable code formatting / clippy checks in the CI
|
||||||
|
|
||||||
Uncomment the respective sections in the `ci.yml` workflow.
|
Uncomment the respective sections in the `ci.yml` workflow.
|
||||||
|
|
||||||
### Use DHAT to profile heap allocations
|
### Use DHAT to profile heap allocations
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo solve 1 --dhat
|
cargo solve 1 --dhat
|
||||||
|
|
||||||
# output:
|
# output:
|
||||||
# Running `target/dhat/1`
|
# Running `target/dhat/1`
|
||||||
# dhat: Total: 276 bytes in 3 blocks
|
# dhat: Total: 276 bytes in 3 blocks
|
||||||
# dhat: At t-gmax: 232 bytes in 2 blocks
|
# dhat: At t-gmax: 232 bytes in 2 blocks
|
||||||
# dhat: At t-end: 0 bytes in 0 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
|
# dhat: The data has been saved to dhat-heap.json, and is viewable with dhat/dh_view.html
|
||||||
# Part 1: 9001 (4.1ms)
|
# Part 1: 9001 (4.1ms)
|
||||||
```
|
```
|
||||||
|
|
||||||
The command will output some basic stats to the command-line and generate a `dhat-heap.json` report in the repo root directory.
|
The command will output some basic stats to the command-line and generate a `dhat-heap.json` report in the repo root directory.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
### Use VS Code to debug your code
|
### 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).
|
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]
|
2. Set breakpoints in your code. [^3]
|
||||||
3. Click _Debug_ next to the unit test or the _main_ function. [^4]
|
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]
|
4. The debugger will halt your program at the specific line and allow you to inspect the local stack. [^5]
|
||||||
|
|
||||||
## Useful crates
|
## Useful crates
|
||||||
|
|
||||||
- [itertools](https://crates.io/crates/itertools): Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles.
|
- [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.
|
- [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).
|
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)
|
Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md)
|
||||||
|
|
||||||
## Common pitfalls
|
## 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.
|
- **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
|
## 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.
|
[^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.
|
[^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]:
|
[^3]:
|
||||||
<img src="https://user-images.githubusercontent.com/1682504/198838369-453dc22c-c645-4803-afe0-fc50d5a3f00c.png" alt="Set a breakpoint" width="450" />
|
<img src="https://user-images.githubusercontent.com/1682504/198838369-453dc22c-c645-4803-afe0-fc50d5a3f00c.png" alt="Set a breakpoint" width="450" />
|
||||||
|
|
||||||
[^4]:
|
[^4]:
|
||||||
<img alt="Run debugger" src="https://user-images.githubusercontent.com/1682504/198838372-c89369f6-0d05-462e-a4c7-8cd97b0912e6.png" width="450" />
|
<img alt="Run debugger" src="https://user-images.githubusercontent.com/1682504/198838372-c89369f6-0d05-462e-a4c7-8cd97b0912e6.png" width="450" />
|
||||||
|
|
||||||
[^5]:
|
[^5]:
|
||||||
<img alt="Inspect debugger state" src="https://user-images.githubusercontent.com/1682504/198838373-36df6996-23bf-4757-9335-0bc4c1db0276.png" width="450" />
|
<img alt="Inspect debugger state" src="https://user-images.githubusercontent.com/1682504/198838373-36df6996-23bf-4757-9335-0bc4c1db0276.png" width="450" />
|
||||||
@@ -1 +1 @@
|
|||||||
0 1 10 99 999
|
125 17
|
||||||
+56
-25
@@ -5,44 +5,75 @@ use itertools::Itertools;
|
|||||||
advent_of_code::solution!(11);
|
advent_of_code::solution!(11);
|
||||||
|
|
||||||
pub fn part_one(input: &str) -> Option<u32> {
|
pub fn part_one(input: &str) -> Option<u32> {
|
||||||
let mut stones = input.strip_suffix("\n")?.split(" ").collect_vec();
|
let mut stones = vecify_stones(input);
|
||||||
|
step_stones(&mut stones, 25);
|
||||||
|
|
||||||
for step in 0..25 {
|
let total_count = stones.len();
|
||||||
println!("STEP {step}: {:?}", stones);
|
println!("FINAL STONES: {:?} stones", total_count);
|
||||||
// stones.clone_from(&process_stones(stones).iter().map(|s| s as &str).collect_vec());
|
|
||||||
// stones = next_step.clone().iter().map(|s| s as &str).collect_vec();
|
|
||||||
for idx in (0..stones.len()).rev() {
|
|
||||||
let stone = stones[idx];
|
|
||||||
let stone_length = stone.len();
|
|
||||||
if stone == "0" {
|
|
||||||
stones[idx] = "1";
|
|
||||||
} else if stone_length % 2 == 0 {
|
|
||||||
let stone_iter = stone.chars().into_iter();
|
|
||||||
// THIS IS BROKEN AND I"M VERY SAD
|
|
||||||
let first_half = stone_iter.by_ref().take(stone_length/2).collect::<String>().as_str();
|
|
||||||
|
|
||||||
let second_half = stone.chars().into_iter().skip(stone_length/2).take(stone_length/2).collect::<String>().as_str();
|
Some(total_count as u32)
|
||||||
stones[idx] = first_half;
|
|
||||||
stones.insert(idx + 1, second_half);
|
|
||||||
} else {
|
|
||||||
stones[idx] = stone.parse::<usize>().unwrap().mul(2024).to_string().as_str();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("FINAL STONES: {:?}", stones);
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part_two(input: &str) -> Option<u32> {
|
pub fn part_two(input: &str) -> Option<u32> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn step_stones(stones: &mut Vec<String>, max_steps: usize) {
|
||||||
|
for _step in 0..max_steps {
|
||||||
|
// println!("STEP #{step}: {:?} stones", stones.len());
|
||||||
|
// stones.clone_from(&process_stones(stones).iter().map(|s| s as &str).collect_vec());
|
||||||
|
// stones = next_step.clone().iter().map(|s| s as &str).collect_vec();
|
||||||
|
for idx in (0..stones.len()).rev() {
|
||||||
|
let stone = &stones[idx];
|
||||||
|
let stone_length = stone.len();
|
||||||
|
if stone == "0" {
|
||||||
|
stones[idx] = String::from("1");
|
||||||
|
} else if stone_length % 2 == 0 {
|
||||||
|
let mut stone_iter = stone.chars().into_iter();
|
||||||
|
let first_half = stone_iter.by_ref().take(stone_length/2).collect::<String>();
|
||||||
|
let second_half = stone_iter.by_ref().take(stone_length/2).collect::<String>().parse::<usize>().unwrap().to_string();
|
||||||
|
|
||||||
|
stones[idx] = first_half;
|
||||||
|
stones.insert(idx + 1, second_half);
|
||||||
|
} else {
|
||||||
|
let new_stone = stone.parse::<usize>().unwrap().mul(2024).to_string();
|
||||||
|
stones[idx] = new_stone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn vecify_stones(input: &str) -> Vec<String> {
|
||||||
|
input.strip_suffix("\n").unwrap().split(" ").map(|s| s.to_string()).collect_vec()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_step_stones() {
|
||||||
|
let mut stones= vecify_stones("125 17\n");
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("253000 1 7\n"));
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("253 0 2024 14168\n"));
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("512072 1 20 24 28676032\n"));
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("512 72 2024 2 0 2 4 2867 6032\n"));
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32\n"));
|
||||||
|
|
||||||
|
step_stones(&mut stones, 1);
|
||||||
|
assert_eq!(stones, vecify_stones("2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2\n"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part_one() {
|
fn test_part_one() {
|
||||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||||
|
|||||||
Reference in new issue
Block a user