Compare commits
21 commits
Author | SHA1 | Date | |
---|---|---|---|
6a3a324ea1 | |||
ae09ba2cc7 | |||
51e34a6b06 | |||
|
36608db7b3 | ||
|
a50047ab0d | ||
|
3abf88c16a | ||
|
10f8c0e1b4 | ||
|
a49a1d3c4a | ||
|
fe89ad7990 | ||
|
1d46931242 | ||
|
59866c3d70 | ||
|
3657f8c6dc | ||
|
e2062e33fd | ||
|
54f3c61092 | ||
|
a9ba30187c | ||
|
335f2631a0 | ||
|
234ac70c4e | ||
|
c82e1e2c08 | ||
|
3aef583c58 | ||
|
f43530b297 | ||
|
84208a663a |
26 changed files with 456 additions and 158 deletions
|
@ -9,4 +9,4 @@ all = "run --quiet --release -- all"
|
|||
time = "run --quiet --release -- time"
|
||||
|
||||
[env]
|
||||
AOC_YEAR = "2023"
|
||||
AOC_YEAR = "2024"
|
||||
|
|
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -6,13 +6,13 @@ env:
|
|||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
test:
|
||||
ci:
|
||||
runs-on: ubuntu-latest
|
||||
name: CI
|
||||
name: Continuous Integration
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up cargo cache
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
continue-on-error: false
|
||||
with:
|
||||
path: |
|
||||
|
|
4
.vscode/extensions.json
vendored
4
.vscode/extensions.json
vendored
|
@ -2,6 +2,6 @@
|
|||
"recommendations": [
|
||||
"vadimcn.vscode-lldb",
|
||||
"rust-lang.rust-analyzer",
|
||||
"serayuzgur.crates"
|
||||
"editorConfig.editorConfig"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
40
.vscode/launch.json
vendored
40
.vscode/launch.json
vendored
|
@ -7,13 +7,17 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'advent_of_code'",
|
||||
"name": "Debug unit tests for a solution",
|
||||
"cargo": {
|
||||
"args": ["test", "--no-run", "--bin=advent_of_code", "--package=advent_of_code"],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "bin"
|
||||
}
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
// replace with binary name (e.g. "01") here if you always
|
||||
// want to debug one file regardless of the active file in
|
||||
// the editor.
|
||||
"--bin=${fileBasenameNoExtension}",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
|
@ -21,15 +25,17 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'advent_of_code'",
|
||||
"name": "Debug a solution",
|
||||
"cargo": {
|
||||
"args": ["build", "--bin=advent_of_code", "--package=advent_of_code"],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "bin"
|
||||
}
|
||||
"args": [
|
||||
"build",
|
||||
// replace with binary name (e.g. "01") here if you always
|
||||
// want to debug one file regardless of the active file in
|
||||
// the editor
|
||||
"--bin=${fileBasenameNoExtension}",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
},
|
||||
"args": ["1"],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
|
@ -37,7 +43,13 @@
|
|||
"request": "launch",
|
||||
"name": "Debug unit tests in library 'advent_of_code'",
|
||||
"cargo": {
|
||||
"args": ["test", "--no-run", "--lib", "--package=advent_of_code"],
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--lib",
|
||||
"--features=test_lib",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "lib"
|
||||
|
|
94
Cargo.lock
generated
94
Cargo.lock
generated
|
@ -19,7 +19,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
|||
|
||||
[[package]]
|
||||
name = "advent_of_code"
|
||||
version = "0.10.0"
|
||||
version = "0.11.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dhat",
|
||||
|
@ -92,16 +92,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.31"
|
||||
version = "0.4.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
|
||||
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -112,9 +112,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
|
|||
|
||||
[[package]]
|
||||
name = "dhat"
|
||||
version = "0.3.2"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4f2aaf837aaf456f6706cb46386ba8dffd4013a757e36f4ea05c20dd46b209a3"
|
||||
checksum = "98cd11d84628e233de0ce467de10b8633f4ddaecafadefc86e13b84b8739b827"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"lazy_static",
|
||||
|
@ -267,7 +267,7 @@ dependencies = [
|
|||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -463,7 +463,7 @@ version = "0.51.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -472,13 +472,29 @@ version = "0.48.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
"windows_aarch64_gnullvm 0.48.5",
|
||||
"windows_aarch64_msvc 0.48.5",
|
||||
"windows_i686_gnu 0.48.5",
|
||||
"windows_i686_msvc 0.48.5",
|
||||
"windows_x86_64_gnu 0.48.5",
|
||||
"windows_x86_64_gnullvm 0.48.5",
|
||||
"windows_x86_64_msvc 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.52.6",
|
||||
"windows_aarch64_msvc 0.52.6",
|
||||
"windows_i686_gnu 0.52.6",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc 0.52.6",
|
||||
"windows_x86_64_gnu 0.52.6",
|
||||
"windows_x86_64_gnullvm 0.52.6",
|
||||
"windows_x86_64_msvc 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -487,38 +503,86 @@ version = "0.48.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
|
12
Cargo.toml
12
Cargo.toml
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "advent_of_code"
|
||||
version = "0.10.0"
|
||||
version = "0.11.0"
|
||||
authors = ["Felix Spöttel <1682504+fspoettel@users.noreply.github.com>"]
|
||||
edition = "2021"
|
||||
default-run = "advent_of_code"
|
||||
|
@ -20,7 +20,11 @@ today = ["chrono"]
|
|||
test_lib = []
|
||||
|
||||
[dependencies]
|
||||
chrono = { version = "0.4.31", optional = true }
|
||||
dhat = { version = "0.3.2", optional = true }
|
||||
|
||||
# Template dependencies
|
||||
chrono = { version = "0.4.38", optional = true }
|
||||
dhat = { version = "0.3.3", optional = true }
|
||||
pico-args = "0.5.0"
|
||||
tinyjson = "2"
|
||||
tinyjson = "2.5.1"
|
||||
|
||||
# Solution dependencies
|
||||
|
|
75
README.md
75
README.md
|
@ -6,6 +6,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
|
|||
|
||||
<!--- advent_readme_stars table --->
|
||||
|
||||
<!--- benchmarking table --->
|
||||
## Benchmarks
|
||||
|
||||
| Day | Part 1 | Part 2 |
|
||||
| :---: | :---: | :---: |
|
||||
| [Day 1](./src/bin/01.rs) | `89.5µs` | `125.4µs` |
|
||||
|
||||
**Total: 0.21ms**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
@ -52,7 +60,7 @@ Individual solutions live in the `./src/bin/` directory as separate binaries. _I
|
|||
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]
|
||||
> 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`.
|
||||
> 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
|
||||
|
||||
|
@ -89,16 +97,12 @@ cargo solve <day>
|
|||
|
||||
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 requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
|
||||
In order to submit part of a solution for checking, append the `--submit <part>` option to the `solve` command.
|
||||
Append the `--submit <part>` option to the `solve` command to submit your solution for checking.
|
||||
|
||||
### ➡️ Run all solutions
|
||||
|
||||
|
@ -116,15 +120,36 @@ cargo all
|
|||
# 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 and the `--time` flag outputs benchmarks.
|
||||
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
|
||||
### ➡️ Benchmark your solutions
|
||||
|
||||
The template can write benchmark times to the README via the `cargo time` command.
|
||||
```sh
|
||||
# example: `cargo time 8 --store`
|
||||
cargo time <day> [--all] [--store]
|
||||
|
||||
By default, this command checks for missing benchmarks, runs those solutions, and then updates the table. If you want to (re-)time all solutions, run `cargo time --all`. If you want to (re-)time one specific solution, run `cargo time <day>`.
|
||||
# output:
|
||||
# Day 08
|
||||
# ------
|
||||
# Part 1: 1 (39.0ns @ 10000 samples)
|
||||
# Part 2: 2 (39.0ns @ 10000 samples)
|
||||
#
|
||||
# Total (Run): 0.00ms
|
||||
#
|
||||
# Stored updated benchmarks.
|
||||
```
|
||||
|
||||
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.
|
||||
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:
|
||||
|
||||
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.
|
||||
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`.
|
||||
|
||||
> 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
|
||||
|
||||
|
@ -134,18 +159,6 @@ 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`.
|
||||
|
||||
### ➡️ Format code
|
||||
|
||||
```sh
|
||||
cargo fmt
|
||||
```
|
||||
|
||||
### ➡️ Lint code
|
||||
|
||||
```sh
|
||||
cargo clippy
|
||||
```
|
||||
|
||||
### ➡️ Read puzzle description
|
||||
|
||||
> [!IMPORTANT]
|
||||
|
@ -196,12 +209,24 @@ cargo today
|
|||
# ...the input...
|
||||
```
|
||||
|
||||
### ➡️ Format code
|
||||
|
||||
```sh
|
||||
cargo fmt
|
||||
```
|
||||
|
||||
### ➡️ Lint code
|
||||
|
||||
```sh
|
||||
cargo clippy
|
||||
```
|
||||
|
||||
## 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]
|
||||
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).
|
||||
|
||||
|
@ -229,7 +254,7 @@ Go to the _Variables_ tab in your repository settings and create the following v
|
|||
|
||||
✨ 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
|
||||
### Enable code formatting / clippy checks in the CI
|
||||
|
||||
Uncomment the respective sections in the `ci.yml` workflow.
|
||||
|
||||
|
|
0
data/examples/01.txt
Normal file
0
data/examples/01.txt
Normal file
6
data/examples/02.txt
Normal file
6
data/examples/02.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
0
data/examples/03.txt
Normal file
0
data/examples/03.txt
Normal file
0
data/examples/04.txt
Normal file
0
data/examples/04.txt
Normal file
85
src/bin/01.rs
Normal file
85
src/bin/01.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
advent_of_code::solution!(1);
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let total_distance: u32 = left
|
||||
.iter()
|
||||
.zip(right.iter())
|
||||
.map(|(&x, &y)| if x > y { x - y } else { y - x })
|
||||
.sum();
|
||||
|
||||
Some(total_distance)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let mut right_list_counts: HashMap<u32, u32> = HashMap::new();
|
||||
for &num in &right {
|
||||
*right_list_counts.entry(num).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let total_similarity_score: u32 = left
|
||||
.iter()
|
||||
.map(|&num| num * right_list_counts.get(&num).unwrap_or(&0))
|
||||
.sum();
|
||||
|
||||
Some(total_similarity_score)
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
42
src/bin/02.rs
Normal file
42
src/bin/02.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
advent_of_code::solution!(2);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
let mut num_safe: u32 = 0;
|
||||
let lines = input.lines();
|
||||
for line in lines {
|
||||
let levels: Vec<u32> = line
|
||||
.split_whitespace()
|
||||
.filter_map(|i| i.parse::<u32>().ok())
|
||||
.collect();
|
||||
|
||||
if levels
|
||||
.windows(2)
|
||||
.all(|w| (w[0] > w[1] && w[0] - w[1] <= 3) || (w[1] > w[0] && w[1] - w[0] <= 3))
|
||||
&& (levels.windows(2).all(|w| w[0] < w[1]) || levels.windows(2).all(|w| w[0] > w[1]))
|
||||
{
|
||||
num_safe += 1;
|
||||
}
|
||||
}
|
||||
Some(num_safe)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
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, Some(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
26
src/bin/03.rs
Normal file
26
src/bin/03.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
advent_of_code::solution!(3);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
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);
|
||||
}
|
||||
}
|
26
src/bin/04.rs
Normal file
26
src/bin/04.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
advent_of_code::solution!(4);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
pub mod template;
|
||||
|
||||
// Use this file to add helper functions and additional modules.
|
||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -20,21 +20,21 @@ mod args {
|
|||
Scaffold {
|
||||
day: Day,
|
||||
download: bool,
|
||||
overwrite: bool,
|
||||
},
|
||||
Solve {
|
||||
day: Day,
|
||||
release: bool,
|
||||
time: bool,
|
||||
dhat: bool,
|
||||
submit: Option<u8>,
|
||||
},
|
||||
All {
|
||||
release: bool,
|
||||
time: bool,
|
||||
},
|
||||
Time {
|
||||
all: bool,
|
||||
day: Option<Day>,
|
||||
store: bool,
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
Today,
|
||||
|
@ -46,14 +46,15 @@ mod args {
|
|||
let app_args = match args.subcommand()?.as_deref() {
|
||||
Some("all") => AppArguments::All {
|
||||
release: args.contains("--release"),
|
||||
time: args.contains("--time"),
|
||||
},
|
||||
Some("time") => {
|
||||
let all = args.contains("--all");
|
||||
let store = args.contains("--store");
|
||||
|
||||
AppArguments::Time {
|
||||
all,
|
||||
day: args.opt_free_from_str()?,
|
||||
store,
|
||||
}
|
||||
}
|
||||
Some("download") => AppArguments::Download {
|
||||
|
@ -65,12 +66,12 @@ mod args {
|
|||
Some("scaffold") => AppArguments::Scaffold {
|
||||
day: args.free_from_str()?,
|
||||
download: args.contains("--download"),
|
||||
overwrite: args.contains("--overwrite"),
|
||||
},
|
||||
Some("solve") => AppArguments::Solve {
|
||||
day: args.free_from_str()?,
|
||||
release: args.contains("--release"),
|
||||
submit: args.opt_value_from_str("--submit")?,
|
||||
time: args.contains("--time"),
|
||||
dhat: args.contains("--dhat"),
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
|
@ -101,12 +102,16 @@ fn main() {
|
|||
std::process::exit(1);
|
||||
}
|
||||
Ok(args) => match args {
|
||||
AppArguments::All { release, time } => all::handle(release, time),
|
||||
AppArguments::Time { day, all } => time::handle(day, all),
|
||||
AppArguments::All { release } => all::handle(release),
|
||||
AppArguments::Time { day, all, store } => time::handle(day, all, store),
|
||||
AppArguments::Download { day } => download::handle(day),
|
||||
AppArguments::Read { day } => read::handle(day),
|
||||
AppArguments::Scaffold { day, download } => {
|
||||
scaffold::handle(day);
|
||||
AppArguments::Scaffold {
|
||||
day,
|
||||
download,
|
||||
overwrite,
|
||||
} => {
|
||||
scaffold::handle(day, overwrite);
|
||||
if download {
|
||||
download::handle(day);
|
||||
}
|
||||
|
@ -114,15 +119,14 @@ fn main() {
|
|||
AppArguments::Solve {
|
||||
day,
|
||||
release,
|
||||
time,
|
||||
dhat,
|
||||
submit,
|
||||
} => solve::handle(day, release, time, dhat, submit),
|
||||
} => solve::handle(day, release, dhat, submit),
|
||||
#[cfg(feature = "today")]
|
||||
AppArguments::Today => {
|
||||
match Day::today() {
|
||||
Some(day) => {
|
||||
scaffold::handle(day);
|
||||
scaffold::handle(day, false);
|
||||
download::handle(day);
|
||||
read::handle(day)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::template::{all_days, run_multi::run_multi};
|
||||
|
||||
pub fn handle(is_release: bool, is_timed: bool) {
|
||||
run_multi(all_days().collect(), is_release, is_timed);
|
||||
pub fn handle(is_release: bool) {
|
||||
run_multi(&all_days().collect(), is_release, false);
|
||||
}
|
||||
|
|
|
@ -9,20 +9,30 @@ use crate::template::Day;
|
|||
const MODULE_TEMPLATE: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));
|
||||
|
||||
fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create_new(true).open(path)
|
||||
fn safe_create_file(path: &str, overwrite: bool) -> Result<File, std::io::Error> {
|
||||
let mut file = OpenOptions::new();
|
||||
if overwrite {
|
||||
file.create(true);
|
||||
} else {
|
||||
file.create_new(true);
|
||||
}
|
||||
file.truncate(true).write(true).open(path)
|
||||
}
|
||||
|
||||
fn create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create(true).open(path)
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(path)
|
||||
}
|
||||
|
||||
pub fn handle(day: Day) {
|
||||
pub fn handle(day: Day, overwrite: bool) {
|
||||
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) {
|
||||
let mut file = match safe_create_file(&module_path, overwrite) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create module file: {e}");
|
||||
|
@ -65,5 +75,5 @@ pub fn handle(day: Day) {
|
|||
}
|
||||
|
||||
println!("---");
|
||||
println!("🎄 Type `cargo solve {}` to run your solution.", day);
|
||||
println!("🎄 Type `cargo solve {day}` to run your solution.");
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::process::{Command, Stdio};
|
|||
|
||||
use crate::template::Day;
|
||||
|
||||
pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Option<u8>) {
|
||||
pub fn handle(day: Day, release: bool, dhat: bool, submit_part: Option<u8>) {
|
||||
let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()];
|
||||
|
||||
if dhat {
|
||||
|
@ -23,10 +23,6 @@ pub fn handle(day: Day, release: bool, time: bool, dhat: bool, submit_part: Opti
|
|||
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())
|
||||
|
|
|
@ -4,32 +4,37 @@ use crate::template::run_multi::run_multi;
|
|||
use crate::template::timings::Timings;
|
||||
use crate::template::{all_days, readme_benchmarks, Day};
|
||||
|
||||
pub fn handle(day: Option<Day>, recreate_all: bool) {
|
||||
pub fn handle(day: Option<Day>, run_all: bool, store: bool) {
|
||||
let stored_timings = Timings::read_from_file();
|
||||
|
||||
let days_to_run = day.map(|day| HashSet::from([day])).unwrap_or_else(|| {
|
||||
if recreate_all {
|
||||
all_days().collect()
|
||||
} else {
|
||||
// when the `--all` flag is not set, filter out days that are fully benched.
|
||||
all_days()
|
||||
.filter(|day| !stored_timings.is_day_complete(day))
|
||||
.collect()
|
||||
}
|
||||
});
|
||||
let days_to_run = day.map_or_else(
|
||||
|| {
|
||||
if run_all {
|
||||
all_days().collect()
|
||||
} else {
|
||||
// when the `--all` flag is not set, filter out days that are fully benched.
|
||||
all_days()
|
||||
.filter(|day| !stored_timings.is_day_complete(*day))
|
||||
.collect()
|
||||
}
|
||||
},
|
||||
|day| HashSet::from([day]),
|
||||
);
|
||||
|
||||
let timings = run_multi(days_to_run, true, true).unwrap();
|
||||
let timings = run_multi(&days_to_run, true, true).unwrap();
|
||||
|
||||
let merged_timings = stored_timings.merge(&timings);
|
||||
merged_timings.store_file().unwrap();
|
||||
if store {
|
||||
let merged_timings = stored_timings.merge(&timings);
|
||||
merged_timings.store_file().unwrap();
|
||||
|
||||
println!();
|
||||
match readme_benchmarks::update(merged_timings) {
|
||||
Ok(()) => {
|
||||
println!("Stored updated benchmarks.")
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!("Failed to store updated benchmarks.");
|
||||
println!();
|
||||
match readme_benchmarks::update(merged_timings) {
|
||||
Ok(()) => {
|
||||
println!("Stored updated benchmarks.");
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!("Failed to store updated benchmarks.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,10 @@ use std::fmt::Display;
|
|||
use std::str::FromStr;
|
||||
|
||||
#[cfg(feature = "today")]
|
||||
use chrono::{Datelike, Local};
|
||||
use chrono::{Datelike, FixedOffset, Utc};
|
||||
|
||||
#[cfg(feature = "today")]
|
||||
const SERVER_UTC_OFFSET: i32 = -5;
|
||||
|
||||
/// A valid day number of advent (i.e. an integer in range 1 to 25).
|
||||
///
|
||||
|
@ -44,7 +47,8 @@ impl Day {
|
|||
impl Day {
|
||||
/// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
|
||||
pub fn today() -> Option<Self> {
|
||||
let today = Local::now();
|
||||
let offset = FixedOffset::east_opt(SERVER_UTC_OFFSET * 3600)?;
|
||||
let today = Utc::now().with_timezone(&offset);
|
||||
if today.month() == 12 && today.day() <= 25 {
|
||||
Self::new(u8::try_from(today.day()).ok()?)
|
||||
} else {
|
||||
|
|
|
@ -7,6 +7,7 @@ use crate::template::Day;
|
|||
|
||||
static MARKER: &str = "<!--- benchmarking table --->";
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Parser(String),
|
||||
|
|
|
@ -7,31 +7,32 @@ use super::{
|
|||
timings::{Timing, Timings},
|
||||
};
|
||||
|
||||
pub fn run_multi(days_to_run: HashSet<Day>, is_release: bool, is_timed: bool) -> Option<Timings> {
|
||||
pub fn run_multi(days_to_run: &HashSet<Day>, is_release: bool, is_timed: bool) -> Option<Timings> {
|
||||
let mut timings: Vec<Timing> = Vec::with_capacity(days_to_run.len());
|
||||
|
||||
all_days().for_each(|day| {
|
||||
if day > 1 {
|
||||
println!();
|
||||
}
|
||||
let mut need_space = false;
|
||||
|
||||
println!("{ANSI_BOLD}Day {day}{ANSI_RESET}");
|
||||
println!("------");
|
||||
// NOTE: use non-duplicate, sorted day values.
|
||||
all_days()
|
||||
.filter(|day| days_to_run.contains(day))
|
||||
.for_each(|day| {
|
||||
if need_space {
|
||||
println!();
|
||||
}
|
||||
need_space = true;
|
||||
|
||||
if !days_to_run.contains(&day) {
|
||||
println!("Skipped.");
|
||||
return;
|
||||
}
|
||||
println!("{ANSI_BOLD}Day {day}{ANSI_RESET}");
|
||||
println!("------");
|
||||
|
||||
let output = child_commands::run_solution(day, is_timed, is_release).unwrap();
|
||||
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 output.is_empty() {
|
||||
println!("Not solved.");
|
||||
} else {
|
||||
let val = child_commands::parse_exec_time(&output, day);
|
||||
timings.push(val);
|
||||
}
|
||||
});
|
||||
|
||||
if is_timed {
|
||||
let timings = Timings { data: timings };
|
||||
|
@ -45,6 +46,7 @@ pub fn run_multi(days_to_run: HashSet<Day>, is_release: bool, is_timed: bool) ->
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
BrokenPipe,
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::{cmp, env, process};
|
|||
use crate::template::ANSI_BOLD;
|
||||
use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET};
|
||||
|
||||
pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
|
||||
pub fn run_part<I: Copy, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
|
||||
let part_str = format!("Part {part}");
|
||||
|
||||
let (result, duration, samples) =
|
||||
|
@ -25,15 +25,13 @@ pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, d
|
|||
/// 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<I: Clone, T>(
|
||||
fn run_timed<I: Copy, T>(
|
||||
func: impl Fn(I) -> T,
|
||||
input: I,
|
||||
hook: impl Fn(&T),
|
||||
) -> (T, Duration, u128) {
|
||||
let timer = Instant::now();
|
||||
let result = {
|
||||
let input = input.clone();
|
||||
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
let _profiler = dhat::Profiler::new_heap();
|
||||
|
||||
|
@ -52,27 +50,20 @@ fn run_timed<I: Clone, T>(
|
|||
(result, run.0, run.1)
|
||||
}
|
||||
|
||||
fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
|
||||
fn bench<I: Copy, T>(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 bench_iterations =
|
||||
(Duration::from_secs(1).as_nanos() / cmp::max(base_time.as_nanos(), 10)).clamp(10, 10000);
|
||||
|
||||
let mut timers: Vec<Duration> = vec![];
|
||||
|
||||
for _ in 0..bench_iterations {
|
||||
// need a clone here to make the borrow checker happy.
|
||||
let cloned = input.clone();
|
||||
let timer = Instant::now();
|
||||
black_box(func(black_box(cloned)));
|
||||
black_box(func(black_box(input)));
|
||||
timers.push(timer.elapsed());
|
||||
}
|
||||
|
||||
|
|
|
@ -31,17 +31,10 @@ impl Timings {
|
|||
|
||||
/// Rehydrate timings from a JSON file. If not present, returns empty timings.
|
||||
pub fn read_from_file() -> Self {
|
||||
let s = fs::read_to_string(TIMINGS_FILE_PATH)
|
||||
fs::read_to_string(TIMINGS_FILE_PATH)
|
||||
.map_err(|x| x.to_string())
|
||||
.and_then(Timings::try_from);
|
||||
|
||||
match s {
|
||||
Ok(timings) => timings,
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
Timings::default()
|
||||
}
|
||||
}
|
||||
.and_then(Timings::try_from)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Merge two sets of timings, overwriting `self` with `other` if present.
|
||||
|
@ -67,10 +60,10 @@ impl Timings {
|
|||
self.data.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64
|
||||
}
|
||||
|
||||
pub fn is_day_complete(&self, day: &Day) -> bool {
|
||||
pub fn is_day_complete(&self, day: Day) -> bool {
|
||||
self.data
|
||||
.iter()
|
||||
.any(|t| &t.day == day && t.part_1.is_some() && t.part_2.is_some())
|
||||
.any(|t| t.day == day && t.part_1.is_some() && t.part_2.is_some())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue