Compare commits
114 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 | ||
|
c9671558ac | ||
|
874f57b359 | ||
|
4c4232139a | ||
|
f8a1368765 | ||
|
b696aa45da | ||
|
ff6b542114 | ||
|
72e1283c11 | ||
|
62bb12cb19 | ||
|
a79ce7ad6d | ||
|
b71cbddde9 | ||
|
c241820c41 | ||
|
a9dcd181aa | ||
|
df48bfe6ec | ||
|
9d064019c3 | ||
|
3260b731be | ||
|
47e4f227bd | ||
|
58916603d0 | ||
|
61cc69b124 | ||
|
38dc536984 | ||
|
b7b5a58f0f | ||
|
8376a61d0b | ||
|
47ac046562 | ||
|
ae1d965f45 | ||
|
fddbb2d987 | ||
|
751fde7451 | ||
|
656bd38f4c | ||
|
a3cc3a7efe | ||
|
6d9bf346a0 | ||
|
6653e856a6 | ||
|
6708184f1b | ||
|
d7af3dca9e | ||
|
f46d1e209c | ||
|
83e229c7df | ||
|
c11ac61252 | ||
|
1b10ee20ac | ||
|
70dac9329f | ||
|
d10ec0573e | ||
|
1c8ea27eae | ||
|
c8c3dc04be | ||
|
0908ad602b | ||
|
4d10812fd3 | ||
|
7ef01ab32f | ||
|
6ae29a25e8 | ||
|
6379e82105 | ||
|
b8b688f9d8 | ||
|
8b6ec2b607 | ||
|
5090d04b26 | ||
|
f0a1ebe8c4 | ||
|
d0b923f727 | ||
|
9bc635a056 | ||
|
7d663f1407 | ||
|
a6405f2edb | ||
|
f5b7707dd8 | ||
|
3ac30268ea | ||
|
ffc7cdb4ce | ||
|
9137ecc126 | ||
|
ab591ae1e9 | ||
|
c4507b6db8 | ||
|
8daca41984 | ||
|
00a63c8afc | ||
|
b66028a8f7 | ||
|
a67432389e | ||
|
132fb662de | ||
|
840d617dde | ||
|
f6b75e38a5 | ||
|
24ea1ba3a1 | ||
|
66ca5907f9 | ||
|
e1a4fa6657 | ||
|
b1149487c5 | ||
|
e50725168a | ||
|
3fb88bc654 | ||
|
412c0ae687 | ||
|
e45e930602 | ||
|
e327f00aa8 | ||
|
aae6a8f029 | ||
|
19cdd21019 | ||
|
1dca8ac5fe | ||
|
ec5859083a | ||
|
9eb5e488e2 | ||
|
12b206e6ca | ||
|
5c84286d9e | ||
|
1d991f7aab | ||
|
b82974cd4f | ||
|
30670f33fb | ||
|
34ead1f65e | ||
|
d30cd82753 | ||
|
a9dcb53c4a | ||
|
bd97e34f66 | ||
|
6613b6bfce | ||
|
afa39070de | ||
|
8f126cb448 | ||
|
49123f790a | ||
|
7b0b9f100c |
43 changed files with 2875 additions and 442 deletions
|
@ -1,2 +0,0 @@
|
|||
[alias]
|
||||
rr = "run --release"
|
12
.cargo/config.toml
Normal file
12
.cargo/config.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[alias]
|
||||
today = "run --quiet --release --features today -- today"
|
||||
scaffold = "run --quiet --release -- scaffold"
|
||||
download = "run --quiet --release -- download"
|
||||
read = "run --quiet --release -- read"
|
||||
|
||||
solve = "run --quiet --release -- solve"
|
||||
all = "run --quiet --release -- all"
|
||||
time = "run --quiet --release -- time"
|
||||
|
||||
[env]
|
||||
AOC_YEAR = "2024"
|
6
.devcontainer/devcontainer.json
Normal file
6
.devcontainer/devcontainer.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "rust-devcontainer",
|
||||
"image": "mcr.microsoft.com/devcontainers/rust:latest",
|
||||
"postCreateCommand": "rustc --version",
|
||||
"remoteUser": "vscode"
|
||||
}
|
|
@ -11,6 +11,7 @@ trim_trailing_whitespace = true
|
|||
|
||||
[*.txt]
|
||||
insert_final_newline = false
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
|
39
.github/workflows/ci.yml
vendored
39
.github/workflows/ci.yml
vendored
|
@ -1,22 +1,33 @@
|
|||
name: CI
|
||||
name: Continuous Integration
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
on: push
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
ci:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
name: Continuous Integration
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up cargo cache
|
||||
uses: actions/cache@v4
|
||||
continue-on-error: false
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/bin/
|
||||
~/.cargo/registry/index/
|
||||
~/.cargo/registry/cache/
|
||||
~/.cargo/git/db/
|
||||
target/
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||
restore-keys: ${{ runner.os }}-cargo-
|
||||
- name: cargo test
|
||||
run: cargo test
|
||||
# uncomment to enable clippy linter
|
||||
# - name: cargo clippy
|
||||
# run: cargo clippy -- -D warnings
|
||||
# uncomment to enable format linter
|
||||
# - name: cargo fmt
|
||||
# run: cargo fmt --check
|
||||
|
|
25
.github/workflows/readme-stars.yml
vendored
25
.github/workflows/readme-stars.yml
vendored
|
@ -1,29 +1,24 @@
|
|||
name: Update readme progress tracker
|
||||
name: Update readme ⭐️ progress
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "51 */4 * * *" # Every 4 hours
|
||||
# !Please set a different minute than 51 if you enable this!
|
||||
# schedule:
|
||||
# - cron: "51 */6 * * *" # Every 6 hours
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
update-readme:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ vars.AOC_ENABLED == 'true' }}
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
if: ${{ env.AOC_ENABLED }}
|
||||
env:
|
||||
AOC_ENABLED: ${{ secrets.AOC_ENABLED }}
|
||||
- uses: actions/checkout@v4
|
||||
- uses: k2bd/advent-readme-stars@v1
|
||||
if: ${{ env.AOC_ENABLED }}
|
||||
env:
|
||||
AOC_ENABLED: ${{ secrets.AOC_ENABLED }}
|
||||
with:
|
||||
userId: ${{ secrets.AOC_USER_ID }}
|
||||
sessionCookie: ${{ secrets.AOC_SESSION }}
|
||||
year: ${{ secrets.AOC_YEAR }}
|
||||
- uses: stefanzweifel/git-auto-commit-action@v4
|
||||
if: ${{ env.AOC_ENABLED }}
|
||||
env:
|
||||
AOC_ENABLED: ${{ secrets.AOC_ENABLED }}
|
||||
- uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: "update readme progess"
|
||||
commit_message: "update readme progress"
|
||||
|
|
14
.gitignore
vendored
14
.gitignore
vendored
|
@ -16,5 +16,15 @@ target/
|
|||
|
||||
# Advent of Code
|
||||
# @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3
|
||||
/src/inputs
|
||||
!/src/inputs/.keep
|
||||
|
||||
data/inputs/*
|
||||
!data/inputs/.keep
|
||||
data/puzzles/*
|
||||
!data/puzzles/.keep
|
||||
|
||||
# Dhat
|
||||
dhat-heap.json
|
||||
|
||||
# Benchmarks
|
||||
|
||||
data/timings.json
|
||||
|
|
7
.vscode/extensions.json
vendored
Normal file
7
.vscode/extensions.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"recommendations": [
|
||||
"vadimcn.vscode-lldb",
|
||||
"rust-lang.rust-analyzer",
|
||||
"editorConfig.editorConfig"
|
||||
]
|
||||
}
|
34
.vscode/launch.json
vendored
34
.vscode/launch.json
vendored
|
@ -7,18 +7,17 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'aoc'",
|
||||
"name": "Debug unit tests for a solution",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=aoc",
|
||||
"--package=aoc"
|
||||
// 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"
|
||||
],
|
||||
"filter": {
|
||||
"name": "aoc",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
|
@ -26,34 +25,33 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'aoc'",
|
||||
"name": "Debug a solution",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=aoc",
|
||||
"--package=aoc"
|
||||
// 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"
|
||||
],
|
||||
"filter": {
|
||||
"name": "aoc",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": ["1"],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in library 'aoc'",
|
||||
"name": "Debug unit tests in library 'advent_of_code'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--lib",
|
||||
"--package=aoc"
|
||||
"--features=test_lib",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
"filter": {
|
||||
"name": "aoc",
|
||||
"name": "advent_of_code",
|
||||
"kind": "lib"
|
||||
}
|
||||
},
|
||||
|
|
583
Cargo.lock
generated
583
Cargo.lock
generated
|
@ -3,5 +3,586 @@
|
|||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc"
|
||||
name = "addr2line"
|
||||
version = "0.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "advent_of_code"
|
||||
version = "0.11.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dhat",
|
||||
"pico-args",
|
||||
"tinyjson",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
|
||||
|
||||
[[package]]
|
||||
name = "dhat"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "98cd11d84628e233de0ce467de10b8633f4ddaecafadefc86e13b84b8739b827"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"lazy_static",
|
||||
"mintex",
|
||||
"parking_lot",
|
||||
"rustc-hash",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thousands",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.28.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mintex"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd7c5ba1c3b5a23418d7bbf98c71c3d4946a0125002129231da8d6b723d559cb"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"sys-info",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.32.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pico-args"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.70"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.193"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.193"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.108"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sys-info"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thousands"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bf63baf9f5039dadc247375c29eb13706706cfde997d0330d05aa63a77d8820"
|
||||
|
||||
[[package]]
|
||||
name = "tinyjson"
|
||||
version = "2.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ab95735ea2c8fd51154d01e39cf13912a78071c2d89abc49a7ef102a7dd725a"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.51.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
|
||||
dependencies = [
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"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]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
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"
|
||||
|
|
27
Cargo.toml
27
Cargo.toml
|
@ -1,9 +1,30 @@
|
|||
[package]
|
||||
name = "aoc"
|
||||
version = "0.2.0"
|
||||
name = "advent_of_code"
|
||||
version = "0.11.0"
|
||||
authors = ["Felix Spöttel <1682504+fspoettel@users.noreply.github.com>"]
|
||||
edition = "2021"
|
||||
default-run = "aoc"
|
||||
default-run = "advent_of_code"
|
||||
publish = false
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
doctest = false
|
||||
|
||||
[profile.dhat]
|
||||
inherits = "release"
|
||||
debug = 1
|
||||
|
||||
[features]
|
||||
dhat-heap = ["dhat"]
|
||||
today = ["chrono"]
|
||||
test_lib = []
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Template dependencies
|
||||
chrono = { version = "0.4.38", optional = true }
|
||||
dhat = { version = "0.3.3", optional = true }
|
||||
pico-args = "0.5.0"
|
||||
tinyjson = "2.5.1"
|
||||
|
||||
# Solution dependencies
|
||||
|
|
285
README.md
285
README.md
|
@ -1,21 +1,35 @@
|
|||
<img src="./.assets/christmas_ferris.png" width="164" align="center">
|
||||
<img src="./.assets/christmas_ferris.png" width="164">
|
||||
|
||||
# 🎄 [Advent of Code](https://adventofcode.com/)
|
||||
# 🎄 Advent of Code {year}
|
||||
|
||||

|
||||
[](https://github.com/fspoettel/advent-of-code-rust/actions/workflows/ci.yml)
|
||||
Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/).
|
||||
|
||||
<!--- advent_readme_stars table --->
|
||||
|
||||
## Setup
|
||||
<!--- benchmarking table --->
|
||||
## Benchmarks
|
||||
|
||||
### Create your _advent of code_ repository
|
||||
| Day | Part 1 | Part 2 |
|
||||
| :---: | :---: | :---: |
|
||||
| [Day 1](./src/bin/01.rs) | `89.5µs` | `125.4µs` |
|
||||
|
||||
**Total: 0.21ms**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
||||
## Template setup
|
||||
|
||||
This template supports all major OS (macOS, Linux, Windows).
|
||||
|
||||
### 📝 Create your repository
|
||||
|
||||
1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github.
|
||||
2. Click `Use this template` 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.
|
||||
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).
|
||||
2. (recommended) Install the [rust-analyzer](https://rust-analyzer.github.io/manual.html) extension for your code editor.
|
||||
|
@ -27,105 +41,181 @@
|
|||
|
||||
## Usage
|
||||
|
||||
### Setup new day
|
||||
### ➡️ Scaffold a day
|
||||
|
||||
```sh
|
||||
# example: `./bin/scaffold 1`
|
||||
./bin/scaffold <day>
|
||||
# example: `cargo scaffold 1`
|
||||
cargo scaffold <day>
|
||||
|
||||
# output:
|
||||
# Created module "src/bin/01.rs"
|
||||
# Created empty input file "src/inputs/01.txt"
|
||||
# Created empty example file "src/examples/01.txt"
|
||||
# Created module file "src/bin/01.rs"
|
||||
# Created empty input file "data/inputs/01.txt"
|
||||
# Created empty example file "data/examples/01.txt"
|
||||
# ---
|
||||
# 🎄 Type `cargo run --bin 01` to run your solution.
|
||||
# 🎄 Type `cargo solve 01` to run your solution.
|
||||
```
|
||||
|
||||
Individual solutions live in the `./src/bin/` directory as separate binaries.
|
||||
Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory.
|
||||
|
||||
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/master/bin/scaffold#L21-L52) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solution against example inputs. When editing a solution, `rust-analyzer` will display buttons for these actions above the unit tests.
|
||||
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 inputs 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.
|
||||
|
||||
> **Note**
|
||||
> This command requires configuring the optional [automatic input downloads](#automatic-input-downloads) feature.
|
||||
### ➡️ Download input for a day
|
||||
|
||||
> [!IMPORTANT]
|
||||
> 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:
|
||||
|
||||
```sh
|
||||
# example: `./bin/download 1`
|
||||
./bin/download <day>
|
||||
# example: `cargo download 1`
|
||||
cargo download <day>
|
||||
|
||||
# output:
|
||||
# Loaded session cookie from "/home/felix/.adventofcode.session".
|
||||
# Downloading input for day 1, 2021...
|
||||
# Saving puzzle input to "/tmp/tmp.MBdcAdL9Iw/input"...
|
||||
# Done!
|
||||
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
||||
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
||||
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
||||
# ---
|
||||
# 🎄 Successfully wrote input to "src/inputs/01.txt"!
|
||||
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
||||
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
||||
```
|
||||
|
||||
To download inputs for previous years, append the `--year` flag. _(example: `./bin/download 1 --year 2020`)_
|
||||
|
||||
Puzzle inputs are not checked into git. [See here](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3) why.
|
||||
|
||||
### Run solutions for a day
|
||||
### ➡️ Run solutions for a day
|
||||
|
||||
```sh
|
||||
# example: `cargo run --bin 01`
|
||||
cargo run --bin <day>
|
||||
# example: `cargo solve 01`
|
||||
cargo solve <day>
|
||||
|
||||
# output:
|
||||
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s
|
||||
# Running `target/debug/01`
|
||||
# 🎄 Part 1 🎄
|
||||
#
|
||||
# 6 (elapsed: 37.03µs)
|
||||
#
|
||||
# 🎄 Part 2 🎄
|
||||
#
|
||||
# 9 (elapsed: 33.18µs)
|
||||
# Part 1: 42 (166.0ns)
|
||||
# Part 2: 42 (41.0ns)
|
||||
```
|
||||
|
||||
To run an optimized version for benchmarking, use the `--release` flag or the alias `cargo rr --bin <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.
|
||||
|
||||
Displayed _timings_ show the raw execution time of your solution w/o overhead (e.g. file reads).
|
||||
#### Submitting solutions
|
||||
|
||||
### Run solutions for all days
|
||||
> [!IMPORTANT]
|
||||
> 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.
|
||||
|
||||
### ➡️ Run all solutions
|
||||
|
||||
```sh
|
||||
cargo run
|
||||
cargo all
|
||||
|
||||
# output:
|
||||
# Running `target/release/aoc`
|
||||
# Running `target/release/advent_of_code`
|
||||
# ----------
|
||||
# | Day 01 |
|
||||
# ----------
|
||||
# 🎄 Part 1 🎄
|
||||
#
|
||||
# 0 (elapsed: 170.00µs)
|
||||
#
|
||||
# 🎄 Part 2 🎄
|
||||
#
|
||||
# 0 (elapsed: 30.00µs)
|
||||
# Part 1: 42 (19.0ns)
|
||||
# Part 2: 42 (19.0ns)
|
||||
# <...other days...>
|
||||
# Total: 0.20ms
|
||||
```
|
||||
|
||||
To run an optimized version for benchmarking, use the `--release` flag or the alias `cargo rr`.
|
||||
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 timing_ is computed from individual solution _timings_ and excludes as much overhead as possible.
|
||||
### ➡️ Benchmark your solutions
|
||||
|
||||
### Run all solutions against example input
|
||||
```sh
|
||||
# example: `cargo time 8 --store`
|
||||
cargo time <day> [--all] [--store]
|
||||
|
||||
# output:
|
||||
# Day 08
|
||||
# ------
|
||||
# Part 1: 1 (39.0ns @ 10000 samples)
|
||||
# Part 2: 2 (39.0ns @ 10000 samples)
|
||||
#
|
||||
# Total (Run): 0.00ms
|
||||
#
|
||||
# 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.
|
||||
|
||||
`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
|
||||
|
||||
```sh
|
||||
cargo test
|
||||
```
|
||||
|
||||
### Format code
|
||||
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
|
||||
|
||||
> [!IMPORTANT]
|
||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
|
||||
```sh
|
||||
# example: `cargo read 1`
|
||||
cargo read <day>
|
||||
|
||||
# output:
|
||||
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
||||
# Fetching puzzle for day 1, 2022...
|
||||
# ...the input...
|
||||
```
|
||||
|
||||
### ➡️ Scaffold, download & read the current aoc day
|
||||
|
||||
> [!IMPORTANT]
|
||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
|
||||
During december, the `today` shorthand command can be used to:
|
||||
|
||||
- scaffold a solution for the current day
|
||||
- download its input
|
||||
- and read the puzzle
|
||||
|
||||
in one go.
|
||||
|
||||
```sh
|
||||
# example: `cargo today` on December 1st
|
||||
cargo today
|
||||
|
||||
# output:
|
||||
# Created module file "src/bin/01.rs"
|
||||
# Created empty input file "data/inputs/01.txt"
|
||||
# Created empty example file "data/examples/01.txt"
|
||||
# ---
|
||||
# 🎄 Type `cargo solve 01` to run your solution.
|
||||
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
|
||||
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
|
||||
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
|
||||
# ---
|
||||
# 🎄 Successfully wrote input to "data/inputs/01.txt".
|
||||
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
||||
#
|
||||
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
|
||||
# Fetching puzzle for day 1, 2022...
|
||||
# ...the input...
|
||||
```
|
||||
|
||||
### ➡️ Format code
|
||||
|
||||
```sh
|
||||
cargo fmt
|
||||
```
|
||||
|
||||
### Lint code
|
||||
### ➡️ Lint code
|
||||
|
||||
```sh
|
||||
cargo clippy
|
||||
|
@ -133,17 +223,20 @@ cargo clippy
|
|||
|
||||
## Optional template features
|
||||
|
||||
### Automatic input downloads
|
||||
### Configure aoc-cli integration
|
||||
|
||||
Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) and follow their setup instructions. Once installed, you can use the [download command](#download-inputs-for-a-day).
|
||||
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]
|
||||
|
||||
### Readme progress tracker
|
||||
Once installed, you can use the [download command](#download-input--description-for-a-day), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions).
|
||||
|
||||
### Automatically track ⭐️ progress in the readme
|
||||
|
||||
This template includes [a Github action](https://github.com/k2bd/advent-readme-stars) that automatically updates the readme with your advent of code progress.
|
||||
|
||||
To enable it, complete the following steps:
|
||||
|
||||
#### 1. Create 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}`.
|
||||
|
||||
|
@ -151,17 +244,69 @@ Go to the leaderboard page of the year you want to track and click _Private Lead
|
|||
|
||||
Go to the _Secrets_ tab in your repository settings and create the following secrets:
|
||||
|
||||
- `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker.
|
||||
- `AOC_USER_ID`: Go to [this page](https://adventofcode.com/settings) and copy your user id. It's the number behind the `#` symbol in the first name option. Example: `3031`
|
||||
- `AOC_YEAR`: the year you want to track. Example: `2021`
|
||||
- `AOC_SESSION`: an active session 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`.
|
||||
- `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.
|
||||
|
||||
> **Note**
|
||||
> 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.
|
||||
Go to the _Variables_ tab in your repository settings and create the following variable:
|
||||
|
||||
- `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker. After you complete AoC or no longer work on it, you can set this to `false` to disable the CI.
|
||||
|
||||
✨ You can now run this action manually via the _Run workflow_ button on the workflow page. If you want the workflow to run automatically, uncomment the `schedule` section in the `readme-stars.yml` workflow file or add a `push` trigger.
|
||||
|
||||
### Enable code formatting / clippy checks in the CI
|
||||
|
||||
Uncomment the respective sections in the `ci.yml` workflow.
|
||||
|
||||
### 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.
|
||||
|
||||
```sh
|
||||
cargo solve 1 --dhat
|
||||
|
||||
# output:
|
||||
# Running `target/dhat/1`
|
||||
# dhat: Total: 276 bytes in 3 blocks
|
||||
# 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
|
||||
# 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.
|
||||
|
||||
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
|
||||
|
||||
1. Install [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) and [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb).
|
||||
2. Set breakpoints in your code. [^3]
|
||||
3. Click _Debug_ next to the unit test or the _main_ function. [^4]
|
||||
4. The debugger will halt your program at the specific line and allow you to inspect the local stack. [^5]
|
||||
|
||||
## Useful crates
|
||||
|
||||
- [itertools](https://crates.io/crates/itertools): Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles.
|
||||
- [hashbrown](https://crates.io/crates/hashbrown): Faster hashing functions for `HashMap` and `HashSet`.
|
||||
- [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/master/README.md)
|
||||
A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/crates).
|
||||
|
||||
Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md)
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Integer overflows:** This template uses 32-bit integers by default because it is generally faster - for example when packed in large arrays or structs - than using 64-bit integers everywhere. For some problems, solutions for real input might exceed 32-bit integer space. While this is checked and panics in `debug` mode, integers [wrap](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow) in `release` mode, leading to wrong output when running your solution.
|
||||
|
||||
## Footnotes
|
||||
|
||||
[^1]: The session cookie might expire after a while (~1 month) which causes the downloads to fail. To fix this issue, refresh the `.adventofcode.session` file.
|
||||
[^2]: The session cookie might expire after a while (~1 month) which causes the automated workflow to fail. To fix this issue, refresh the AOC_SESSION secret.
|
||||
[^3]:
|
||||
<img src="https://user-images.githubusercontent.com/1682504/198838369-453dc22c-c645-4803-afe0-fc50d5a3f00c.png" alt="Set a breakpoint" width="450" />
|
||||
|
||||
[^4]:
|
||||
<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" />
|
||||
|
|
56
bin/download
56
bin/download
|
@ -1,56 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e;
|
||||
|
||||
if ! command -v 'aoc' &> /dev/null
|
||||
then
|
||||
echo "command \`aoc\` not found. Try running \`cargo install aoc-cli\` to install it."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
POSITIONAL=()
|
||||
while [[ $# -gt 0 ]]; do
|
||||
key="$1"
|
||||
|
||||
case $key in
|
||||
-y|--year)
|
||||
year="$2"
|
||||
shift
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
POSITIONAL+=("$1")
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
set -- "${POSITIONAL[@]}"
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
>&2 echo "Argument is required for day."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
day=$(echo "$1" | sed 's/^0*//');
|
||||
day_padded=$(printf %02d "$day");
|
||||
|
||||
filename="$day_padded";
|
||||
input_path="src/inputs/$filename.txt";
|
||||
|
||||
tmp_dir=$(mktemp -d);
|
||||
tmp_file_path="$tmp_dir/input";
|
||||
|
||||
if [[ "$year" != "" ]]
|
||||
then
|
||||
aoc download --day "$day" --year "$year" --file "$tmp_file_path";
|
||||
else
|
||||
aoc download --day "$day" --file "$tmp_file_path";
|
||||
fi
|
||||
|
||||
cat "$tmp_file_path" > "$input_path";
|
||||
echo "---"
|
||||
echo "🎄 Successfully wrote input to \"$input_path\"!"
|
||||
|
||||
trap "exit 1" HUP INT PIPE QUIT TERM
|
||||
trap 'rm -rf "$tmp_dir"' EXIT
|
64
bin/scaffold
64
bin/scaffold
|
@ -1,64 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e;
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
>&2 echo "Argument is required for day."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
day=$(echo "$1" | sed 's/^0*//');
|
||||
day_padded=$(printf %02d "$day");
|
||||
|
||||
filename="$day_padded";
|
||||
|
||||
input_path="src/inputs/$filename.txt";
|
||||
example_path="src/examples/$filename.txt";
|
||||
module_path="src/bin/$filename.rs";
|
||||
|
||||
touch "$module_path";
|
||||
|
||||
cat > "$module_path" <<EOF
|
||||
pub fn part_one(input: &str) -> u32 {
|
||||
0
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> u32 {
|
||||
0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
aoc::solve!(&aoc::read_file("inputs", DAYNUM), part_one, part_two)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
use aoc::read_file;
|
||||
let input = read_file("examples", DAYNUM);
|
||||
assert_eq!(part_one(&input), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
use aoc::read_file;
|
||||
let input = read_file("examples", DAYNUM);
|
||||
assert_eq!(part_two(&input), 0);
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
perl -pi -e "s,DAYNUM,$day,g" "$module_path";
|
||||
|
||||
echo "Created module \"$module_path\"";
|
||||
|
||||
touch "$input_path";
|
||||
echo "Created empty input file \"$input_path\"";
|
||||
|
||||
touch "$example_path";
|
||||
echo "Created empty example file \"$example_path\"";
|
||||
echo "---"
|
||||
echo "🎄 Type \`cargo run --bin $day_padded\` to run your solution."
|
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
0
data/inputs/.keep
Normal file
0
data/inputs/.keep
Normal file
0
data/puzzles/.keep
Normal file
0
data/puzzles/.keep
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,2 +0,0 @@
|
|||
// Use this file if you want to extract sections of your solutions.
|
||||
// Example import: `use aoc::helpers::example_fn;`
|
121
src/lib.rs
121
src/lib.rs
|
@ -1,120 +1,3 @@
|
|||
// This file contains template helpers.
|
||||
// Prefer `./helpers.rs` if you want to extract code from your solutions.
|
||||
use std::env;
|
||||
use std::fs;
|
||||
pub mod template;
|
||||
|
||||
pub mod helpers;
|
||||
|
||||
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
||||
pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! solve {
|
||||
($input:expr, $part_one:ident, $part_two:ident) => {{
|
||||
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
||||
use std::fmt::Display;
|
||||
use std::time::Instant;
|
||||
|
||||
fn print_result<T: Display>(func: impl FnOnce(&str) -> T, input: &str) {
|
||||
let timer = Instant::now();
|
||||
let result = func(input);
|
||||
let elapsed = timer.elapsed();
|
||||
println!(
|
||||
"{} {}(elapsed: {:.2?}){}",
|
||||
result, ANSI_ITALIC, elapsed, ANSI_RESET
|
||||
);
|
||||
}
|
||||
|
||||
println!("🎄 {}Part 1{} 🎄", ANSI_BOLD, ANSI_RESET);
|
||||
println!("");
|
||||
print_result($part_one, $input);
|
||||
println!("");
|
||||
println!("🎄 {}Part 2{} 🎄", ANSI_BOLD, ANSI_RESET);
|
||||
println!("");
|
||||
print_result($part_two, $input);
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn read_file(folder: &str, day: u8) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
|
||||
let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day));
|
||||
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file")
|
||||
}
|
||||
|
||||
fn parse_time(val: &str, postfix: &str) -> f64 {
|
||||
val.split(postfix).next().unwrap().parse().unwrap()
|
||||
}
|
||||
|
||||
pub fn parse_exec_time(output: &str) -> f64 {
|
||||
output.lines().fold(0_f64, |acc, l| {
|
||||
if !l.contains("elapsed:") {
|
||||
acc
|
||||
} else {
|
||||
let timing = l.split("(elapsed: ").last().unwrap();
|
||||
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
|
||||
// for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200
|
||||
if timing.contains("ns)") {
|
||||
acc // range below rounding precision.
|
||||
} else if timing.contains("µs)") {
|
||||
acc + parse_time(timing, "µs") / 1000_f64
|
||||
} else if timing.contains("ms)") {
|
||||
acc + parse_time(timing, "ms")
|
||||
} else if timing.contains("s)") {
|
||||
acc + parse_time(timing, "s") * 1000_f64
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333
|
||||
#[cfg(test)]
|
||||
macro_rules! assert_approx_eq {
|
||||
($a:expr, $b:expr) => {{
|
||||
let (a, b) = (&$a, &$b);
|
||||
assert!(
|
||||
(*a - *b).abs() < 1.0e-6,
|
||||
"{} is not approximately equal to {}",
|
||||
*a,
|
||||
*b
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_exec_time() {
|
||||
assert_approx_eq!(
|
||||
parse_exec_time(&format!(
|
||||
"🎄 Part 1 🎄\n0 (elapsed: 74.13ns){}\n🎄 Part 2 🎄\n0 (elapsed: 50.00ns){}",
|
||||
ANSI_RESET, ANSI_RESET
|
||||
)),
|
||||
0_f64
|
||||
);
|
||||
|
||||
assert_approx_eq!(
|
||||
parse_exec_time("🎄 Part 1 🎄\n0 (elapsed: 755µs)\n🎄 Part 2 🎄\n0 (elapsed: 700µs)"),
|
||||
1.455_f64
|
||||
);
|
||||
|
||||
assert_approx_eq!(
|
||||
parse_exec_time("🎄 Part 1 🎄\n0 (elapsed: 70µs)\n🎄 Part 2 🎄\n0 (elapsed: 1.45ms)"),
|
||||
1.52_f64
|
||||
);
|
||||
|
||||
assert_approx_eq!(
|
||||
parse_exec_time(
|
||||
"🎄 Part 1 🎄\n0 (elapsed: 10.3s)\n🎄 Part 2 🎄\n0 (elapsed: 100.50ms)"
|
||||
),
|
||||
10400.50_f64
|
||||
);
|
||||
}
|
||||
}
|
||||
// Use this file to add helper functions and additional modules.
|
||||
|
|
169
src/main.rs
169
src/main.rs
|
@ -1,35 +1,144 @@
|
|||
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
||||
use std::process::Command;
|
||||
use advent_of_code::template::commands::{all, download, read, scaffold, solve, time};
|
||||
use args::{parse, AppArguments};
|
||||
|
||||
#[cfg(feature = "today")]
|
||||
use advent_of_code::template::Day;
|
||||
#[cfg(feature = "today")]
|
||||
use std::process;
|
||||
|
||||
mod args {
|
||||
use advent_of_code::template::Day;
|
||||
use std::process;
|
||||
|
||||
pub enum AppArguments {
|
||||
Download {
|
||||
day: Day,
|
||||
},
|
||||
Read {
|
||||
day: Day,
|
||||
},
|
||||
Scaffold {
|
||||
day: Day,
|
||||
download: bool,
|
||||
overwrite: bool,
|
||||
},
|
||||
Solve {
|
||||
day: Day,
|
||||
release: bool,
|
||||
dhat: bool,
|
||||
submit: Option<u8>,
|
||||
},
|
||||
All {
|
||||
release: bool,
|
||||
},
|
||||
Time {
|
||||
all: bool,
|
||||
day: Option<Day>,
|
||||
store: bool,
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
Today,
|
||||
}
|
||||
|
||||
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
|
||||
let mut args = pico_args::Arguments::from_env();
|
||||
|
||||
let app_args = match args.subcommand()?.as_deref() {
|
||||
Some("all") => AppArguments::All {
|
||||
release: args.contains("--release"),
|
||||
},
|
||||
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 {
|
||||
day: args.free_from_str()?,
|
||||
},
|
||||
Some("read") => AppArguments::Read {
|
||||
day: args.free_from_str()?,
|
||||
},
|
||||
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")?,
|
||||
dhat: args.contains("--dhat"),
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
Some("today") => AppArguments::Today,
|
||||
Some(x) => {
|
||||
eprintln!("Unknown command: {x}");
|
||||
process::exit(1);
|
||||
}
|
||||
None => {
|
||||
eprintln!("No command specified.");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let remaining = args.finish();
|
||||
if !remaining.is_empty() {
|
||||
eprintln!("Warning: unknown argument(s): {remaining:?}.");
|
||||
}
|
||||
|
||||
Ok(app_args)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let total: f64 = (1..=25)
|
||||
.map(|day| {
|
||||
let day = format!("{:02}", day);
|
||||
|
||||
let cmd = Command::new("cargo")
|
||||
.args(&["run", "--release", "--bin", &day])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
println!("----------");
|
||||
println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET);
|
||||
println!("----------");
|
||||
|
||||
let output = String::from_utf8(cmd.stdout).unwrap();
|
||||
let is_empty = output.is_empty();
|
||||
|
||||
println!("{}", if is_empty { "Not solved." } else { &output });
|
||||
|
||||
if is_empty {
|
||||
0_f64
|
||||
} else {
|
||||
aoc::parse_exec_time(&output)
|
||||
match parse() {
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
|
||||
println!(
|
||||
"{}Total:{} {}{:.2}ms{}",
|
||||
ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total, ANSI_RESET
|
||||
Ok(args) => match args {
|
||||
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,
|
||||
overwrite,
|
||||
} => {
|
||||
scaffold::handle(day, overwrite);
|
||||
if download {
|
||||
download::handle(day);
|
||||
}
|
||||
}
|
||||
AppArguments::Solve {
|
||||
day,
|
||||
release,
|
||||
dhat,
|
||||
submit,
|
||||
} => solve::handle(day, release, dhat, submit),
|
||||
#[cfg(feature = "today")]
|
||||
AppArguments::Today => {
|
||||
match Day::today() {
|
||||
Some(day) => {
|
||||
scaffold::handle(day, false);
|
||||
download::handle(day);
|
||||
read::handle(day)
|
||||
}
|
||||
None => {
|
||||
eprintln!(
|
||||
"`today` command can only be run between the 1st and \
|
||||
the 25th of december. Please use `scaffold` with a specific day."
|
||||
);
|
||||
process::exit(1)
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
26
src/template.txt
Normal file
26
src/template.txt
Normal file
|
@ -0,0 +1,26 @@
|
|||
advent_of_code::solution!(%DAY_NUMBER%);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
125
src/template/aoc_cli.rs
Normal file
125
src/template/aoc_cli.rs
Normal file
|
@ -0,0 +1,125 @@
|
|||
/// Wrapper module around the "aoc-cli" command-line.
|
||||
use std::{
|
||||
fmt::Display,
|
||||
process::{Command, Output, Stdio},
|
||||
};
|
||||
|
||||
use crate::template::Day;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AocCommandError {
|
||||
CommandNotFound,
|
||||
CommandNotCallable,
|
||||
BadExitStatus(Output),
|
||||
}
|
||||
|
||||
impl Display for AocCommandError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
AocCommandError::CommandNotFound => write!(f, "aoc-cli is not present in environment."),
|
||||
AocCommandError::CommandNotCallable => write!(f, "aoc-cli could not be called."),
|
||||
AocCommandError::BadExitStatus(_) => {
|
||||
write!(f, "aoc-cli exited with a non-zero status.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check() -> Result<(), AocCommandError> {
|
||||
Command::new("aoc")
|
||||
.arg("-V")
|
||||
.output()
|
||||
.map_err(|_| AocCommandError::CommandNotFound)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read(day: Day) -> Result<Output, AocCommandError> {
|
||||
let puzzle_path = get_puzzle_path(day);
|
||||
|
||||
let args = build_args(
|
||||
"read",
|
||||
&[
|
||||
"--description-only".into(),
|
||||
"--puzzle-file".into(),
|
||||
puzzle_path,
|
||||
],
|
||||
day,
|
||||
);
|
||||
|
||||
call_aoc_cli(&args)
|
||||
}
|
||||
|
||||
pub fn download(day: Day) -> Result<Output, AocCommandError> {
|
||||
let input_path = get_input_path(day);
|
||||
let puzzle_path = get_puzzle_path(day);
|
||||
|
||||
let args = build_args(
|
||||
"download",
|
||||
&[
|
||||
"--overwrite".into(),
|
||||
"--input-file".into(),
|
||||
input_path.to_string(),
|
||||
"--puzzle-file".into(),
|
||||
puzzle_path.to_string(),
|
||||
],
|
||||
day,
|
||||
);
|
||||
|
||||
let output = call_aoc_cli(&args)?;
|
||||
println!("---");
|
||||
println!("🎄 Successfully wrote input to \"{}\".", &input_path);
|
||||
println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path);
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn submit(day: Day, part: u8, result: &str) -> Result<Output, AocCommandError> {
|
||||
// workaround: the argument order is inverted for submit.
|
||||
let mut args = build_args("submit", &[], day);
|
||||
args.push(part.to_string());
|
||||
args.push(result.to_string());
|
||||
call_aoc_cli(&args)
|
||||
}
|
||||
|
||||
fn get_input_path(day: Day) -> String {
|
||||
format!("data/inputs/{day}.txt")
|
||||
}
|
||||
|
||||
fn get_puzzle_path(day: Day) -> String {
|
||||
format!("data/puzzles/{day}.md")
|
||||
}
|
||||
|
||||
fn get_year() -> Option<u16> {
|
||||
match std::env::var("AOC_YEAR") {
|
||||
Ok(x) => x.parse().ok().or(None),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_args(command: &str, args: &[String], day: Day) -> Vec<String> {
|
||||
let mut cmd_args = args.to_vec();
|
||||
|
||||
if let Some(year) = get_year() {
|
||||
cmd_args.push("--year".into());
|
||||
cmd_args.push(year.to_string());
|
||||
}
|
||||
|
||||
cmd_args.append(&mut vec!["--day".into(), day.to_string(), command.into()]);
|
||||
|
||||
cmd_args
|
||||
}
|
||||
|
||||
fn call_aoc_cli(args: &[String]) -> Result<Output, AocCommandError> {
|
||||
// println!("Calling >aoc with: {}", args.join(" "));
|
||||
let output = Command::new("aoc")
|
||||
.args(args)
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
.output()
|
||||
.map_err(|_| AocCommandError::CommandNotCallable)?;
|
||||
|
||||
if output.status.success() {
|
||||
Ok(output)
|
||||
} else {
|
||||
Err(AocCommandError::BadExitStatus(output))
|
||||
}
|
||||
}
|
5
src/template/commands/all.rs
Normal file
5
src/template/commands/all.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use crate::template::{all_days, run_multi::run_multi};
|
||||
|
||||
pub fn handle(is_release: bool) {
|
||||
run_multi(&all_days().collect(), is_release, false);
|
||||
}
|
14
src/template/commands/download.rs
Normal file
14
src/template/commands/download.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use crate::template::{aoc_cli, Day};
|
||||
use std::process;
|
||||
|
||||
pub fn handle(day: Day) {
|
||||
if aoc_cli::check().is_err() {
|
||||
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
if let Err(e) = aoc_cli::download(day) {
|
||||
eprintln!("failed to call aoc-cli: {e}");
|
||||
process::exit(1);
|
||||
};
|
||||
}
|
6
src/template/commands/mod.rs
Normal file
6
src/template/commands/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
pub mod all;
|
||||
pub mod download;
|
||||
pub mod read;
|
||||
pub mod scaffold;
|
||||
pub mod solve;
|
||||
pub mod time;
|
15
src/template/commands/read.rs
Normal file
15
src/template/commands/read.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::process;
|
||||
|
||||
use crate::template::{aoc_cli, Day};
|
||||
|
||||
pub fn handle(day: Day) {
|
||||
if aoc_cli::check().is_err() {
|
||||
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
if let Err(e) = aoc_cli::read(day) {
|
||||
eprintln!("failed to call aoc-cli: {e}");
|
||||
process::exit(1);
|
||||
};
|
||||
}
|
79
src/template/commands/scaffold.rs
Normal file
79
src/template/commands/scaffold.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use std::{
|
||||
fs::{File, OpenOptions},
|
||||
io::Write,
|
||||
process,
|
||||
};
|
||||
|
||||
use crate::template::Day;
|
||||
|
||||
const MODULE_TEMPLATE: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));
|
||||
|
||||
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)
|
||||
.truncate(true)
|
||||
.open(path)
|
||||
}
|
||||
|
||||
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, overwrite) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create module file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
match file.write_all(
|
||||
MODULE_TEMPLATE
|
||||
.replace("%DAY_NUMBER%", &day.into_inner().to_string())
|
||||
.as_bytes(),
|
||||
) {
|
||||
Ok(()) => {
|
||||
println!("Created module file \"{}\"", &module_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to write module contents: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
match create_file(&input_path) {
|
||||
Ok(_) => {
|
||||
println!("Created empty input file \"{}\"", &input_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create input file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
match create_file(&example_path) {
|
||||
Ok(_) => {
|
||||
println!("Created empty example file \"{}\"", &example_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create example file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
println!("---");
|
||||
println!("🎄 Type `cargo solve {day}` to run your solution.");
|
||||
}
|
34
src/template/commands/solve.rs
Normal file
34
src/template/commands/solve.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use std::process::{Command, Stdio};
|
||||
|
||||
use crate::template::Day;
|
||||
|
||||
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 {
|
||||
cmd_args.extend([
|
||||
"--profile".to_string(),
|
||||
"dhat".to_string(),
|
||||
"--features".to_string(),
|
||||
"dhat-heap".to_string(),
|
||||
]);
|
||||
} else if release {
|
||||
cmd_args.push("--release".to_string());
|
||||
}
|
||||
|
||||
cmd_args.push("--".to_string());
|
||||
|
||||
if let Some(submit_part) = submit_part {
|
||||
cmd_args.push("--submit".to_string());
|
||||
cmd_args.push(submit_part.to_string());
|
||||
}
|
||||
|
||||
let mut cmd = Command::new("cargo")
|
||||
.args(&cmd_args)
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
cmd.wait().unwrap();
|
||||
}
|
40
src/template/commands/time.rs
Normal file
40
src/template/commands/time.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
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>, run_all: bool, store: bool) {
|
||||
let stored_timings = Timings::read_from_file();
|
||||
|
||||
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();
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
192
src/template/day.rs
Normal file
192
src/template/day.rs
Normal file
|
@ -0,0 +1,192 @@
|
|||
use std::error::Error;
|
||||
use std::fmt::Display;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[cfg(feature = "today")]
|
||||
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).
|
||||
///
|
||||
/// # Display
|
||||
/// This value displays as a two digit number.
|
||||
///
|
||||
/// ```
|
||||
/// # use advent_of_code::Day;
|
||||
/// let day = Day::new(8).unwrap();
|
||||
/// assert_eq!(day.to_string(), "08")
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Day(u8);
|
||||
|
||||
impl Day {
|
||||
/// Creates a [`Day`] from the provided value if it's in the valid range,
|
||||
/// returns [`None`] otherwise.
|
||||
pub fn new(day: u8) -> Option<Self> {
|
||||
if day == 0 || day > 25 {
|
||||
return None;
|
||||
}
|
||||
Some(Self(day))
|
||||
}
|
||||
|
||||
// Not part of the public API
|
||||
#[doc(hidden)]
|
||||
pub const fn __new_unchecked(day: u8) -> Self {
|
||||
Self(day)
|
||||
}
|
||||
|
||||
/// Converts the [`Day`] into an [`u8`].
|
||||
pub fn into_inner(self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "today")]
|
||||
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 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 {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Day {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:02}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<u8> for Day {
|
||||
fn eq(&self, other: &u8) -> bool {
|
||||
self.0.eq(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd<u8> for Day {
|
||||
fn partial_cmp(&self, other: &u8) -> Option<std::cmp::Ordering> {
|
||||
self.0.partial_cmp(other)
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
impl FromStr for Day {
|
||||
type Err = DayFromStrError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let day = s.parse().map_err(|_| DayFromStrError)?;
|
||||
Self::new(day).ok_or(DayFromStrError)
|
||||
}
|
||||
}
|
||||
|
||||
/// An error which can be returned when parsing a [`Day`].
|
||||
#[derive(Debug)]
|
||||
pub struct DayFromStrError;
|
||||
|
||||
impl Error for DayFromStrError {}
|
||||
|
||||
impl Display for DayFromStrError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("expecting a day number between 1 and 25")
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/// An iterator that yields every day of advent from the 1st to the 25th.
|
||||
pub fn all_days() -> AllDays {
|
||||
AllDays::new()
|
||||
}
|
||||
|
||||
/// An iterator that yields every day of advent from the 1st to the 25th.
|
||||
pub struct AllDays {
|
||||
current: u8,
|
||||
}
|
||||
|
||||
impl AllDays {
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
Self { current: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for AllDays {
|
||||
type Item = Day;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current > 25 {
|
||||
return None;
|
||||
}
|
||||
// NOTE: the iterator starts at 1 and we have verified that the value is not above 25.
|
||||
let day = Day(self.current);
|
||||
self.current += 1;
|
||||
|
||||
Some(day)
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/// Creates a [`Day`] value in a const context.
|
||||
#[macro_export]
|
||||
macro_rules! day {
|
||||
($day:expr) => {{
|
||||
const _ASSERT: () = assert!(
|
||||
$day != 0 && $day <= 25,
|
||||
concat!(
|
||||
"invalid day number `",
|
||||
$day,
|
||||
"`, expecting a value between 1 and 25"
|
||||
),
|
||||
);
|
||||
$crate::template::Day::__new_unchecked($day)
|
||||
}};
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
#[cfg(feature = "test_lib")]
|
||||
mod tests {
|
||||
use super::{all_days, Day};
|
||||
|
||||
#[test]
|
||||
fn all_days_iterator() {
|
||||
let mut iter = all_days();
|
||||
|
||||
assert_eq!(iter.next(), Some(Day(1)));
|
||||
assert_eq!(iter.next(), Some(Day(2)));
|
||||
assert_eq!(iter.next(), Some(Day(3)));
|
||||
assert_eq!(iter.next(), Some(Day(4)));
|
||||
assert_eq!(iter.next(), Some(Day(5)));
|
||||
assert_eq!(iter.next(), Some(Day(6)));
|
||||
assert_eq!(iter.next(), Some(Day(7)));
|
||||
assert_eq!(iter.next(), Some(Day(8)));
|
||||
assert_eq!(iter.next(), Some(Day(9)));
|
||||
assert_eq!(iter.next(), Some(Day(10)));
|
||||
assert_eq!(iter.next(), Some(Day(11)));
|
||||
assert_eq!(iter.next(), Some(Day(12)));
|
||||
assert_eq!(iter.next(), Some(Day(13)));
|
||||
assert_eq!(iter.next(), Some(Day(14)));
|
||||
assert_eq!(iter.next(), Some(Day(15)));
|
||||
assert_eq!(iter.next(), Some(Day(16)));
|
||||
assert_eq!(iter.next(), Some(Day(17)));
|
||||
assert_eq!(iter.next(), Some(Day(18)));
|
||||
assert_eq!(iter.next(), Some(Day(19)));
|
||||
assert_eq!(iter.next(), Some(Day(20)));
|
||||
assert_eq!(iter.next(), Some(Day(21)));
|
||||
assert_eq!(iter.next(), Some(Day(22)));
|
||||
assert_eq!(iter.next(), Some(Day(23)));
|
||||
assert_eq!(iter.next(), Some(Day(24)));
|
||||
assert_eq!(iter.next(), Some(Day(25)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
68
src/template/mod.rs
Normal file
68
src/template/mod.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use std::{env, fs};
|
||||
|
||||
pub mod aoc_cli;
|
||||
pub mod commands;
|
||||
pub mod runner;
|
||||
|
||||
pub use day::*;
|
||||
|
||||
mod day;
|
||||
mod readme_benchmarks;
|
||||
mod run_multi;
|
||||
mod timings;
|
||||
|
||||
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
||||
pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||
|
||||
/// Helper function that reads a text file to a string.
|
||||
#[must_use]
|
||||
pub fn read_file(folder: &str, day: Day) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let filepath = cwd.join("data").join(folder).join(format!("{day}.txt"));
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file")
|
||||
}
|
||||
|
||||
/// Helper function that reads a text file to string, appending a part suffix. E.g. like `01-2.txt`.
|
||||
#[must_use]
|
||||
pub fn read_file_part(folder: &str, day: Day, part: u8) -> String {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let filepath = cwd
|
||||
.join("data")
|
||||
.join(folder)
|
||||
.join(format!("{day}-{part}.txt"));
|
||||
let f = fs::read_to_string(filepath);
|
||||
f.expect("could not open input file")
|
||||
}
|
||||
|
||||
/// Creates the constant `DAY` and sets up the input and runner for each part.
|
||||
///
|
||||
/// The optional, second parameter (1 or 2) allows you to only run a single part of the solution.
|
||||
#[macro_export]
|
||||
macro_rules! solution {
|
||||
($day:expr) => {
|
||||
$crate::solution!(@impl $day, [part_one, 1] [part_two, 2]);
|
||||
};
|
||||
($day:expr, 1) => {
|
||||
$crate::solution!(@impl $day, [part_one, 1]);
|
||||
};
|
||||
($day:expr, 2) => {
|
||||
$crate::solution!(@impl $day, [part_two, 2]);
|
||||
};
|
||||
|
||||
(@impl $day:expr, $( [$func:expr, $part:expr] )*) => {
|
||||
/// The current day.
|
||||
const DAY: $crate::template::Day = $crate::day!($day);
|
||||
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
#[global_allocator]
|
||||
static ALLOC: dhat::Alloc = dhat::Alloc;
|
||||
|
||||
fn main() {
|
||||
use $crate::template::runner::*;
|
||||
let input = $crate::template::read_file("inputs", DAY);
|
||||
$( run_part($func, &input, DAY, $part); )*
|
||||
}
|
||||
};
|
||||
}
|
183
src/template/readme_benchmarks.rs
Normal file
183
src/template/readme_benchmarks.rs
Normal file
|
@ -0,0 +1,183 @@
|
|||
/// Module that updates the readme me with timing information.
|
||||
/// The approach taken is similar to how `aoc-readme-stars` handles this.
|
||||
use std::{fs, io};
|
||||
|
||||
use crate::template::timings::Timings;
|
||||
use crate::template::Day;
|
||||
|
||||
static MARKER: &str = "<!--- benchmarking table --->";
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Parser(String),
|
||||
IO(io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Error::IO(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TablePosition {
|
||||
pos_start: usize,
|
||||
pos_end: usize,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_path_for_bin(day: Day) -> String {
|
||||
format!("./src/bin/{day}.rs")
|
||||
}
|
||||
|
||||
fn locate_table(readme: &str) -> Result<TablePosition, Error> {
|
||||
let matches: Vec<_> = readme.match_indices(MARKER).collect();
|
||||
|
||||
if matches.len() > 2 {
|
||||
return Err(Error::Parser(
|
||||
"{}: too many occurences of marker in README.".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let pos_start = matches
|
||||
.first()
|
||||
.map(|m| m.0)
|
||||
.ok_or_else(|| Error::Parser("Could not find table start position.".into()))?;
|
||||
|
||||
let pos_end = matches
|
||||
.last()
|
||||
.map(|m| m.0 + m.1.len())
|
||||
.ok_or_else(|| Error::Parser("Could not find table end position.".into()))?;
|
||||
|
||||
Ok(TablePosition { pos_start, pos_end })
|
||||
}
|
||||
|
||||
fn construct_table(prefix: &str, timings: Timings, total_millis: f64) -> String {
|
||||
let header = format!("{prefix} Benchmarks");
|
||||
|
||||
let mut lines: Vec<String> = vec![
|
||||
MARKER.into(),
|
||||
header,
|
||||
String::new(),
|
||||
"| Day | Part 1 | Part 2 |".into(),
|
||||
"| :---: | :---: | :---: |".into(),
|
||||
];
|
||||
|
||||
for timing in timings.data {
|
||||
let path = get_path_for_bin(timing.day);
|
||||
lines.push(format!(
|
||||
"| [Day {}]({}) | `{}` | `{}` |",
|
||||
timing.day.into_inner(),
|
||||
path,
|
||||
timing.part_1.unwrap_or_else(|| "-".into()),
|
||||
timing.part_2.unwrap_or_else(|| "-".into())
|
||||
));
|
||||
}
|
||||
|
||||
lines.push(String::new());
|
||||
lines.push(format!("**Total: {total_millis:.2}ms**"));
|
||||
lines.push(MARKER.into());
|
||||
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
fn update_content(s: &mut String, timings: Timings, total_millis: f64) -> Result<(), Error> {
|
||||
let positions = locate_table(s)?;
|
||||
let table = construct_table("##", timings, total_millis);
|
||||
s.replace_range(positions.pos_start..positions.pos_end, &table);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update(timings: Timings) -> Result<(), Error> {
|
||||
let path = "README.md";
|
||||
let mut readme = String::from_utf8_lossy(&fs::read(path)?).to_string();
|
||||
let total_millis = timings.total_millis();
|
||||
update_content(&mut readme, timings, total_millis)?;
|
||||
fs::write(path, &readme)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "test_lib")]
|
||||
mod tests {
|
||||
use super::{update_content, MARKER};
|
||||
use crate::{day, template::timings::Timing, template::timings::Timings};
|
||||
|
||||
fn get_mock_timings() -> Timings {
|
||||
Timings {
|
||||
data: vec![
|
||||
Timing {
|
||||
day: day!(1),
|
||||
part_1: Some("10ms".into()),
|
||||
part_2: Some("20ms".into()),
|
||||
total_nanos: 3e+10,
|
||||
},
|
||||
Timing {
|
||||
day: day!(2),
|
||||
part_1: Some("30ms".into()),
|
||||
part_2: Some("40ms".into()),
|
||||
total_nanos: 7e+10,
|
||||
},
|
||||
Timing {
|
||||
day: day!(4),
|
||||
part_1: Some("40ms".into()),
|
||||
part_2: Some("50ms".into()),
|
||||
total_nanos: 9e+10,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn errors_if_marker_not_present() {
|
||||
let mut s = "# readme".to_string();
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn errors_if_too_many_markers_present() {
|
||||
let mut s = format!("{} {} {}", MARKER, MARKER, MARKER);
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_empty_benchmarks() {
|
||||
let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER);
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
assert_eq!(s.contains("## Benchmarks"), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_existing_benchmarks() {
|
||||
let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER);
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
assert_eq!(s.matches(MARKER).collect::<Vec<&str>>().len(), 2);
|
||||
assert_eq!(s.matches("## Benchmarks").collect::<Vec<&str>>().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_benchmarks() {
|
||||
let mut s = format!("foo\nbar\n{}\n{}\nbaz", MARKER, MARKER);
|
||||
update_content(&mut s, get_mock_timings(), 190.0).unwrap();
|
||||
let expected = [
|
||||
"foo",
|
||||
"bar",
|
||||
"<!--- benchmarking table --->",
|
||||
"## Benchmarks",
|
||||
"",
|
||||
"| Day | Part 1 | Part 2 |",
|
||||
"| :---: | :---: | :---: |",
|
||||
"| [Day 1](./src/bin/01.rs) | `10ms` | `20ms` |",
|
||||
"| [Day 2](./src/bin/02.rs) | `30ms` | `40ms` |",
|
||||
"| [Day 4](./src/bin/04.rs) | `40ms` | `50ms` |",
|
||||
"",
|
||||
"**Total: 190.00ms**",
|
||||
"<!--- benchmarking table --->",
|
||||
"baz",
|
||||
]
|
||||
.join("\n");
|
||||
assert_eq!(s, expected);
|
||||
}
|
||||
}
|
257
src/template/run_multi.rs
Normal file
257
src/template/run_multi.rs
Normal file
|
@ -0,0 +1,257 @@
|
|||
use std::{collections::HashSet, io};
|
||||
|
||||
use crate::template::{Day, ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
||||
|
||||
use super::{
|
||||
all_days,
|
||||
timings::{Timing, 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());
|
||||
|
||||
let mut need_space = false;
|
||||
|
||||
// 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;
|
||||
|
||||
println!("{ANSI_BOLD}Day {day}{ANSI_RESET}");
|
||||
println!("------");
|
||||
|
||||
let output = child_commands::run_solution(day, is_timed, is_release).unwrap();
|
||||
|
||||
if output.is_empty() {
|
||||
println!("Not solved.");
|
||||
} else {
|
||||
let val = child_commands::parse_exec_time(&output, day);
|
||||
timings.push(val);
|
||||
}
|
||||
});
|
||||
|
||||
if is_timed {
|
||||
let timings = Timings { data: timings };
|
||||
let total_millis = timings.total_millis();
|
||||
println!(
|
||||
"\n{ANSI_BOLD}Total (Run):{ANSI_RESET} {ANSI_ITALIC}{total_millis:.2}ms{ANSI_RESET}"
|
||||
);
|
||||
Some(timings)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
BrokenPipe,
|
||||
IO(io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Error::IO(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_path_for_bin(day: Day) -> String {
|
||||
format!("./src/bin/{day}.rs")
|
||||
}
|
||||
|
||||
/// All solutions live in isolated binaries.
|
||||
/// This module encapsulates interaction with these binaries, both invoking them as well as parsing the timing output.
|
||||
pub mod child_commands {
|
||||
use super::{get_path_for_bin, Error};
|
||||
use crate::template::Day;
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
path::Path,
|
||||
process::{Command, Stdio},
|
||||
thread,
|
||||
};
|
||||
|
||||
/// Run the solution bin for a given day
|
||||
pub fn run_solution(day: Day, is_timed: bool, is_release: bool) -> Result<Vec<String>, Error> {
|
||||
// skip command invocation for days that have not been scaffolded yet.
|
||||
if !Path::new(&get_path_for_bin(day)).exists() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let day_padded = day.to_string();
|
||||
let mut args = vec!["run", "--quiet", "--bin", &day_padded];
|
||||
|
||||
if is_release {
|
||||
args.push("--release");
|
||||
}
|
||||
|
||||
if is_timed {
|
||||
// mirror `--time` flag to child invocations.
|
||||
args.push("--");
|
||||
args.push("--time");
|
||||
}
|
||||
|
||||
// spawn child command with piped stdout/stderr.
|
||||
// forward output to stdout/stderr while grabbing stdout lines.
|
||||
|
||||
let mut cmd = Command::new("cargo")
|
||||
.args(&args)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let stdout = BufReader::new(cmd.stdout.take().ok_or(super::Error::BrokenPipe)?);
|
||||
let stderr = BufReader::new(cmd.stderr.take().ok_or(super::Error::BrokenPipe)?);
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
let thread = thread::spawn(move || {
|
||||
stderr.lines().for_each(|line| {
|
||||
eprintln!("{}", line.unwrap());
|
||||
});
|
||||
});
|
||||
|
||||
for line in stdout.lines() {
|
||||
let line = line.unwrap();
|
||||
println!("{line}");
|
||||
output.push(line);
|
||||
}
|
||||
|
||||
thread.join().unwrap();
|
||||
cmd.wait()?;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn parse_exec_time(output: &[String], day: Day) -> super::Timing {
|
||||
let mut timings = super::Timing {
|
||||
day,
|
||||
part_1: None,
|
||||
part_2: None,
|
||||
total_nanos: 0_f64,
|
||||
};
|
||||
|
||||
output
|
||||
.iter()
|
||||
.filter_map(|l| {
|
||||
if !l.contains(" samples)") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let Some((timing_str, nanos)) = parse_time(l) else {
|
||||
eprintln!("Could not parse timings from line: {l}");
|
||||
return None;
|
||||
};
|
||||
|
||||
let part = l.split(':').next()?;
|
||||
Some((part, timing_str, nanos))
|
||||
})
|
||||
.for_each(|(part, timing_str, nanos)| {
|
||||
if part.contains("Part 1") {
|
||||
timings.part_1 = Some(timing_str.into());
|
||||
} else if part.contains("Part 2") {
|
||||
timings.part_2 = Some(timing_str.into());
|
||||
}
|
||||
|
||||
timings.total_nanos += nanos;
|
||||
});
|
||||
|
||||
timings
|
||||
}
|
||||
|
||||
fn parse_to_float(s: &str, postfix: &str) -> Option<f64> {
|
||||
s.split(postfix).next()?.parse().ok()
|
||||
}
|
||||
|
||||
fn parse_time(line: &str) -> Option<(&str, f64)> {
|
||||
// for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200
|
||||
let str_timing = line
|
||||
.split(" samples)")
|
||||
.next()?
|
||||
.split('(')
|
||||
.last()?
|
||||
.split('@')
|
||||
.next()?
|
||||
.trim();
|
||||
|
||||
let parsed_timing = match str_timing {
|
||||
s if s.contains("ns") => s.split("ns").next()?.parse::<f64>().ok(),
|
||||
s if s.contains("µs") => parse_to_float(s, "µs").map(|x| x * 1000_f64),
|
||||
s if s.contains("ms") => parse_to_float(s, "ms").map(|x| x * 1_000_000_f64),
|
||||
s => parse_to_float(s, "s").map(|x| x * 1_000_000_000_f64),
|
||||
}?;
|
||||
|
||||
Some((str_timing, parsed_timing))
|
||||
}
|
||||
|
||||
/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333
|
||||
#[cfg(feature = "test_lib")]
|
||||
macro_rules! assert_approx_eq {
|
||||
($a:expr, $b:expr) => {{
|
||||
let (a, b) = (&$a, &$b);
|
||||
assert!(
|
||||
(*a - *b).abs() < 1.0e-6,
|
||||
"{} is not approximately equal to {}",
|
||||
*a,
|
||||
*b
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(feature = "test_lib")]
|
||||
mod tests {
|
||||
use super::parse_exec_time;
|
||||
|
||||
use crate::day;
|
||||
|
||||
#[test]
|
||||
fn parses_execution_times() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: 0 (74.13ns @ 100000 samples)".into(),
|
||||
"Part 2: 10 (74.13ms @ 99999 samples)".into(),
|
||||
"".into(),
|
||||
],
|
||||
day!(1),
|
||||
);
|
||||
assert_approx_eq!(res.total_nanos, 74130074.13_f64);
|
||||
assert_eq!(res.part_1.unwrap(), "74.13ns");
|
||||
assert_eq!(res.part_2.unwrap(), "74.13ms");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_with_patterns_in_input() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: @ @ @ ( ) ms (2s @ 5 samples)".into(),
|
||||
"Part 2: 10s (100ms @ 1 samples)".into(),
|
||||
"".into(),
|
||||
],
|
||||
day!(1),
|
||||
);
|
||||
assert_approx_eq!(res.total_nanos, 2100000000_f64);
|
||||
assert_eq!(res.part_1.unwrap(), "2s");
|
||||
assert_eq!(res.part_2.unwrap(), "100ms");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_missing_parts() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: ✖ ".into(),
|
||||
"Part 2: ✖ ".into(),
|
||||
"".into(),
|
||||
],
|
||||
day!(1),
|
||||
);
|
||||
assert_approx_eq!(res.total_nanos, 0_f64);
|
||||
assert_eq!(res.part_1.is_none(), true);
|
||||
assert_eq!(res.part_2.is_none(), true);
|
||||
}
|
||||
}
|
||||
}
|
165
src/template/runner.rs
Normal file
165
src/template/runner.rs
Normal file
|
@ -0,0 +1,165 @@
|
|||
/// Encapsulates code that interacts with solution functions.
|
||||
use std::fmt::Display;
|
||||
use std::hint::black_box;
|
||||
use std::io::{stdout, Write};
|
||||
use std::process::Output;
|
||||
use std::time::{Duration, Instant};
|
||||
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: 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) =
|
||||
run_timed(func, input, |result| print_result(result, &part_str, ""));
|
||||
|
||||
print_result(&result, &part_str, &format_duration(&duration, samples));
|
||||
|
||||
if let Some(result) = result {
|
||||
submit_result(result, day, part);
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a solution part. The behavior differs depending on whether we are running a release or debug build:
|
||||
/// 1. in debug, the function is executed once.
|
||||
/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.)
|
||||
fn run_timed<I: Copy, T>(
|
||||
func: impl Fn(I) -> T,
|
||||
input: I,
|
||||
hook: impl Fn(&T),
|
||||
) -> (T, Duration, u128) {
|
||||
let timer = Instant::now();
|
||||
let result = {
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
let _profiler = dhat::Profiler::new_heap();
|
||||
|
||||
func(input)
|
||||
};
|
||||
let base_time = timer.elapsed();
|
||||
|
||||
hook(&result);
|
||||
|
||||
let run = if std::env::args().any(|x| x == "--time") {
|
||||
bench(func, input, &base_time)
|
||||
} else {
|
||||
(base_time, 1)
|
||||
};
|
||||
|
||||
(result, run.0, run.1)
|
||||
}
|
||||
|
||||
fn bench<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 =
|
||||
(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 {
|
||||
let timer = Instant::now();
|
||||
black_box(func(black_box(input)));
|
||||
timers.push(timer.elapsed());
|
||||
}
|
||||
|
||||
(
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
Duration::from_nanos(average_duration(&timers) as u64),
|
||||
bench_iterations,
|
||||
)
|
||||
}
|
||||
|
||||
fn average_duration(numbers: &[Duration]) -> u128 {
|
||||
numbers
|
||||
.iter()
|
||||
.map(std::time::Duration::as_nanos)
|
||||
.sum::<u128>()
|
||||
/ numbers.len() as u128
|
||||
}
|
||||
|
||||
fn format_duration(duration: &Duration, samples: u128) -> String {
|
||||
if samples == 1 {
|
||||
format!(" ({duration:.1?})")
|
||||
} else {
|
||||
format!(" ({duration:.1?} @ {samples} samples)")
|
||||
}
|
||||
}
|
||||
|
||||
fn print_result<T: Display>(result: &Option<T>, part: &str, duration_str: &str) {
|
||||
let is_intermediate_result = duration_str.is_empty();
|
||||
|
||||
match result {
|
||||
Some(result) => {
|
||||
if result.to_string().contains('\n') {
|
||||
let str = format!("{part}: ▼ {duration_str}");
|
||||
if is_intermediate_result {
|
||||
print!("{str}");
|
||||
} else {
|
||||
print!("\r");
|
||||
println!("{str}");
|
||||
println!("{result}");
|
||||
}
|
||||
} else {
|
||||
let str = format!("{part}: {ANSI_BOLD}{result}{ANSI_RESET}{duration_str}");
|
||||
if is_intermediate_result {
|
||||
print!("{str}");
|
||||
} else {
|
||||
print!("\r");
|
||||
println!("{str}");
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if is_intermediate_result {
|
||||
print!("{part}: ✖");
|
||||
} else {
|
||||
print!("\r");
|
||||
println!("{part}: ✖ ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the arguments passed to `solve` and try to submit one part of the solution if:
|
||||
/// 1. we are in `--release` mode.
|
||||
/// 2. aoc-cli is installed.
|
||||
fn submit_result<T: Display>(
|
||||
result: T,
|
||||
day: Day,
|
||||
part: u8,
|
||||
) -> Option<Result<Output, aoc_cli::AocCommandError>> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if !args.contains(&"--submit".into()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
if args.len() < 3 {
|
||||
eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1;
|
||||
|
||||
let Ok(part_submit) = args[part_index].parse::<u8>() else {
|
||||
eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1");
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
if part_submit != part {
|
||||
return None;
|
||||
}
|
||||
|
||||
if aoc_cli::check().is_err() {
|
||||
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
println!("Submitting result via aoc-cli...");
|
||||
Some(aoc_cli::submit(day, part, &result.to_string()))
|
||||
}
|
384
src/template/timings.rs
Normal file
384
src/template/timings.rs
Normal file
|
@ -0,0 +1,384 @@
|
|||
use std::{collections::HashMap, fs, io::Error, str::FromStr};
|
||||
use tinyjson::JsonValue;
|
||||
|
||||
use crate::template::Day;
|
||||
|
||||
static TIMINGS_FILE_PATH: &str = "./data/timings.json";
|
||||
|
||||
/// Represents benchmark times for a single day.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Timing {
|
||||
pub day: Day,
|
||||
pub part_1: Option<String>,
|
||||
pub part_2: Option<String>,
|
||||
pub total_nanos: f64,
|
||||
}
|
||||
|
||||
/// Represents benchmark times for a set of days.
|
||||
/// Can be serialized from / to JSON.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Timings {
|
||||
pub data: Vec<Timing>,
|
||||
}
|
||||
|
||||
impl Timings {
|
||||
/// Dehydrate timings to a JSON file.
|
||||
pub fn store_file(&self) -> Result<(), Error> {
|
||||
let json = JsonValue::from(self.clone());
|
||||
let mut file = fs::File::create(TIMINGS_FILE_PATH)?;
|
||||
json.format_to(&mut file)
|
||||
}
|
||||
|
||||
/// Rehydrate timings from a JSON file. If not present, returns empty timings.
|
||||
pub fn read_from_file() -> Self {
|
||||
fs::read_to_string(TIMINGS_FILE_PATH)
|
||||
.map_err(|x| x.to_string())
|
||||
.and_then(Timings::try_from)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Merge two sets of timings, overwriting `self` with `other` if present.
|
||||
pub fn merge(&self, new: &Self) -> Self {
|
||||
let mut data: Vec<Timing> = vec![];
|
||||
|
||||
for timing in &new.data {
|
||||
data.push(timing.clone());
|
||||
}
|
||||
|
||||
for timing in &self.data {
|
||||
if !data.iter().any(|t| t.day == timing.day) {
|
||||
data.push(timing.clone());
|
||||
}
|
||||
}
|
||||
|
||||
data.sort_unstable_by(|a, b| a.day.cmp(&b.day));
|
||||
Timings { data }
|
||||
}
|
||||
|
||||
/// Sum up total duration of timings as millis.
|
||||
pub fn total_millis(&self) -> f64 {
|
||||
self.data.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
impl From<Timings> for JsonValue {
|
||||
fn from(value: Timings) -> Self {
|
||||
let mut map: HashMap<String, JsonValue> = HashMap::new();
|
||||
|
||||
map.insert(
|
||||
"data".into(),
|
||||
JsonValue::Array(value.data.iter().map(JsonValue::from).collect()),
|
||||
);
|
||||
|
||||
JsonValue::Object(map)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for Timings {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
let json = JsonValue::from_str(&value).or(Err("not valid JSON file."))?;
|
||||
|
||||
let json_data = json
|
||||
.get::<HashMap<String, JsonValue>>()
|
||||
.ok_or("expected JSON document to be an object.")?
|
||||
.get("data")
|
||||
.ok_or("expected JSON document to have key `data`.")?
|
||||
.get::<Vec<JsonValue>>()
|
||||
.ok_or("expected `json.data` to be an array.")?;
|
||||
|
||||
Ok(Timings {
|
||||
data: json_data
|
||||
.iter()
|
||||
.map(Timing::try_from)
|
||||
.collect::<Result<_, _>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
impl From<&Timing> for JsonValue {
|
||||
fn from(value: &Timing) -> Self {
|
||||
let mut map: HashMap<String, JsonValue> = HashMap::new();
|
||||
|
||||
map.insert("day".into(), JsonValue::String(value.day.to_string()));
|
||||
map.insert("total_nanos".into(), JsonValue::Number(value.total_nanos));
|
||||
|
||||
let part_1 = value.part_1.clone().map(JsonValue::String);
|
||||
let part_2 = value.part_2.clone().map(JsonValue::String);
|
||||
|
||||
map.insert(
|
||||
"part_1".into(),
|
||||
match part_1 {
|
||||
Some(x) => x,
|
||||
None => JsonValue::Null,
|
||||
},
|
||||
);
|
||||
|
||||
map.insert(
|
||||
"part_2".into(),
|
||||
match part_2 {
|
||||
Some(x) => x,
|
||||
None => JsonValue::Null,
|
||||
},
|
||||
);
|
||||
|
||||
JsonValue::Object(map)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&JsonValue> for Timing {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: &JsonValue) -> Result<Self, Self::Error> {
|
||||
let json = value
|
||||
.get::<HashMap<String, JsonValue>>()
|
||||
.ok_or("Expected timing to be a JSON object.")?;
|
||||
|
||||
let day = json
|
||||
.get("day")
|
||||
.and_then(|v| v.get::<String>())
|
||||
.and_then(|day| Day::from_str(day).ok())
|
||||
.ok_or("Expected timing.day to be a Day struct.")?;
|
||||
|
||||
let part_1 = json
|
||||
.get("part_1")
|
||||
.map(|v| if v.is_null() { None } else { v.get::<String>() })
|
||||
.ok_or("Expected timing.part_1 to be null or string.")?;
|
||||
|
||||
let part_2 = json
|
||||
.get("part_2")
|
||||
.map(|v| if v.is_null() { None } else { v.get::<String>() })
|
||||
.ok_or("Expected timing.part_2 to be null or string.")?;
|
||||
|
||||
let total_nanos = json
|
||||
.get("total_nanos")
|
||||
.and_then(|v| v.get::<f64>().copied())
|
||||
.ok_or("Expected timing.total_nanos to be a number.")?;
|
||||
|
||||
Ok(Timing {
|
||||
day,
|
||||
part_1: part_1.cloned(),
|
||||
part_2: part_2.cloned(),
|
||||
total_nanos,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
#[cfg(feature = "test_lib")]
|
||||
mod tests {
|
||||
use crate::day;
|
||||
|
||||
use super::{Timing, Timings};
|
||||
|
||||
fn get_mock_timings() -> Timings {
|
||||
Timings {
|
||||
data: vec![
|
||||
Timing {
|
||||
day: day!(1),
|
||||
part_1: Some("10ms".into()),
|
||||
part_2: Some("20ms".into()),
|
||||
total_nanos: 3e+10,
|
||||
},
|
||||
Timing {
|
||||
day: day!(2),
|
||||
part_1: Some("30ms".into()),
|
||||
part_2: Some("40ms".into()),
|
||||
total_nanos: 7e+10,
|
||||
},
|
||||
Timing {
|
||||
day: day!(4),
|
||||
part_1: Some("40ms".into()),
|
||||
part_2: None,
|
||||
total_nanos: 4e+10,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
mod deserialization {
|
||||
use crate::{day, template::timings::Timings};
|
||||
|
||||
#[test]
|
||||
fn handles_json_timings() {
|
||||
let json = r#"{ "data": [{ "day": "01", "part_1": "1ms", "part_2": null, "total_nanos": 1000000000 }] }"#.to_string();
|
||||
let timings = Timings::try_from(json).unwrap();
|
||||
assert_eq!(timings.data.len(), 1);
|
||||
let timing = timings.data.first().unwrap();
|
||||
assert_eq!(timing.day, day!(1));
|
||||
assert_eq!(timing.part_1, Some("1ms".to_string()));
|
||||
assert_eq!(timing.part_2, None);
|
||||
assert_eq!(timing.total_nanos, 1_000_000_000_f64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_empty_timings() {
|
||||
let json = r#"{ "data": [] }"#.to_string();
|
||||
let timings = Timings::try_from(json).unwrap();
|
||||
assert_eq!(timings.data.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn panics_for_invalid_json() {
|
||||
let json = r#"{}"#.to_string();
|
||||
Timings::try_from(json).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn panics_for_malformed_timings() {
|
||||
let json = r#"{ "data": [{ "day": "01" }, { "day": "26" }, { "day": "02", "part_2": null, "total_nanos": 0 }] }"#.to_string();
|
||||
Timings::try_from(json).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
mod serialization {
|
||||
use super::get_mock_timings;
|
||||
use std::collections::HashMap;
|
||||
use tinyjson::JsonValue;
|
||||
|
||||
#[test]
|
||||
fn serializes_timings() {
|
||||
let timings = get_mock_timings();
|
||||
let value = JsonValue::try_from(timings).unwrap();
|
||||
assert_eq!(
|
||||
value
|
||||
.get::<HashMap<String, JsonValue>>()
|
||||
.unwrap()
|
||||
.get("data")
|
||||
.unwrap()
|
||||
.get::<Vec<JsonValue>>()
|
||||
.unwrap()
|
||||
.len(),
|
||||
3
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
mod is_day_complete {
|
||||
use crate::{
|
||||
day,
|
||||
template::timings::{Timing, Timings},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn handles_completed_days() {
|
||||
let timings = Timings {
|
||||
data: vec![Timing {
|
||||
day: day!(1),
|
||||
part_1: Some("1ms".into()),
|
||||
part_2: Some("2ms".into()),
|
||||
total_nanos: 3_000_000_000_f64,
|
||||
}],
|
||||
};
|
||||
|
||||
assert_eq!(timings.is_day_complete(&day!(1)), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_partial_days() {
|
||||
let timings = Timings {
|
||||
data: vec![Timing {
|
||||
day: day!(1),
|
||||
part_1: Some("1ms".into()),
|
||||
part_2: None,
|
||||
total_nanos: 1_000_000_000_f64,
|
||||
}],
|
||||
};
|
||||
|
||||
assert_eq!(timings.is_day_complete(&day!(1)), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_uncompleted_days() {
|
||||
let timings = Timings {
|
||||
data: vec![Timing {
|
||||
day: day!(1),
|
||||
part_1: None,
|
||||
part_2: None,
|
||||
total_nanos: 0.0,
|
||||
}],
|
||||
};
|
||||
|
||||
assert_eq!(timings.is_day_complete(&day!(1)), false);
|
||||
}
|
||||
}
|
||||
|
||||
mod merge {
|
||||
use crate::{
|
||||
day,
|
||||
template::timings::{Timing, Timings},
|
||||
};
|
||||
|
||||
use super::get_mock_timings;
|
||||
|
||||
#[test]
|
||||
fn handles_disjunct_timings() {
|
||||
let timings = get_mock_timings();
|
||||
let other = Timings {
|
||||
data: vec![Timing {
|
||||
day: day!(3),
|
||||
part_1: None,
|
||||
part_2: None,
|
||||
total_nanos: 0_f64,
|
||||
}],
|
||||
};
|
||||
let merged = timings.merge(&other);
|
||||
assert_eq!(merged.data.len(), 4);
|
||||
assert_eq!(merged.data[0].day, day!(1));
|
||||
assert_eq!(merged.data[1].day, day!(2));
|
||||
assert_eq!(merged.data[2].day, day!(3));
|
||||
assert_eq!(merged.data[3].day, day!(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_overlapping_timings() {
|
||||
let timings = get_mock_timings();
|
||||
|
||||
let other = Timings {
|
||||
data: vec![Timing {
|
||||
day: day!(2),
|
||||
part_1: None,
|
||||
part_2: None,
|
||||
total_nanos: 0_f64,
|
||||
}],
|
||||
};
|
||||
let merged = timings.merge(&other);
|
||||
|
||||
assert_eq!(merged.data.len(), 3);
|
||||
assert_eq!(merged.data[0].day, day!(1));
|
||||
assert_eq!(merged.data[1].day, day!(2));
|
||||
assert_eq!(merged.data[1].total_nanos, 0_f64);
|
||||
assert_eq!(merged.data[2].day, day!(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_empty_timings() {
|
||||
let timings = Timings::default();
|
||||
let other = get_mock_timings();
|
||||
let merged = timings.merge(&other);
|
||||
assert_eq!(merged.data.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_empty_other_timings() {
|
||||
let timings = get_mock_timings();
|
||||
let other = Timings::default();
|
||||
let merged = timings.merge(&other);
|
||||
assert_eq!(merged.data.len(), 3);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue