Compare commits
49 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 |
34 changed files with 2075 additions and 479 deletions
|
@ -1,11 +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 -- all --release --time"
|
||||
time = "run --quiet --release -- time"
|
||||
|
||||
[env]
|
||||
AOC_YEAR = "2022"
|
||||
AOC_YEAR = "2024"
|
||||
|
|
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -6,13 +6,13 @@ env:
|
|||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
test:
|
||||
ci:
|
||||
runs-on: ubuntu-latest
|
||||
name: CI
|
||||
name: Continuous Integration
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up cargo cache
|
||||
uses: actions/cache@v3
|
||||
uses: actions/cache@v4
|
||||
continue-on-error: false
|
||||
with:
|
||||
path: |
|
||||
|
|
18
.github/workflows/readme-stars.yml
vendored
18
.github/workflows/readme-stars.yml
vendored
|
@ -9,22 +9,16 @@ on:
|
|||
jobs:
|
||||
update-readme:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ vars.AOC_ENABLED == 'true' }}
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
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"
|
||||
|
|
12
.gitignore
vendored
12
.gitignore
vendored
|
@ -17,8 +17,14 @@ target/
|
|||
# Advent of Code
|
||||
# @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3
|
||||
|
||||
data
|
||||
|
||||
data/inputs/*
|
||||
!data/inputs/.keep
|
||||
!data/examples/.keep
|
||||
data/puzzles/*
|
||||
!data/puzzles/.keep
|
||||
|
||||
# Dhat
|
||||
dhat-heap.json
|
||||
|
||||
# Benchmarks
|
||||
|
||||
data/timings.json
|
||||
|
|
4
.vscode/extensions.json
vendored
4
.vscode/extensions.json
vendored
|
@ -2,6 +2,6 @@
|
|||
"recommendations": [
|
||||
"vadimcn.vscode-lldb",
|
||||
"rust-lang.rust-analyzer",
|
||||
"serayuzgur.crates"
|
||||
"editorConfig.editorConfig"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
40
.vscode/launch.json
vendored
40
.vscode/launch.json
vendored
|
@ -7,13 +7,17 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'advent_of_code'",
|
||||
"name": "Debug unit tests for a solution",
|
||||
"cargo": {
|
||||
"args": ["test", "--no-run", "--bin=advent_of_code", "--package=advent_of_code"],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "bin"
|
||||
}
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
// replace with binary name (e.g. "01") here if you always
|
||||
// want to debug one file regardless of the active file in
|
||||
// the editor.
|
||||
"--bin=${fileBasenameNoExtension}",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
|
@ -21,15 +25,17 @@
|
|||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'advent_of_code'",
|
||||
"name": "Debug a solution",
|
||||
"cargo": {
|
||||
"args": ["build", "--bin=advent_of_code", "--package=advent_of_code"],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "bin"
|
||||
}
|
||||
"args": [
|
||||
"build",
|
||||
// replace with binary name (e.g. "01") here if you always
|
||||
// want to debug one file regardless of the active file in
|
||||
// the editor
|
||||
"--bin=${fileBasenameNoExtension}",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
},
|
||||
"args": ["1"],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
|
@ -37,7 +43,13 @@
|
|||
"request": "launch",
|
||||
"name": "Debug unit tests in library 'advent_of_code'",
|
||||
"cargo": {
|
||||
"args": ["test", "--no-run", "--lib", "--package=advent_of_code"],
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--lib",
|
||||
"--features=test_lib",
|
||||
"--package=advent_of_code"
|
||||
],
|
||||
"filter": {
|
||||
"name": "advent_of_code",
|
||||
"kind": "lib"
|
||||
|
|
576
Cargo.lock
generated
576
Cargo.lock
generated
|
@ -3,10 +3,271 @@
|
|||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "advent_of_code"
|
||||
version = "0.9.0"
|
||||
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]]
|
||||
|
@ -14,3 +275,314 @@ 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"
|
||||
|
|
17
Cargo.toml
17
Cargo.toml
|
@ -1,17 +1,30 @@
|
|||
[package]
|
||||
name = "advent_of_code"
|
||||
version = "0.9.0"
|
||||
version = "0.11.0"
|
||||
authors = ["Felix Spöttel <1682504+fspoettel@users.noreply.github.com>"]
|
||||
edition = "2021"
|
||||
default-run = "advent_of_code"
|
||||
publish = false
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
# 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
|
||||
|
|
168
README.md
168
README.md
|
@ -6,6 +6,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
|
|||
|
||||
<!--- advent_readme_stars table --->
|
||||
|
||||
<!--- benchmarking table --->
|
||||
## Benchmarks
|
||||
|
||||
| Day | Part 1 | Part 2 |
|
||||
| :---: | :---: | :---: |
|
||||
| [Day 1](./src/bin/01.rs) | `89.5µs` | `125.4µs` |
|
||||
|
||||
**Total: 0.21ms**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
@ -14,14 +22,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
|
|||
|
||||
This template supports all major OS (macOS, Linux, Windows).
|
||||
|
||||
### Create your repository 📝
|
||||
### 📝 Create your repository
|
||||
|
||||
1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github.
|
||||
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.
|
||||
|
@ -33,7 +41,7 @@ This template supports all major OS (macOS, Linux, Windows).
|
|||
|
||||
## Usage
|
||||
|
||||
### Scaffold a day
|
||||
### ➡️ Scaffold a day
|
||||
|
||||
```sh
|
||||
# example: `cargo scaffold 1`
|
||||
|
@ -49,14 +57,17 @@ cargo scaffold <day>
|
|||
|
||||
Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory.
|
||||
|
||||
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solutions against the example input.
|
||||
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template.txt) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input. In VS Code, `rust-analyzer` will display buttons for running / debugging these unit tests above the unit test blocks.
|
||||
|
||||
Tip: when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks.
|
||||
> [!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.
|
||||
|
||||
### Download input & description for a day
|
||||
### ➡️ Download input for a day
|
||||
|
||||
> **Note**
|
||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
> [!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: `cargo download 1`
|
||||
|
@ -71,7 +82,7 @@ cargo download <day>
|
|||
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
|
||||
```
|
||||
|
||||
### Run solutions for a day
|
||||
### ➡️ Run solutions for a day
|
||||
|
||||
```sh
|
||||
# example: `cargo solve 01`
|
||||
|
@ -86,18 +97,14 @@ cargo solve <day>
|
|||
|
||||
The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program.
|
||||
|
||||
By default, `solve` executes your code once and shows the execution time. If you append the `--time` flag to the command, the runner will run your code between `10` and `10.000` times (depending on execution time of first execution) and print the average execution time.
|
||||
|
||||
For example, running a benchmarked, optimized execution of day 1 would look like `cargo solve 1 --release --time`. Displayed _timings_ show the raw execution time of your solution without overhead like file reads.
|
||||
|
||||
#### Submitting solutions
|
||||
|
||||
> **Note**
|
||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
> [!IMPORTANT]
|
||||
> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
|
||||
In order to submit part of a solution for checking, append the `--submit <part>` option to the `solve` command.
|
||||
Append the `--submit <part>` option to the `solve` command to submit your solution for checking.
|
||||
|
||||
### Run all solutions
|
||||
### ➡️ Run all solutions
|
||||
|
||||
```sh
|
||||
cargo all
|
||||
|
@ -113,15 +120,38 @@ cargo all
|
|||
# Total: 0.20ms
|
||||
```
|
||||
|
||||
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, `--release` controls whether real inputs will be used.
|
||||
This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build.
|
||||
|
||||
#### Update readme benchmarks
|
||||
### ➡️ Benchmark your solutions
|
||||
|
||||
The template can output a table with solution times to your readme. Please note that these are not "scientific" benchmarks, understand them as a fun approximation. 😉
|
||||
```sh
|
||||
# example: `cargo time 8 --store`
|
||||
cargo time <day> [--all] [--store]
|
||||
|
||||
In order to generate a benchmarking table, run `cargo all --release --time`. If everything goes well, the command will output "_Successfully updated README with benchmarks._" after the execution finishes.
|
||||
# output:
|
||||
# Day 08
|
||||
# ------
|
||||
# Part 1: 1 (39.0ns @ 10000 samples)
|
||||
# Part 2: 2 (39.0ns @ 10000 samples)
|
||||
#
|
||||
# Total (Run): 0.00ms
|
||||
#
|
||||
# Stored updated benchmarks.
|
||||
```
|
||||
|
||||
### Run all tests
|
||||
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
|
||||
|
@ -129,21 +159,9 @@ cargo test
|
|||
|
||||
To run tests for a specific day, append `--bin <day>`, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`.
|
||||
|
||||
### Format code
|
||||
### ➡️ Read puzzle description
|
||||
|
||||
```sh
|
||||
cargo fmt
|
||||
```
|
||||
|
||||
### Lint code
|
||||
|
||||
```sh
|
||||
cargo clippy
|
||||
```
|
||||
|
||||
### Read puzzle description in terminal
|
||||
|
||||
> **Note**
|
||||
> [!IMPORTANT]
|
||||
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
|
||||
|
||||
```sh
|
||||
|
@ -156,14 +174,61 @@ cargo read <day>
|
|||
# ...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
|
||||
|
||||
```sh
|
||||
cargo clippy
|
||||
```
|
||||
|
||||
## Optional template features
|
||||
|
||||
### Configure aoc-cli integration
|
||||
|
||||
1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0`
|
||||
2. Create an `.adventofcode.session` file in your home directory and paste your session cookie. To retrieve the session cookie, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1]
|
||||
2. Create the file `<home_directory>/.adventofcode.session` and paste your session cookie into it. To retrieve the session cookie, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in _Cookies_ under the _Application_ or _Storage_ tab, and copy out the `session` cookie value. [^1]
|
||||
|
||||
Once installed, you can use the [download command](#download-input--description-for-a-day) and automatically submit solutions via the [`--submit` flag](#submitting-solutions).
|
||||
Once installed, you can use the [download command](#download-input--description-for-a-day), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions).
|
||||
|
||||
### Automatically track ⭐️ progress in the readme
|
||||
|
||||
|
@ -179,17 +244,40 @@ 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[^2] for the advent of code website. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie.
|
||||
|
||||
Go to the _Variables_ tab in your repository settings and create the following variable:
|
||||
|
||||
- `AOC_ENABLED`: This variable controls whether the workflow is enabled. Set it to `true` to enable the progress tracker. After you complete AoC or no longer work on it, you can set this to `false` to disable the CI.
|
||||
|
||||
✨ You can now run this action manually via the _Run workflow_ button on the workflow page. If you want the workflow to run automatically, uncomment the `schedule` section in the `readme-stars.yml` workflow file or add a `push` trigger.
|
||||
|
||||
### Check code formatting / clippy lints in CI
|
||||
### 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).
|
||||
|
|
0
data/examples/01.txt
Normal file
0
data/examples/01.txt
Normal file
6
data/examples/02.txt
Normal file
6
data/examples/02.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
0
data/examples/03.txt
Normal file
0
data/examples/03.txt
Normal file
0
data/examples/04.txt
Normal file
0
data/examples/04.txt
Normal file
85
src/bin/01.rs
Normal file
85
src/bin/01.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
advent_of_code::solution!(1);
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let total_distance: u32 = left
|
||||
.iter()
|
||||
.zip(right.iter())
|
||||
.map(|(&x, &y)| if x > y { x - y } else { y - x })
|
||||
.sum();
|
||||
|
||||
Some(total_distance)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let mut right_list_counts: HashMap<u32, u32> = HashMap::new();
|
||||
for &num in &right {
|
||||
*right_list_counts.entry(num).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let total_similarity_score: u32 = left
|
||||
.iter()
|
||||
.map(|&num| num * right_list_counts.get(&num).unwrap_or(&0))
|
||||
.sum();
|
||||
|
||||
Some(total_similarity_score)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
42
src/bin/02.rs
Normal file
42
src/bin/02.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
advent_of_code::solution!(2);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
let mut num_safe: u32 = 0;
|
||||
let lines = input.lines();
|
||||
for line in lines {
|
||||
let levels: Vec<u32> = line
|
||||
.split_whitespace()
|
||||
.filter_map(|i| i.parse::<u32>().ok())
|
||||
.collect();
|
||||
|
||||
if levels
|
||||
.windows(2)
|
||||
.all(|w| (w[0] > w[1] && w[0] - w[1] <= 3) || (w[1] > w[0] && w[1] - w[0] <= 3))
|
||||
&& (levels.windows(2).all(|w| w[0] < w[1]) || levels.windows(2).all(|w| w[0] > w[1]))
|
||||
{
|
||||
num_safe += 1;
|
||||
}
|
||||
}
|
||||
Some(num_safe)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, Some(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
26
src/bin/03.rs
Normal file
26
src/bin/03.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
advent_of_code::solution!(3);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
26
src/bin/04.rs
Normal file
26
src/bin/04.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
advent_of_code::solution!(4);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
pub mod template;
|
||||
|
||||
// Use this file to add helper functions and additional modules.
|
||||
|
|
80
src/main.rs
80
src/main.rs
|
@ -1,29 +1,43 @@
|
|||
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
|
||||
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: u8,
|
||||
day: Day,
|
||||
},
|
||||
Read {
|
||||
day: u8,
|
||||
day: Day,
|
||||
},
|
||||
Scaffold {
|
||||
day: u8,
|
||||
day: Day,
|
||||
download: bool,
|
||||
overwrite: bool,
|
||||
},
|
||||
Solve {
|
||||
day: u8,
|
||||
day: Day,
|
||||
release: bool,
|
||||
time: bool,
|
||||
dhat: bool,
|
||||
submit: Option<u8>,
|
||||
},
|
||||
All {
|
||||
release: bool,
|
||||
time: bool,
|
||||
},
|
||||
Time {
|
||||
all: bool,
|
||||
day: Option<Day>,
|
||||
store: bool,
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
Today,
|
||||
}
|
||||
|
||||
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
|
||||
|
@ -32,8 +46,17 @@ mod args {
|
|||
let app_args = match args.subcommand()?.as_deref() {
|
||||
Some("all") => AppArguments::All {
|
||||
release: args.contains("--release"),
|
||||
time: args.contains("--time"),
|
||||
},
|
||||
Some("time") => {
|
||||
let all = args.contains("--all");
|
||||
let store = args.contains("--store");
|
||||
|
||||
AppArguments::Time {
|
||||
all,
|
||||
day: args.opt_free_from_str()?,
|
||||
store,
|
||||
}
|
||||
}
|
||||
Some("download") => AppArguments::Download {
|
||||
day: args.free_from_str()?,
|
||||
},
|
||||
|
@ -42,13 +65,17 @@ mod args {
|
|||
},
|
||||
Some("scaffold") => AppArguments::Scaffold {
|
||||
day: args.free_from_str()?,
|
||||
download: args.contains("--download"),
|
||||
overwrite: args.contains("--overwrite"),
|
||||
},
|
||||
Some("solve") => AppArguments::Solve {
|
||||
day: args.free_from_str()?,
|
||||
release: args.contains("--release"),
|
||||
submit: args.opt_value_from_str("--submit")?,
|
||||
time: args.contains("--time"),
|
||||
dhat: args.contains("--dhat"),
|
||||
},
|
||||
#[cfg(feature = "today")]
|
||||
Some("today") => AppArguments::Today,
|
||||
Some(x) => {
|
||||
eprintln!("Unknown command: {x}");
|
||||
process::exit(1);
|
||||
|
@ -75,16 +102,43 @@ fn main() {
|
|||
std::process::exit(1);
|
||||
}
|
||||
Ok(args) => match args {
|
||||
AppArguments::All { release, time } => all::handle(release, time),
|
||||
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 } => scaffold::handle(day),
|
||||
AppArguments::Scaffold {
|
||||
day,
|
||||
download,
|
||||
overwrite,
|
||||
} => {
|
||||
scaffold::handle(day, overwrite);
|
||||
if download {
|
||||
download::handle(day);
|
||||
}
|
||||
}
|
||||
AppArguments::Solve {
|
||||
day,
|
||||
release,
|
||||
time,
|
||||
dhat,
|
||||
submit,
|
||||
} => solve::handle(day, release, time, 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);
|
||||
}
|
||||
}
|
|
@ -4,12 +4,13 @@ use std::{
|
|||
process::{Command, Output, Stdio},
|
||||
};
|
||||
|
||||
use crate::template::Day;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AocCommandError {
|
||||
CommandNotFound,
|
||||
CommandNotCallable,
|
||||
BadExitStatus(Output),
|
||||
IoError,
|
||||
}
|
||||
|
||||
impl Display for AocCommandError {
|
||||
|
@ -20,7 +21,6 @@ impl Display for AocCommandError {
|
|||
AocCommandError::BadExitStatus(_) => {
|
||||
write!(f, "aoc-cli exited with a non-zero status.")
|
||||
}
|
||||
AocCommandError::IoError => write!(f, "could not write output files to file system."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ pub fn check() -> Result<(), AocCommandError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read(day: u8) -> Result<Output, AocCommandError> {
|
||||
pub fn read(day: Day) -> Result<Output, AocCommandError> {
|
||||
let puzzle_path = get_puzzle_path(day);
|
||||
|
||||
let args = build_args(
|
||||
|
@ -49,7 +49,7 @@ pub fn read(day: u8) -> Result<Output, AocCommandError> {
|
|||
call_aoc_cli(&args)
|
||||
}
|
||||
|
||||
pub fn download(day: u8) -> Result<Output, AocCommandError> {
|
||||
pub fn download(day: Day) -> Result<Output, AocCommandError> {
|
||||
let input_path = get_input_path(day);
|
||||
let puzzle_path = get_puzzle_path(day);
|
||||
|
||||
|
@ -72,7 +72,7 @@ pub fn download(day: u8) -> Result<Output, AocCommandError> {
|
|||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn submit(day: u8, part: u8, result: &str) -> Result<Output, AocCommandError> {
|
||||
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());
|
||||
|
@ -80,14 +80,12 @@ pub fn submit(day: u8, part: u8, result: &str) -> Result<Output, AocCommandError
|
|||
call_aoc_cli(&args)
|
||||
}
|
||||
|
||||
fn get_input_path(day: u8) -> String {
|
||||
let day_padded = format!("{day:02}");
|
||||
format!("data/inputs/{day_padded}.txt")
|
||||
fn get_input_path(day: Day) -> String {
|
||||
format!("data/inputs/{day}.txt")
|
||||
}
|
||||
|
||||
fn get_puzzle_path(day: u8) -> String {
|
||||
let day_padded = format!("{day:02}");
|
||||
format!("data/puzzles/{day_padded}.md")
|
||||
fn get_puzzle_path(day: Day) -> String {
|
||||
format!("data/puzzles/{day}.md")
|
||||
}
|
||||
|
||||
fn get_year() -> Option<u16> {
|
||||
|
@ -97,7 +95,7 @@ fn get_year() -> Option<u16> {
|
|||
}
|
||||
}
|
||||
|
||||
fn build_args(command: &str, args: &[String], day: u8) -> Vec<String> {
|
||||
fn build_args(command: &str, args: &[String], day: Day) -> Vec<String> {
|
||||
let mut cmd_args = args.to_vec();
|
||||
|
||||
if let Some(year) = get_year() {
|
||||
|
|
|
@ -1,256 +1,5 @@
|
|||
use std::io;
|
||||
use crate::template::{all_days, run_multi::run_multi};
|
||||
|
||||
use crate::template::{
|
||||
readme_benchmarks::{self, Timings},
|
||||
ANSI_BOLD, ANSI_ITALIC, ANSI_RESET,
|
||||
};
|
||||
|
||||
pub fn handle(is_release: bool, is_timed: bool) {
|
||||
let mut timings: Vec<Timings> = vec![];
|
||||
|
||||
(1..=25).for_each(|day| {
|
||||
if day > 1 {
|
||||
println!();
|
||||
}
|
||||
|
||||
println!("{ANSI_BOLD}Day {day}{ANSI_RESET}");
|
||||
println!("------");
|
||||
|
||||
let output = child_commands::run_solution(day, is_timed, is_release).unwrap();
|
||||
|
||||
if output.is_empty() {
|
||||
println!("Not solved.");
|
||||
} else {
|
||||
let val = child_commands::parse_exec_time(&output, day);
|
||||
timings.push(val);
|
||||
}
|
||||
});
|
||||
|
||||
if is_timed {
|
||||
let total_millis = timings.iter().map(|x| x.total_nanos).sum::<f64>() / 1_000_000_f64;
|
||||
|
||||
println!("\n{ANSI_BOLD}Total:{ANSI_RESET} {ANSI_ITALIC}{total_millis:.2}ms{ANSI_RESET}");
|
||||
|
||||
if is_release {
|
||||
match readme_benchmarks::update(timings, total_millis) {
|
||||
Ok(()) => println!("Successfully updated README with benchmarks."),
|
||||
Err(_) => {
|
||||
eprintln!("Failed to update readme with benchmarks.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
BrokenPipe,
|
||||
Parser(String),
|
||||
IO(io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Error::IO(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_path_for_bin(day: usize) -> String {
|
||||
let day_padded = format!("{day:02}");
|
||||
format!("./src/bin/{day_padded}.rs")
|
||||
}
|
||||
|
||||
/// All solutions live in isolated binaries.
|
||||
/// This module encapsulates interaction with these binaries, both invoking them as well as parsing the timing output.
|
||||
mod child_commands {
|
||||
use super::{get_path_for_bin, Error};
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
path::Path,
|
||||
process::{Command, Stdio},
|
||||
thread,
|
||||
};
|
||||
|
||||
/// Run the solution bin for a given day
|
||||
pub fn run_solution(
|
||||
day: usize,
|
||||
is_timed: bool,
|
||||
is_release: bool,
|
||||
) -> Result<Vec<String>, Error> {
|
||||
let day_padded = format!("{day:02}");
|
||||
|
||||
// skip command invocation for days that have not been scaffolded yet.
|
||||
if !Path::new(&get_path_for_bin(day)).exists() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
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: usize) -> super::Timings {
|
||||
let mut timings = super::Timings {
|
||||
day,
|
||||
part_1: None,
|
||||
part_2: None,
|
||||
total_nanos: 0_f64,
|
||||
};
|
||||
|
||||
output
|
||||
.iter()
|
||||
.filter_map(|l| {
|
||||
if !l.contains(" samples)") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let Some((timing_str, nanos)) = parse_time(l) else {
|
||||
eprintln!("Could not parse timings from line: {l}");
|
||||
return None;
|
||||
};
|
||||
|
||||
let part = l.split(':').next()?;
|
||||
Some((part, timing_str, nanos))
|
||||
})
|
||||
.for_each(|(part, timing_str, nanos)| {
|
||||
if part.contains("Part 1") {
|
||||
timings.part_1 = Some(timing_str.into());
|
||||
} else if part.contains("Part 2") {
|
||||
timings.part_2 = Some(timing_str.into());
|
||||
}
|
||||
|
||||
timings.total_nanos += nanos;
|
||||
});
|
||||
|
||||
timings
|
||||
}
|
||||
|
||||
fn parse_to_float(s: &str, postfix: &str) -> Option<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;
|
||||
|
||||
#[test]
|
||||
fn test_well_formed() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: 0 (74.13ns @ 100000 samples)".into(),
|
||||
"Part 2: 10 (74.13ms @ 99999 samples)".into(),
|
||||
"".into(),
|
||||
],
|
||||
1,
|
||||
);
|
||||
assert_approx_eq!(res.total_nanos, 74130074.13_f64);
|
||||
assert_eq!(res.part_1.unwrap(), "74.13ns");
|
||||
assert_eq!(res.part_2.unwrap(), "74.13ms");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_patterns_in_input() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: @ @ @ ( ) ms (2s @ 5 samples)".into(),
|
||||
"Part 2: 10s (100ms @ 1 samples)".into(),
|
||||
"".into(),
|
||||
],
|
||||
1,
|
||||
);
|
||||
assert_approx_eq!(res.total_nanos, 2100000000_f64);
|
||||
assert_eq!(res.part_1.unwrap(), "2s");
|
||||
assert_eq!(res.part_2.unwrap(), "100ms");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_parts() {
|
||||
let res = parse_exec_time(
|
||||
&[
|
||||
"Part 1: ✖ ".into(),
|
||||
"Part 2: ✖ ".into(),
|
||||
"".into(),
|
||||
],
|
||||
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);
|
||||
}
|
||||
}
|
||||
pub fn handle(is_release: bool) {
|
||||
run_multi(&all_days().collect(), is_release, false);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::template::aoc_cli;
|
||||
use crate::template::{aoc_cli, Day};
|
||||
use std::process;
|
||||
|
||||
pub fn handle(day: u8) {
|
||||
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);
|
||||
|
|
|
@ -3,3 +3,4 @@ pub mod download;
|
|||
pub mod read;
|
||||
pub mod scaffold;
|
||||
pub mod solve;
|
||||
pub mod time;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::process;
|
||||
|
||||
use crate::template::aoc_cli;
|
||||
use crate::template::{aoc_cli, Day};
|
||||
|
||||
pub fn handle(day: u8) {
|
||||
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);
|
||||
|
|
|
@ -4,50 +4,35 @@ use std::{
|
|||
process,
|
||||
};
|
||||
|
||||
const MODULE_TEMPLATE: &str = r#"pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
use crate::template::Day;
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
const MODULE_TEMPLATE: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt"));
|
||||
|
||||
advent_of_code::main!(DAY);
|
||||
|
||||
#[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);
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create_new(true).open(path)
|
||||
file.truncate(true).write(true).open(path)
|
||||
}
|
||||
|
||||
fn create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create(true).open(path)
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(path)
|
||||
}
|
||||
|
||||
pub fn handle(day: u8) {
|
||||
let day_padded = format!("{day:02}");
|
||||
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 input_path = format!("data/inputs/{day_padded}.txt");
|
||||
let example_path = format!("data/examples/{day_padded}.txt");
|
||||
let module_path = format!("src/bin/{day_padded}.rs");
|
||||
|
||||
let mut file = match safe_create_file(&module_path) {
|
||||
let mut file = match safe_create_file(&module_path, overwrite) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create module file: {e}");
|
||||
|
@ -55,7 +40,11 @@ pub fn handle(day: u8) {
|
|||
}
|
||||
};
|
||||
|
||||
match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) {
|
||||
match file.write_all(
|
||||
MODULE_TEMPLATE
|
||||
.replace("%DAY_NUMBER%", &day.into_inner().to_string())
|
||||
.as_bytes(),
|
||||
) {
|
||||
Ok(()) => {
|
||||
println!("Created module file \"{}\"", &module_path);
|
||||
}
|
||||
|
@ -86,8 +75,5 @@ pub fn handle(day: u8) {
|
|||
}
|
||||
|
||||
println!("---");
|
||||
println!(
|
||||
"🎄 Type `cargo solve {}` to run your solution.",
|
||||
&day_padded
|
||||
);
|
||||
println!("🎄 Type `cargo solve {day}` to run your solution.");
|
||||
}
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
use std::process::{Command, Stdio};
|
||||
|
||||
pub fn handle(day: u8, release: bool, time: bool, submit_part: Option<u8>) {
|
||||
let day_padded = format!("{day:02}");
|
||||
use crate::template::Day;
|
||||
|
||||
let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded];
|
||||
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 release {
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -16,10 +23,6 @@ pub fn handle(day: u8, release: bool, time: bool, submit_part: Option<u8>) {
|
|||
cmd_args.push(submit_part.to_string());
|
||||
}
|
||||
|
||||
if time {
|
||||
cmd_args.push("--time".to_string());
|
||||
}
|
||||
|
||||
let mut cmd = Command::new("cargo")
|
||||
.args(&cmd_args)
|
||||
.stdout(Stdio::inherit())
|
||||
|
|
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
|
@ -2,33 +2,67 @@ use std::{env, fs};
|
|||
|
||||
pub mod aoc_cli;
|
||||
pub mod commands;
|
||||
pub mod readme_benchmarks;
|
||||
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: u8) -> 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:02}.txt"));
|
||||
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")
|
||||
}
|
||||
|
||||
/// main! produces a block setting up the input and runner for each part.
|
||||
/// 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! main {
|
||||
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 advent_of_code::template::runner::*;
|
||||
let input = advent_of_code::template::read_file("inputs", $day);
|
||||
run_part(part_one, &input, $day, 1);
|
||||
run_part(part_two, &input, $day, 2);
|
||||
use $crate::template::runner::*;
|
||||
let input = $crate::template::read_file("inputs", DAY);
|
||||
$( run_part($func, &input, DAY, $part); )*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,8 +2,12 @@
|
|||
/// 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),
|
||||
|
@ -16,22 +20,14 @@ impl From<std::io::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Timings {
|
||||
pub day: usize,
|
||||
pub part_1: Option<String>,
|
||||
pub part_2: Option<String>,
|
||||
pub total_nanos: f64,
|
||||
}
|
||||
|
||||
pub struct TablePosition {
|
||||
pos_start: usize,
|
||||
pos_end: usize,
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_path_for_bin(day: usize) -> String {
|
||||
let day_padded = format!("{day:02}");
|
||||
format!("./src/bin/{day_padded}.rs")
|
||||
#[must_use]
|
||||
pub fn get_path_for_bin(day: Day) -> String {
|
||||
format!("./src/bin/{day}.rs")
|
||||
}
|
||||
|
||||
fn locate_table(readme: &str) -> Result<TablePosition, Error> {
|
||||
|
@ -56,7 +52,7 @@ fn locate_table(readme: &str) -> Result<TablePosition, Error> {
|
|||
Ok(TablePosition { pos_start, pos_end })
|
||||
}
|
||||
|
||||
fn construct_table(prefix: &str, timings: Vec<Timings>, total_millis: f64) -> String {
|
||||
fn construct_table(prefix: &str, timings: Timings, total_millis: f64) -> String {
|
||||
let header = format!("{prefix} Benchmarks");
|
||||
|
||||
let mut lines: Vec<String> = vec![
|
||||
|
@ -67,11 +63,11 @@ fn construct_table(prefix: &str, timings: Vec<Timings>, total_millis: f64) -> St
|
|||
"| :---: | :---: | :---: |".into(),
|
||||
];
|
||||
|
||||
for timing in timings {
|
||||
for timing in timings.data {
|
||||
let path = get_path_for_bin(timing.day);
|
||||
lines.push(format!(
|
||||
"| [Day {}]({}) | `{}` | `{}` |",
|
||||
timing.day,
|
||||
timing.day.into_inner(),
|
||||
path,
|
||||
timing.part_1.unwrap_or_else(|| "-".into()),
|
||||
timing.part_2.unwrap_or_else(|| "-".into())
|
||||
|
@ -85,16 +81,17 @@ fn construct_table(prefix: &str, timings: Vec<Timings>, total_millis: f64) -> St
|
|||
lines.join("\n")
|
||||
}
|
||||
|
||||
fn update_content(s: &mut String, timings: Vec<Timings>, total_millis: f64) -> Result<(), Error> {
|
||||
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: Vec<Timings>, total_millis: f64) -> Result<(), Error> {
|
||||
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(())
|
||||
|
@ -102,29 +99,32 @@ pub fn update(timings: Vec<Timings>, total_millis: f64) -> Result<(), Error> {
|
|||
|
||||
#[cfg(feature = "test_lib")]
|
||||
mod tests {
|
||||
use super::{update_content, Timings, MARKER};
|
||||
use super::{update_content, MARKER};
|
||||
use crate::{day, template::timings::Timing, template::timings::Timings};
|
||||
|
||||
fn get_mock_timings() -> Vec<Timings> {
|
||||
vec![
|
||||
Timings {
|
||||
day: 1,
|
||||
part_1: Some("10ms".into()),
|
||||
part_2: Some("20ms".into()),
|
||||
total_nanos: 3e+10,
|
||||
},
|
||||
Timings {
|
||||
day: 2,
|
||||
part_1: Some("30ms".into()),
|
||||
part_2: Some("40ms".into()),
|
||||
total_nanos: 7e+10,
|
||||
},
|
||||
Timings {
|
||||
day: 4,
|
||||
part_1: Some("40ms".into()),
|
||||
part_2: Some("50ms".into()),
|
||||
total_nanos: 9e+10,
|
||||
},
|
||||
]
|
||||
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]
|
||||
|
|
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
/// Encapsulates code that interacts with solution functions.
|
||||
use crate::template::{aoc_cli, ANSI_ITALIC, ANSI_RESET};
|
||||
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 super::ANSI_BOLD;
|
||||
use crate::template::ANSI_BOLD;
|
||||
use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET};
|
||||
|
||||
pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: u8, part: u8) {
|
||||
pub fn run_part<I: Copy, T: Display>(func: impl Fn(I) -> Option<T>, input: I, day: Day, part: u8) {
|
||||
let part_str = format!("Part {part}");
|
||||
|
||||
let (result, duration, samples) =
|
||||
|
@ -24,13 +25,18 @@ pub fn run_part<I: Clone, T: Display>(func: impl Fn(I) -> Option<T>, input: I, d
|
|||
/// Run a solution part. The behavior differs depending on whether we are running a release or debug build:
|
||||
/// 1. in debug, the function is executed once.
|
||||
/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.)
|
||||
fn run_timed<I: Clone, T>(
|
||||
fn run_timed<I: Copy, T>(
|
||||
func: impl Fn(I) -> T,
|
||||
input: I,
|
||||
hook: impl Fn(&T),
|
||||
) -> (T, Duration, u128) {
|
||||
let timer = Instant::now();
|
||||
let result = func(input.clone());
|
||||
let result = {
|
||||
#[cfg(feature = "dhat-heap")]
|
||||
let _profiler = dhat::Profiler::new_heap();
|
||||
|
||||
func(input)
|
||||
};
|
||||
let base_time = timer.elapsed();
|
||||
|
||||
hook(&result);
|
||||
|
@ -44,27 +50,20 @@ fn run_timed<I: Clone, T>(
|
|||
(result, run.0, run.1)
|
||||
}
|
||||
|
||||
fn bench<I: Clone, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
|
||||
fn bench<I: Copy, T>(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) {
|
||||
let mut stdout = stdout();
|
||||
|
||||
print!(" > {ANSI_ITALIC}benching{ANSI_RESET}");
|
||||
let _ = stdout.flush();
|
||||
|
||||
let bench_iterations = cmp::min(
|
||||
10000,
|
||||
cmp::max(
|
||||
Duration::from_secs(1).as_nanos() / cmp::max(base_time.as_nanos(), 10),
|
||||
10,
|
||||
),
|
||||
);
|
||||
let bench_iterations =
|
||||
(Duration::from_secs(1).as_nanos() / cmp::max(base_time.as_nanos(), 10)).clamp(10, 10000);
|
||||
|
||||
let mut timers: Vec<Duration> = vec![];
|
||||
|
||||
for _ in 0..bench_iterations {
|
||||
// need a clone here to make the borrow checker happy.
|
||||
let cloned = input.clone();
|
||||
let timer = Instant::now();
|
||||
func(cloned);
|
||||
black_box(func(black_box(input)));
|
||||
timers.push(timer.elapsed());
|
||||
}
|
||||
|
||||
|
@ -131,7 +130,7 @@ fn print_result<T: Display>(result: &Option<T>, part: &str, duration_str: &str)
|
|||
/// 2. aoc-cli is installed.
|
||||
fn submit_result<T: Display>(
|
||||
result: T,
|
||||
day: u8,
|
||||
day: Day,
|
||||
part: u8,
|
||||
) -> Option<Result<Output, aoc_cli::AocCommandError>> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
|
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