diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 6731f8f..0000000 --- a/.cargo/config +++ /dev/null @@ -1,6 +0,0 @@ -[alias] -scaffold = "run --bin scaffold -- " -download = "run --bin download -- " - -solve = "run --bin" -all = "run" diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..cf0d80d --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,12 @@ +[alias] +today = "run --quiet --release --features today -- today" +scaffold = "run --quiet --release -- scaffold" +download = "run --quiet --release -- download" +read = "run --quiet --release -- read" + +solve = "run --quiet --release -- solve" +all = "run --quiet --release -- all" +time = "run --quiet --release -- time" + +[env] +AOC_YEAR = "2024" diff --git a/.editorconfig b/.editorconfig index c075e7e..560e94b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,6 +11,7 @@ trim_trailing_whitespace = true [*.txt] insert_final_newline = false +trim_trailing_whitespace = false [*.md] trim_trailing_whitespace = false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dadca12..b67acb7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,25 +6,28 @@ env: CARGO_TERM_COLOR: always jobs: - check: + ci: runs-on: ubuntu-latest - name: Check + name: Continuous Integration steps: - - uses: actions/checkout@v2 - - name: cargo check - run: cargo check - test: - runs-on: ubuntu-latest - name: Test - steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Set up cargo cache + uses: actions/cache@v4 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- - name: cargo test run: cargo test - # uncomment to enable clippy lints - # clippy: - # runs-on: ubuntu-latest - # name: Lint (clippy) - # steps: - # - uses: actions/checkout@v2 - # - name: cargo clippy - # run: cargo clippy -- -D warnings + # uncomment to enable clippy linter + # - name: cargo clippy + # run: cargo clippy -- -D warnings + # uncomment to enable format linter + # - name: cargo fmt + # run: cargo fmt --check diff --git a/.github/workflows/readme-stars.yml b/.github/workflows/readme-stars.yml index e1b2b1e..3de260e 100644 --- a/.github/workflows/readme-stars.yml +++ b/.github/workflows/readme-stars.yml @@ -1,29 +1,24 @@ name: Update readme ⭐️ progress on: - schedule: - - cron: "51 */4 * * *" # Every 4 hours + # !Please set a different minute than 51 if you enable this! + # schedule: + # - cron: "51 */6 * * *" # Every 6 hours workflow_dispatch: jobs: update-readme: runs-on: ubuntu-latest + if: ${{ vars.AOC_ENABLED == 'true' }} + permissions: + contents: write steps: - - uses: actions/checkout@v2 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} + - uses: actions/checkout@v4 - uses: k2bd/advent-readme-stars@v1 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} with: userId: ${{ secrets.AOC_USER_ID }} sessionCookie: ${{ secrets.AOC_SESSION }} year: ${{ secrets.AOC_YEAR }} - - uses: stefanzweifel/git-auto-commit-action@v4 - if: ${{ env.AOC_ENABLED }} - env: - AOC_ENABLED: ${{ secrets.AOC_ENABLED }} + - uses: stefanzweifel/git-auto-commit-action@v5 with: - commit_message: "update readme progess" + commit_message: "update readme progress" diff --git a/.gitignore b/.gitignore index f2fd7aa..216820d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,15 @@ target/ # Advent of Code # @see https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3 -/src/inputs -!/src/inputs/.keep + +data/inputs/* +!data/inputs/.keep +data/puzzles/* +!data/puzzles/.keep + +# Dhat +dhat-heap.json + +# Benchmarks + +data/timings.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 2475a54..996c817 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,6 +2,6 @@ "recommendations": [ "vadimcn.vscode-lldb", "rust-lang.rust-analyzer", - "serayuzgur.crates" + "editorConfig.editorConfig" ] -} \ No newline at end of file +} diff --git a/.vscode/launch.json b/.vscode/launch.json index 0a9370e..2aa7026 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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" diff --git a/Cargo.lock b/Cargo.lock index a53d473..9504be6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,11 +2,272 @@ # It is not intended for manual editing. version = 3 +[[package]] +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.8.0" +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" diff --git a/Cargo.toml b/Cargo.toml index 3ec21e9..038a1a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,30 @@ [package] name = "advent_of_code" -version = "0.8.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 +[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 diff --git a/README.md b/README.md index 216bb10..e3db903 100644 --- a/README.md +++ b/README.md @@ -6,19 +6,22 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. + + --- ## Template setup 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. @@ -30,71 +33,70 @@ This template supports all major OS (macOS, Linux, Windows). ## Usage -### Scaffold a day +### ➑️ Scaffold a day ```sh # example: `cargo scaffold 1` cargo scaffold # output: -# Created module "src/bin/01.rs" -# Created empty input file "src/inputs/01.txt" -# Created empty example file "src/examples/01.txt" +# Created module file "src/bin/01.rs" +# Created empty input file "data/inputs/01.txt" +# Created empty example file "data/examples/01.txt" # --- # πŸŽ„ Type `cargo solve 01` to run your solution. ``` -Individual solutions live in the `./src/bin/` directory as separate binaries. +Individual solutions live in the `./src/bin/` directory as separate binaries. _Inputs_ and _examples_ live in the the `./data` directory. + +Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template.txt) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input. In VS Code, `rust-analyzer` will display buttons for running / debugging these unit tests above the unit test blocks. -Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/bin/scaffold.rs#L11-L41) has _unit tests_ referencing its _example_ file. Use these unit tests to develop and debug your solution against the example input. For some puzzles, it might be easier to forgo the example file and hardcode inputs into the tests. +> [!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. -When editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks. +### ➑️ Download input for a day -### Download input for a day +> [!IMPORTANT] +> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration). -> **Note** -> This command requires [installing the aoc-cli crate](#download-puzzle-inputs-via-aoc-cli). +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` cargo download # output: -# Downloading input with aoc-cli... -# Loaded session cookie from "/home/felix/.adventofcode.session". -# Downloading input for day 1, 2021... -# Saving puzzle input to "/tmp/tmp.MBdcAdL9Iw/input"... -# Done! +# [INFO aoc] πŸŽ„ aoc-cli - Advent of Code command-line tool +# [INFO aoc_client] πŸŽ… Saved puzzle to 'data/puzzles/01.md' +# [INFO aoc_client] πŸŽ… Saved input to 'data/inputs/01.txt' # --- -# πŸŽ„ Successfully wrote input to "src/inputs/01.txt"! +# πŸŽ„ Successfully wrote input to "data/inputs/01.txt". +# πŸŽ„ Successfully wrote puzzle to "data/puzzles/01.md". ``` -To download inputs for previous years, append the `--year/-y` flag. _(example: `cargo download 1 --year 2020`)_ - -Puzzle inputs are not checked into git. [Reasoning](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3). - -### Run solutions for a day +### ➑️ Run solutions for a day ```sh # example: `cargo solve 01` cargo solve # output: +# Finished dev [unoptimized + debuginfo] target(s) in 0.13s # Running `target/debug/01` -# πŸŽ„ Part 1 πŸŽ„ -# -# 6 (elapsed: 37.03Β΅s) -# -# πŸŽ„ Part 2 πŸŽ„ -# -# 9 (elapsed: 33.18Β΅s) +# Part 1: 42 (166.0ns) +# Part 2: 42 (41.0ns) ``` -`solve` is an alias for `cargo run --bin`. To run an optimized version for benchmarking, append the `--release` flag. +The `solve` command runs your solution against real puzzle inputs. To run an optimized build of your code, append the `--release` flag as with any other rust program. + +#### Submitting solutions -Displayed _timings_ show the raw execution time of your solution without overhead (e.g. file reads). +> [!IMPORTANT] +> This requires [installing the aoc-cli crate](#configure-aoc-cli-integration). -### Run all solutions +Append the `--submit ` option to the `solve` command to submit your solution for checking. + +### ➑️ Run all solutions ```sh cargo all @@ -104,34 +106,108 @@ cargo all # ---------- # | Day 01 | # ---------- -# πŸŽ„ Part 1 πŸŽ„ -# -# 0 (elapsed: 170.00Β΅s) -# -# πŸŽ„ Part 2 πŸŽ„ -# -# 0 (elapsed: 30.00Β΅s) +# Part 1: 42 (19.0ns) +# Part 2: 42 (19.0ns) # <...other days...> # Total: 0.20ms ``` -`all` is an alias for `cargo run`. To run an optimized version for benchmarking, use the `--release` flag. +This runs all solutions sequentially and prints output to the command-line. Same as for the `solve` command, the `--release` flag runs an optimized build. + +### ➑️ Benchmark your solutions + +```sh +# example: `cargo time 8 --store` +cargo time [--all] [--store] + +# output: +# Day 08 +# ------ +# Part 1: 1 (39.0ns @ 10000 samples) +# Part 2: 2 (39.0ns @ 10000 samples) +# +# Total (Run): 0.00ms +# +# Stored updated benchmarks. +``` + +The `cargo time` command allows you to benchmark your code and store timings in the readme. When benching, the runner will run your code between `10` and `10.000` times, depending on execution time of first execution, and print the average execution time. + +`cargo time` has three modes of execution: + + 1. `cargo time` without arguments incrementally benches solutions that do not have been stored in the readme yet and skips the rest. + 2. `cargo time ` 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`. -_Total timing_ is computed from individual solution _timings_ and excludes as much overhead as possible. +> 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 solutions against the example input +### ➑️ Run all tests ```sh cargo test ``` -### Format code +To run tests for a specific day, append `--bin `, e.g. `cargo test --bin 01`. You can further scope it down to a specific part, e.g. `cargo test --bin 01 part_one`. + +### ➑️ Read puzzle description + +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). + +```sh +# example: `cargo read 1` +cargo read + +# output: +# Loaded session cookie from "/Users//.adventofcode.session". +# Fetching puzzle for day 1, 2022... +# ...the input... +``` + +### ➑️ Scaffold, download & read the current aoc day + +> [!IMPORTANT] +> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration). + +During december, the `today` shorthand command can be used to: + + - scaffold a solution for the current day + - download its input + - and read the puzzle + +in one go. + +```sh +# example: `cargo today` on December 1st +cargo today + +# output: +# Created module file "src/bin/01.rs" +# Created empty input file "data/inputs/01.txt" +# Created empty example file "data/examples/01.txt" +# --- +# πŸŽ„ Type `cargo solve 01` to run your solution. +# [INFO aoc] πŸŽ„ aoc-cli - Advent of Code command-line tool +# [INFO aoc_client] πŸŽ… Saved puzzle to 'data/puzzles/01.md' +# [INFO aoc_client] πŸŽ… Saved input to 'data/inputs/01.txt' +# --- +# πŸŽ„ Successfully wrote input to "data/inputs/01.txt". +# πŸŽ„ Successfully wrote puzzle to "data/puzzles/01.md". +# +# Loaded session cookie from "/Users//.adventofcode.session". +# Fetching puzzle for day 1, 2022... +# ...the input... +``` + +### ➑️ Format code ```sh cargo fmt ``` -### Lint code +### ➑️ Lint code ```sh cargo clippy @@ -139,16 +215,12 @@ cargo clippy ## Optional template features -### Download puzzle inputs via aoc-cli - -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.5.0`. -2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. 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 value. - -Once installed, you can use the [download command](#download-input-for-a-day). +### Configure aoc-cli integration -### Enable clippy lints in CI +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.12.0` +2. Create the file `/.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] -Uncomment the `clippy` job in the `ci.yml` workflow to enable clippy checks in CI. +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 @@ -164,11 +236,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_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. + +### 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). @@ -181,14 +282,10 @@ Go to the _Secrets_ tab in your repository settings and create the following sec - [itertools](https://crates.io/crates/itertools): Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles. - [regex](https://crates.io/crates/regex): Official regular expressions implementation for Rust. -A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/crates). +A curated list of popular crates can be found on [blessed.rs](https://blessed.rs/crates). Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md) -## Common pitfalls - -- **Integer overflows:** This template uses 32-bit integers by default because it is generally faster - for example when packed in large arrays or structs - than using 64-bit integers everywhere. For some problems, solutions for real input might exceed 32-bit integer space. While this is checked and panics in `debug` mode, integers [wrap](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow) in `release` mode, leading to wrong output when running your solution. - ## Footnotes [^1]: The session cookie might expire after a while (~1 month) which causes the downloads to fail. To fix this issue, refresh the `.adventofcode.session` file. diff --git a/src/examples/.keep b/data/examples/.keep similarity index 100% rename from src/examples/.keep rename to data/examples/.keep diff --git a/src/inputs/.keep b/data/inputs/.keep similarity index 100% rename from src/inputs/.keep rename to data/inputs/.keep diff --git a/data/puzzles/.keep b/data/puzzles/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/bin/.keep b/src/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/bin/download.rs b/src/bin/download.rs deleted file mode 100644 index edb567d..0000000 --- a/src/bin/download.rs +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use std::io::Write; -use std::path::PathBuf; -use std::{env::temp_dir, io, process::Command}; -use std::{fs, process}; - -struct Args { - day: u8, - year: Option, -} - -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - Ok(Args { - day: args.free_from_str()?, - year: args.opt_value_from_str(["-y", "--year"])?, - }) -} - -fn remove_file(path: &PathBuf) { - #[allow(unused_must_use)] - { - fs::remove_file(path); - } -} - -fn exit_with_status(status: i32, path: &PathBuf) -> ! { - remove_file(path); - process::exit(status); -} - -fn main() { - // acquire a temp file path to write aoc-cli output to. - // aoc-cli expects this file not to be present - delete just in case. - let mut tmp_file_path = temp_dir(); - tmp_file_path.push("aoc_input_tmp"); - remove_file(&tmp_file_path); - - let args = match parse_args() { - Ok(args) => args, - Err(e) => { - eprintln!("Failed to process arguments: {}", e); - exit_with_status(1, &tmp_file_path); - } - }; - - let day_padded = format!("{:02}", args.day); - let input_path = format!("src/inputs/{}.txt", day_padded); - - // check if aoc binary exists and is callable. - if Command::new("aoc").arg("-V").output().is_err() { - eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); - exit_with_status(1, &tmp_file_path); - } - - let mut cmd_args = vec![]; - - if let Some(year) = args.year { - cmd_args.push("--year".into()); - cmd_args.push(year.to_string()); - } - - cmd_args.append(&mut vec![ - "--input-file".into(), - tmp_file_path.to_string_lossy().to_string(), - "--day".into(), - args.day.to_string(), - "download".into(), - ]); - - println!("Downloading input with >aoc {}", cmd_args.join(" ")); - - match Command::new("aoc").args(cmd_args).output() { - Ok(cmd_output) => { - io::stdout() - .write_all(&cmd_output.stdout) - .expect("could not write cmd stdout to pipe."); - io::stderr() - .write_all(&cmd_output.stderr) - .expect("could not write cmd stderr to pipe."); - if !cmd_output.status.success() { - exit_with_status(1, &tmp_file_path); - } - } - Err(e) => { - eprintln!("failed to spawn aoc-cli: {}", e); - exit_with_status(1, &tmp_file_path); - } - } - - match fs::copy(&tmp_file_path, &input_path) { - Ok(_) => { - println!("---"); - println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); - exit_with_status(0, &tmp_file_path); - } - Err(e) => { - eprintln!("could not copy downloaded input to input file: {}", e); - exit_with_status(1, &tmp_file_path); - } - } -} diff --git a/src/bin/scaffold.rs b/src/bin/scaffold.rs deleted file mode 100644 index 0c77b4d..0000000 --- a/src/bin/scaffold.rs +++ /dev/null @@ -1,114 +0,0 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use std::{ - fs::{File, OpenOptions}, - io::Write, - process, -}; - -const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option { - None -} - -pub fn part_two(input: &str) -> Option { - None -} - -fn main() { - let input = &advent_of_code::read_file("inputs", DAY); - advent_of_code::solve!(1, part_one, input); - advent_of_code::solve!(2, part_two, input); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_part_one() { - let input = advent_of_code::read_file("examples", DAY); - assert_eq!(part_one(&input), None); - } - - #[test] - fn test_part_two() { - let input = advent_of_code::read_file("examples", DAY); - assert_eq!(part_two(&input), None); - } -} -"###; - -fn parse_args() -> Result { - let mut args = pico_args::Arguments::from_env(); - args.free_from_str() -} - -fn safe_create_file(path: &str) -> Result { - OpenOptions::new().write(true).create_new(true).open(path) -} - -fn create_file(path: &str) -> Result { - OpenOptions::new().write(true).create(true).open(path) -} - -fn main() { - let day = match parse_args() { - Ok(day) => day, - Err(_) => { - eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`"); - process::exit(1); - } - }; - - let day_padded = format!("{:02}", day); - - let input_path = format!("src/inputs/{}.txt", day_padded); - let example_path = format!("src/examples/{}.txt", day_padded); - let module_path = format!("src/bin/{}.rs", day_padded); - - let mut file = match safe_create_file(&module_path) { - Ok(file) => file, - Err(e) => { - eprintln!("Failed to create module file: {}", e); - process::exit(1); - } - }; - - match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) { - Ok(_) => { - println!("Created module file \"{}\"", &module_path); - } - Err(e) => { - eprintln!("Failed to write module contents: {}", e); - process::exit(1); - } - } - - match create_file(&input_path) { - Ok(_) => { - println!("Created empty input file \"{}\"", &input_path); - } - Err(e) => { - eprintln!("Failed to create input file: {}", e); - process::exit(1); - } - } - - match create_file(&example_path) { - Ok(_) => { - println!("Created empty example file \"{}\"", &example_path); - } - Err(e) => { - eprintln!("Failed to create example file: {}", e); - process::exit(1); - } - } - - println!("---"); - println!( - "πŸŽ„ Type `cargo solve {}` to run your solution.", - &day_padded - ); -} diff --git a/src/helpers.rs b/src/helpers.rs deleted file mode 100644 index 079071f..0000000 --- a/src/helpers.rs +++ /dev/null @@ -1,4 +0,0 @@ -/* - * Use this file if you want to extract helpers from your solutions. - * Example import from this file: `use advent_of_code::helpers::example_fn;`. - */ diff --git a/src/lib.rs b/src/lib.rs index 23acd28..27d7df8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,125 +1,3 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - * Prefer `./helpers.rs` if you want to extract code from your solutions. - */ -use std::env; -use std::fs; +pub mod template; -pub mod helpers; - -pub const ANSI_ITALIC: &str = "\x1b[3m"; -pub const ANSI_BOLD: &str = "\x1b[1m"; -pub const ANSI_RESET: &str = "\x1b[0m"; - -#[macro_export] -macro_rules! solve { - ($part:expr, $solver:ident, $input:expr) => {{ - use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET}; - use std::fmt::Display; - use std::time::Instant; - - fn print_result(func: impl FnOnce(&str) -> Option, input: &str) { - let timer = Instant::now(); - let result = func(input); - let elapsed = timer.elapsed(); - match result { - Some(result) => { - println!( - "{} {}(elapsed: {:.2?}){}", - result, ANSI_ITALIC, elapsed, ANSI_RESET - ); - } - None => { - println!("not solved.") - } - } - } - - println!("πŸŽ„ {}Part {}{} πŸŽ„", ANSI_BOLD, $part, ANSI_RESET); - print_result($solver, $input); - }}; -} - -pub fn read_file(folder: &str, day: u8) -> String { - let cwd = env::current_dir().unwrap(); - - let filepath = cwd.join("src").join(folder).join(format!("{:02}.txt", day)); - - let f = fs::read_to_string(filepath); - f.expect("could not open input file") -} - -fn parse_time(val: &str, postfix: &str) -> f64 { - val.split(postfix).next().unwrap().parse().unwrap() -} - -pub fn parse_exec_time(output: &str) -> f64 { - output.lines().fold(0_f64, |acc, l| { - if !l.contains("elapsed:") { - acc - } else { - let timing = l.split("(elapsed: ").last().unwrap(); - // use `contains` istd. of `ends_with`: string may contain ANSI escape sequences. - // for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200 - if timing.contains("ns)") { - acc // range below rounding precision. - } else if timing.contains("Β΅s)") { - acc + parse_time(timing, "Β΅s") / 1000_f64 - } else if timing.contains("ms)") { - acc + parse_time(timing, "ms") - } else if timing.contains("s)") { - acc + parse_time(timing, "s") * 1000_f64 - } else { - acc - } - } - }) -} - -/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333 -#[cfg(test)] -macro_rules! assert_approx_eq { - ($a:expr, $b:expr) => {{ - let (a, b) = (&$a, &$b); - assert!( - (*a - *b).abs() < 1.0e-6, - "{} is not approximately equal to {}", - *a, - *b - ); - }}; -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_exec_time() { - assert_approx_eq!( - parse_exec_time(&format!( - "πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 74.13ns){}\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 50.00ns){}", - ANSI_RESET, ANSI_RESET - )), - 0_f64 - ); - - assert_approx_eq!( - parse_exec_time("πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 755Β΅s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 700Β΅s)"), - 1.455_f64 - ); - - assert_approx_eq!( - parse_exec_time("πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 70Β΅s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 1.45ms)"), - 1.52_f64 - ); - - assert_approx_eq!( - parse_exec_time( - "πŸŽ„ Part 1 πŸŽ„\n0 (elapsed: 10.3s)\nπŸŽ„ Part 2 πŸŽ„\n0 (elapsed: 100.50ms)" - ), - 10400.50_f64 - ); - } -} +// Use this file to add helper functions and additional modules. diff --git a/src/main.rs b/src/main.rs index 45701ab..2a360fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,46 +1,144 @@ -/* - * This file contains template code. - * There is no need to edit this file unless you want to change template functionality. - */ -use advent_of_code::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET}; -use std::process::Command; +use advent_of_code::template::commands::{all, download, read, scaffold, solve, time}; +use args::{parse, AppArguments}; -fn main() { - let total: f64 = (1..=25) - .map(|day| { - let day = format!("{:02}", day); - - let cmd = Command::new("cargo") - .args(["run", "--release", "--bin", &day]) - .output() - .unwrap(); - - println!("----------"); - println!("{}| Day {} |{}", ANSI_BOLD, day, ANSI_RESET); - println!("----------"); - - let output = String::from_utf8(cmd.stdout).unwrap(); - let is_empty = output.is_empty(); - - println!( - "{}", - if is_empty { - "Not solved." - } else { - output.trim() - } - ); +#[cfg(feature = "today")] +use advent_of_code::template::Day; +#[cfg(feature = "today")] +use std::process; + +mod args { + use advent_of_code::template::Day; + use std::process; + + pub enum AppArguments { + Download { + day: Day, + }, + Read { + day: Day, + }, + Scaffold { + day: Day, + download: bool, + overwrite: bool, + }, + Solve { + day: Day, + release: bool, + dhat: bool, + submit: Option, + }, + All { + release: bool, + }, + Time { + all: bool, + day: Option, + store: bool, + }, + #[cfg(feature = "today")] + Today, + } - if is_empty { - 0_f64 - } else { - advent_of_code::parse_exec_time(&output) + pub fn parse() -> Result> { + let mut args = pico_args::Arguments::from_env(); + + let app_args = match args.subcommand()?.as_deref() { + Some("all") => AppArguments::All { + release: args.contains("--release"), + }, + Some("time") => { + let all = args.contains("--all"); + let store = args.contains("--store"); + + AppArguments::Time { + all, + day: args.opt_free_from_str()?, + store, + } + } + Some("download") => AppArguments::Download { + day: args.free_from_str()?, + }, + Some("read") => AppArguments::Read { + day: args.free_from_str()?, + }, + Some("scaffold") => AppArguments::Scaffold { + day: args.free_from_str()?, + download: args.contains("--download"), + overwrite: args.contains("--overwrite"), + }, + Some("solve") => AppArguments::Solve { + day: args.free_from_str()?, + release: args.contains("--release"), + submit: args.opt_value_from_str("--submit")?, + dhat: args.contains("--dhat"), + }, + #[cfg(feature = "today")] + Some("today") => AppArguments::Today, + Some(x) => { + eprintln!("Unknown command: {x}"); + process::exit(1); } - }) - .sum(); + None => { + eprintln!("No command specified."); + process::exit(1); + } + }; + + let remaining = args.finish(); + if !remaining.is_empty() { + eprintln!("Warning: unknown argument(s): {remaining:?}."); + } + + Ok(app_args) + } +} - println!( - "{}Total:{} {}{:.2}ms{}", - ANSI_BOLD, ANSI_RESET, ANSI_ITALIC, total, ANSI_RESET - ); +fn main() { + match parse() { + Err(err) => { + eprintln!("Error: {err}"); + std::process::exit(1); + } + Ok(args) => match args { + AppArguments::All { release } => all::handle(release), + AppArguments::Time { day, all, store } => time::handle(day, all, store), + AppArguments::Download { day } => download::handle(day), + AppArguments::Read { day } => read::handle(day), + AppArguments::Scaffold { + day, + download, + overwrite, + } => { + scaffold::handle(day, overwrite); + if download { + download::handle(day); + } + } + AppArguments::Solve { + day, + release, + dhat, + submit, + } => solve::handle(day, release, dhat, submit), + #[cfg(feature = "today")] + AppArguments::Today => { + match Day::today() { + Some(day) => { + scaffold::handle(day, false); + download::handle(day); + read::handle(day) + } + None => { + eprintln!( + "`today` command can only be run between the 1st and \ + the 25th of december. Please use `scaffold` with a specific day." + ); + process::exit(1) + } + }; + } + }, + }; } diff --git a/src/template.txt b/src/template.txt new file mode 100644 index 0000000..87eac99 --- /dev/null +++ b/src/template.txt @@ -0,0 +1,26 @@ +advent_of_code::solution!(%DAY_NUMBER%); + +pub fn part_one(input: &str) -> Option { + None +} + +pub fn part_two(input: &str) -> Option { + 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); + } +} diff --git a/src/template/aoc_cli.rs b/src/template/aoc_cli.rs new file mode 100644 index 0000000..2d3300d --- /dev/null +++ b/src/template/aoc_cli.rs @@ -0,0 +1,125 @@ +/// Wrapper module around the "aoc-cli" command-line. +use std::{ + fmt::Display, + process::{Command, Output, Stdio}, +}; + +use crate::template::Day; + +#[derive(Debug)] +pub enum AocCommandError { + CommandNotFound, + CommandNotCallable, + BadExitStatus(Output), +} + +impl Display for AocCommandError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AocCommandError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), + AocCommandError::CommandNotCallable => write!(f, "aoc-cli could not be called."), + AocCommandError::BadExitStatus(_) => { + write!(f, "aoc-cli exited with a non-zero status.") + } + } + } +} + +pub fn check() -> Result<(), AocCommandError> { + Command::new("aoc") + .arg("-V") + .output() + .map_err(|_| AocCommandError::CommandNotFound)?; + Ok(()) +} + +pub fn read(day: Day) -> Result { + let puzzle_path = get_puzzle_path(day); + + let args = build_args( + "read", + &[ + "--description-only".into(), + "--puzzle-file".into(), + puzzle_path, + ], + day, + ); + + call_aoc_cli(&args) +} + +pub fn download(day: Day) -> Result { + let input_path = get_input_path(day); + let puzzle_path = get_puzzle_path(day); + + let args = build_args( + "download", + &[ + "--overwrite".into(), + "--input-file".into(), + input_path.to_string(), + "--puzzle-file".into(), + puzzle_path.to_string(), + ], + day, + ); + + let output = call_aoc_cli(&args)?; + println!("---"); + println!("πŸŽ„ Successfully wrote input to \"{}\".", &input_path); + println!("πŸŽ„ Successfully wrote puzzle to \"{}\".", &puzzle_path); + Ok(output) +} + +pub fn submit(day: Day, part: u8, result: &str) -> Result { + // workaround: the argument order is inverted for submit. + let mut args = build_args("submit", &[], day); + args.push(part.to_string()); + args.push(result.to_string()); + call_aoc_cli(&args) +} + +fn get_input_path(day: Day) -> String { + format!("data/inputs/{day}.txt") +} + +fn get_puzzle_path(day: Day) -> String { + format!("data/puzzles/{day}.md") +} + +fn get_year() -> Option { + match std::env::var("AOC_YEAR") { + Ok(x) => x.parse().ok().or(None), + Err(_) => None, + } +} + +fn build_args(command: &str, args: &[String], day: Day) -> Vec { + let mut cmd_args = args.to_vec(); + + if let Some(year) = get_year() { + cmd_args.push("--year".into()); + cmd_args.push(year.to_string()); + } + + cmd_args.append(&mut vec!["--day".into(), day.to_string(), command.into()]); + + cmd_args +} + +fn call_aoc_cli(args: &[String]) -> Result { + // println!("Calling >aoc with: {}", args.join(" ")); + let output = Command::new("aoc") + .args(args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(|_| AocCommandError::CommandNotCallable)?; + + if output.status.success() { + Ok(output) + } else { + Err(AocCommandError::BadExitStatus(output)) + } +} diff --git a/src/template/commands/all.rs b/src/template/commands/all.rs new file mode 100644 index 0000000..b844e1e --- /dev/null +++ b/src/template/commands/all.rs @@ -0,0 +1,5 @@ +use crate::template::{all_days, run_multi::run_multi}; + +pub fn handle(is_release: bool) { + run_multi(&all_days().collect(), is_release, false); +} diff --git a/src/template/commands/download.rs b/src/template/commands/download.rs new file mode 100644 index 0000000..9274f05 --- /dev/null +++ b/src/template/commands/download.rs @@ -0,0 +1,14 @@ +use crate::template::{aoc_cli, Day}; +use std::process; + +pub fn handle(day: Day) { + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + if let Err(e) = aoc_cli::download(day) { + eprintln!("failed to call aoc-cli: {e}"); + process::exit(1); + }; +} diff --git a/src/template/commands/mod.rs b/src/template/commands/mod.rs new file mode 100644 index 0000000..36be280 --- /dev/null +++ b/src/template/commands/mod.rs @@ -0,0 +1,6 @@ +pub mod all; +pub mod download; +pub mod read; +pub mod scaffold; +pub mod solve; +pub mod time; diff --git a/src/template/commands/read.rs b/src/template/commands/read.rs new file mode 100644 index 0000000..3e1a307 --- /dev/null +++ b/src/template/commands/read.rs @@ -0,0 +1,15 @@ +use std::process; + +use crate::template::{aoc_cli, Day}; + +pub fn handle(day: Day) { + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + if let Err(e) = aoc_cli::read(day) { + eprintln!("failed to call aoc-cli: {e}"); + process::exit(1); + }; +} diff --git a/src/template/commands/scaffold.rs b/src/template/commands/scaffold.rs new file mode 100644 index 0000000..fc3d950 --- /dev/null +++ b/src/template/commands/scaffold.rs @@ -0,0 +1,79 @@ +use std::{ + fs::{File, OpenOptions}, + io::Write, + process, +}; + +use crate::template::Day; + +const MODULE_TEMPLATE: &str = + include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/template.txt")); + +fn safe_create_file(path: &str, overwrite: bool) -> Result { + let mut file = OpenOptions::new(); + if overwrite { + file.create(true); + } else { + file.create_new(true); + } + file.truncate(true).write(true).open(path) +} + +fn create_file(path: &str) -> Result { + OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(path) +} + +pub fn handle(day: Day, overwrite: bool) { + let input_path = format!("data/inputs/{day}.txt"); + let example_path = format!("data/examples/{day}.txt"); + let module_path = format!("src/bin/{day}.rs"); + + let mut file = match safe_create_file(&module_path, overwrite) { + Ok(file) => file, + Err(e) => { + eprintln!("Failed to create module file: {e}"); + process::exit(1); + } + }; + + match file.write_all( + MODULE_TEMPLATE + .replace("%DAY_NUMBER%", &day.into_inner().to_string()) + .as_bytes(), + ) { + Ok(()) => { + println!("Created module file \"{}\"", &module_path); + } + Err(e) => { + eprintln!("Failed to write module contents: {e}"); + process::exit(1); + } + } + + match create_file(&input_path) { + Ok(_) => { + println!("Created empty input file \"{}\"", &input_path); + } + Err(e) => { + eprintln!("Failed to create input file: {e}"); + process::exit(1); + } + } + + match create_file(&example_path) { + Ok(_) => { + println!("Created empty example file \"{}\"", &example_path); + } + Err(e) => { + eprintln!("Failed to create example file: {e}"); + process::exit(1); + } + } + + println!("---"); + println!("πŸŽ„ Type `cargo solve {day}` to run your solution."); +} diff --git a/src/template/commands/solve.rs b/src/template/commands/solve.rs new file mode 100644 index 0000000..ec92a6f --- /dev/null +++ b/src/template/commands/solve.rs @@ -0,0 +1,34 @@ +use std::process::{Command, Stdio}; + +use crate::template::Day; + +pub fn handle(day: Day, release: bool, dhat: bool, submit_part: Option) { + let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day.to_string()]; + + if dhat { + cmd_args.extend([ + "--profile".to_string(), + "dhat".to_string(), + "--features".to_string(), + "dhat-heap".to_string(), + ]); + } else if release { + cmd_args.push("--release".to_string()); + } + + cmd_args.push("--".to_string()); + + if let Some(submit_part) = submit_part { + cmd_args.push("--submit".to_string()); + cmd_args.push(submit_part.to_string()); + } + + let mut cmd = Command::new("cargo") + .args(&cmd_args) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .spawn() + .unwrap(); + + cmd.wait().unwrap(); +} diff --git a/src/template/commands/time.rs b/src/template/commands/time.rs new file mode 100644 index 0000000..49b91a8 --- /dev/null +++ b/src/template/commands/time.rs @@ -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, 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."); + } + } + } +} diff --git a/src/template/day.rs b/src/template/day.rs new file mode 100644 index 0000000..99b8280 --- /dev/null +++ b/src/template/day.rs @@ -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 { + 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 { + 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 for Day { + fn eq(&self, other: &u8) -> bool { + self.0.eq(other) + } +} + +impl PartialOrd for Day { + fn partial_cmp(&self, other: &u8) -> Option { + self.0.partial_cmp(other) + } +} + +/* -------------------------------------------------------------------------- */ + +impl FromStr for Day { + type Err = DayFromStrError; + + fn from_str(s: &str) -> Result { + 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 { + 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); + } +} + +/* -------------------------------------------------------------------------- */ diff --git a/src/template/mod.rs b/src/template/mod.rs new file mode 100644 index 0000000..dd8e4c0 --- /dev/null +++ b/src/template/mod.rs @@ -0,0 +1,68 @@ +use std::{env, fs}; + +pub mod aoc_cli; +pub mod commands; +pub mod runner; + +pub use day::*; + +mod day; +mod readme_benchmarks; +mod run_multi; +mod timings; + +pub const ANSI_ITALIC: &str = "\x1b[3m"; +pub const ANSI_BOLD: &str = "\x1b[1m"; +pub const ANSI_RESET: &str = "\x1b[0m"; + +/// Helper function that reads a text file to a string. +#[must_use] +pub fn read_file(folder: &str, day: Day) -> String { + let cwd = env::current_dir().unwrap(); + let filepath = cwd.join("data").join(folder).join(format!("{day}.txt")); + let f = fs::read_to_string(filepath); + f.expect("could not open input file") +} + +/// Helper function that reads a text file to string, appending a part suffix. E.g. like `01-2.txt`. +#[must_use] +pub fn read_file_part(folder: &str, day: Day, part: u8) -> String { + let cwd = env::current_dir().unwrap(); + let filepath = cwd + .join("data") + .join(folder) + .join(format!("{day}-{part}.txt")); + let f = fs::read_to_string(filepath); + f.expect("could not open input file") +} + +/// Creates the constant `DAY` and sets up the input and runner for each part. +/// +/// The optional, second parameter (1 or 2) allows you to only run a single part of the solution. +#[macro_export] +macro_rules! solution { + ($day:expr) => { + $crate::solution!(@impl $day, [part_one, 1] [part_two, 2]); + }; + ($day:expr, 1) => { + $crate::solution!(@impl $day, [part_one, 1]); + }; + ($day:expr, 2) => { + $crate::solution!(@impl $day, [part_two, 2]); + }; + + (@impl $day:expr, $( [$func:expr, $part:expr] )*) => { + /// The current day. + const DAY: $crate::template::Day = $crate::day!($day); + + #[cfg(feature = "dhat-heap")] + #[global_allocator] + static ALLOC: dhat::Alloc = dhat::Alloc; + + fn main() { + use $crate::template::runner::*; + let input = $crate::template::read_file("inputs", DAY); + $( run_part($func, &input, DAY, $part); )* + } + }; +} diff --git a/src/template/readme_benchmarks.rs b/src/template/readme_benchmarks.rs new file mode 100644 index 0000000..5c42ae4 --- /dev/null +++ b/src/template/readme_benchmarks.rs @@ -0,0 +1,183 @@ +/// Module that updates the readme me with timing information. +/// The approach taken is similar to how `aoc-readme-stars` handles this. +use std::{fs, io}; + +use crate::template::timings::Timings; +use crate::template::Day; + +static MARKER: &str = ""; + +#[allow(dead_code)] +#[derive(Debug)] +pub enum Error { + Parser(String), + IO(io::Error), +} + +impl From for Error { + fn from(e: std::io::Error) -> Self { + Error::IO(e) + } +} + +pub struct TablePosition { + pos_start: usize, + pos_end: usize, +} + +#[must_use] +pub fn get_path_for_bin(day: Day) -> String { + format!("./src/bin/{day}.rs") +} + +fn locate_table(readme: &str) -> Result { + let matches: Vec<_> = readme.match_indices(MARKER).collect(); + + if matches.len() > 2 { + return Err(Error::Parser( + "{}: too many occurences of marker in README.".into(), + )); + } + + let pos_start = matches + .first() + .map(|m| m.0) + .ok_or_else(|| Error::Parser("Could not find table start position.".into()))?; + + let pos_end = matches + .last() + .map(|m| m.0 + m.1.len()) + .ok_or_else(|| Error::Parser("Could not find table end position.".into()))?; + + Ok(TablePosition { pos_start, pos_end }) +} + +fn construct_table(prefix: &str, timings: Timings, total_millis: f64) -> String { + let header = format!("{prefix} Benchmarks"); + + let mut lines: Vec = vec![ + MARKER.into(), + header, + String::new(), + "| Day | Part 1 | Part 2 |".into(), + "| :---: | :---: | :---: |".into(), + ]; + + for timing in timings.data { + let path = get_path_for_bin(timing.day); + lines.push(format!( + "| [Day {}]({}) | `{}` | `{}` |", + timing.day.into_inner(), + path, + timing.part_1.unwrap_or_else(|| "-".into()), + timing.part_2.unwrap_or_else(|| "-".into()) + )); + } + + lines.push(String::new()); + lines.push(format!("**Total: {total_millis:.2}ms**")); + lines.push(MARKER.into()); + + lines.join("\n") +} + +fn update_content(s: &mut String, timings: Timings, total_millis: f64) -> Result<(), Error> { + let positions = locate_table(s)?; + let table = construct_table("##", timings, total_millis); + s.replace_range(positions.pos_start..positions.pos_end, &table); + Ok(()) +} + +pub fn update(timings: Timings) -> Result<(), Error> { + let path = "README.md"; + let mut readme = String::from_utf8_lossy(&fs::read(path)?).to_string(); + let total_millis = timings.total_millis(); + update_content(&mut readme, timings, total_millis)?; + fs::write(path, &readme)?; + Ok(()) +} + +#[cfg(feature = "test_lib")] +mod tests { + use super::{update_content, MARKER}; + use crate::{day, template::timings::Timing, template::timings::Timings}; + + fn get_mock_timings() -> Timings { + Timings { + data: vec![ + Timing { + day: day!(1), + part_1: Some("10ms".into()), + part_2: Some("20ms".into()), + total_nanos: 3e+10, + }, + Timing { + day: day!(2), + part_1: Some("30ms".into()), + part_2: Some("40ms".into()), + total_nanos: 7e+10, + }, + Timing { + day: day!(4), + part_1: Some("40ms".into()), + part_2: Some("50ms".into()), + total_nanos: 9e+10, + }, + ], + } + } + + #[test] + #[should_panic] + fn errors_if_marker_not_present() { + let mut s = "# readme".to_string(); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + } + + #[test] + #[should_panic] + fn errors_if_too_many_markers_present() { + let mut s = format!("{} {} {}", MARKER, MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + } + + #[test] + fn updates_empty_benchmarks() { + let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + assert_eq!(s.contains("## Benchmarks"), true); + } + + #[test] + fn updates_existing_benchmarks() { + let mut s = format!("foo\nbar\n{}{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + assert_eq!(s.matches(MARKER).collect::>().len(), 2); + assert_eq!(s.matches("## Benchmarks").collect::>().len(), 1); + } + + #[test] + fn format_benchmarks() { + let mut s = format!("foo\nbar\n{}\n{}\nbaz", MARKER, MARKER); + update_content(&mut s, get_mock_timings(), 190.0).unwrap(); + let expected = [ + "foo", + "bar", + "", + "## Benchmarks", + "", + "| Day | Part 1 | Part 2 |", + "| :---: | :---: | :---: |", + "| [Day 1](./src/bin/01.rs) | `10ms` | `20ms` |", + "| [Day 2](./src/bin/02.rs) | `30ms` | `40ms` |", + "| [Day 4](./src/bin/04.rs) | `40ms` | `50ms` |", + "", + "**Total: 190.00ms**", + "", + "baz", + ] + .join("\n"); + assert_eq!(s, expected); + } +} diff --git a/src/template/run_multi.rs b/src/template/run_multi.rs new file mode 100644 index 0000000..c951faa --- /dev/null +++ b/src/template/run_multi.rs @@ -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, is_release: bool, is_timed: bool) -> Option { + let mut timings: Vec = 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 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, 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 { + 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::().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); + } + } +} diff --git a/src/template/runner.rs b/src/template/runner.rs new file mode 100644 index 0000000..43a2bb4 --- /dev/null +++ b/src/template/runner.rs @@ -0,0 +1,165 @@ +/// Encapsulates code that interacts with solution functions. +use std::fmt::Display; +use std::hint::black_box; +use std::io::{stdout, Write}; +use std::process::Output; +use std::time::{Duration, Instant}; +use std::{cmp, env, process}; + +use crate::template::ANSI_BOLD; +use crate::template::{aoc_cli, Day, ANSI_ITALIC, ANSI_RESET}; + +pub fn run_part(func: impl Fn(I) -> Option, input: I, day: Day, part: u8) { + let part_str = format!("Part {part}"); + + let (result, duration, samples) = + run_timed(func, input, |result| print_result(result, &part_str, "")); + + print_result(&result, &part_str, &format_duration(&duration, samples)); + + if let Some(result) = result { + submit_result(result, day, part); + } +} + +/// Run a solution part. The behavior differs depending on whether we are running a release or debug build: +/// 1. in debug, the function is executed once. +/// 2. in release, the function is benched (approx. 1 second of execution time or 10 samples, whatever take longer.) +fn run_timed( + func: impl Fn(I) -> T, + input: I, + hook: impl Fn(&T), +) -> (T, Duration, u128) { + let timer = Instant::now(); + let result = { + #[cfg(feature = "dhat-heap")] + let _profiler = dhat::Profiler::new_heap(); + + func(input) + }; + let base_time = timer.elapsed(); + + hook(&result); + + let run = if std::env::args().any(|x| x == "--time") { + bench(func, input, &base_time) + } else { + (base_time, 1) + }; + + (result, run.0, run.1) +} + +fn bench(func: impl Fn(I) -> T, input: I, base_time: &Duration) -> (Duration, u128) { + let mut stdout = stdout(); + + print!(" > {ANSI_ITALIC}benching{ANSI_RESET}"); + let _ = stdout.flush(); + + let bench_iterations = + (Duration::from_secs(1).as_nanos() / cmp::max(base_time.as_nanos(), 10)).clamp(10, 10000); + + let mut timers: Vec = vec![]; + + for _ in 0..bench_iterations { + let timer = Instant::now(); + black_box(func(black_box(input))); + timers.push(timer.elapsed()); + } + + ( + #[allow(clippy::cast_possible_truncation)] + Duration::from_nanos(average_duration(&timers) as u64), + bench_iterations, + ) +} + +fn average_duration(numbers: &[Duration]) -> u128 { + numbers + .iter() + .map(std::time::Duration::as_nanos) + .sum::() + / numbers.len() as u128 +} + +fn format_duration(duration: &Duration, samples: u128) -> String { + if samples == 1 { + format!(" ({duration:.1?})") + } else { + format!(" ({duration:.1?} @ {samples} samples)") + } +} + +fn print_result(result: &Option, part: &str, duration_str: &str) { + let is_intermediate_result = duration_str.is_empty(); + + match result { + Some(result) => { + if result.to_string().contains('\n') { + let str = format!("{part}: β–Ό {duration_str}"); + if is_intermediate_result { + print!("{str}"); + } else { + print!("\r"); + println!("{str}"); + println!("{result}"); + } + } else { + let str = format!("{part}: {ANSI_BOLD}{result}{ANSI_RESET}{duration_str}"); + if is_intermediate_result { + print!("{str}"); + } else { + print!("\r"); + println!("{str}"); + } + } + } + None => { + if is_intermediate_result { + print!("{part}: βœ–"); + } else { + print!("\r"); + println!("{part}: βœ– "); + } + } + } +} + +/// Parse the arguments passed to `solve` and try to submit one part of the solution if: +/// 1. we are in `--release` mode. +/// 2. aoc-cli is installed. +fn submit_result( + result: T, + day: Day, + part: u8, +) -> Option> { + let args: Vec = env::args().collect(); + + if !args.contains(&"--submit".into()) { + return None; + } + + if args.len() < 3 { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + } + + let part_index = args.iter().position(|x| x == "--submit").unwrap() + 1; + + let Ok(part_submit) = args[part_index].parse::() else { + eprintln!("Unexpected command-line input. Format: cargo solve 1 --submit 1"); + process::exit(1); + }; + + if part_submit != part { + return None; + } + + if aoc_cli::check().is_err() { + eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it."); + process::exit(1); + } + + println!("Submitting result via aoc-cli..."); + Some(aoc_cli::submit(day, part, &result.to_string())) +} diff --git a/src/template/timings.rs b/src/template/timings.rs new file mode 100644 index 0000000..fb79835 --- /dev/null +++ b/src/template/timings.rs @@ -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, + pub part_2: Option, + 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, +} + +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 = 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::() / 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 for JsonValue { + fn from(value: Timings) -> Self { + let mut map: HashMap = HashMap::new(); + + map.insert( + "data".into(), + JsonValue::Array(value.data.iter().map(JsonValue::from).collect()), + ); + + JsonValue::Object(map) + } +} + +impl TryFrom for Timings { + type Error = String; + + fn try_from(value: String) -> Result { + let json = JsonValue::from_str(&value).or(Err("not valid JSON file."))?; + + let json_data = json + .get::>() + .ok_or("expected JSON document to be an object.")? + .get("data") + .ok_or("expected JSON document to have key `data`.")? + .get::>() + .ok_or("expected `json.data` to be an array.")?; + + Ok(Timings { + data: json_data + .iter() + .map(Timing::try_from) + .collect::>()?, + }) + } +} + +/* -------------------------------------------------------------------------- */ + +impl From<&Timing> for JsonValue { + fn from(value: &Timing) -> Self { + let mut map: HashMap = 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 { + let json = value + .get::>() + .ok_or("Expected timing to be a JSON object.")?; + + let day = json + .get("day") + .and_then(|v| v.get::()) + .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::() }) + .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::() }) + .ok_or("Expected timing.part_2 to be null or string.")?; + + let total_nanos = json + .get("total_nanos") + .and_then(|v| v.get::().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::>() + .unwrap() + .get("data") + .unwrap() + .get::>() + .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); + } + } +}