Solutions for Advent of Code in Rust.
Day | Part 1 | Part 2 |
---|---|---|
Day 1 | β | β |
Day 2 | β | β |
Day 3 | β | β |
Day 4 | β | β |
Day 5 | β | β |
Day 6 | β | β |
Day 7 | β | β |
Day 8 | β | β |
Day 9 | β | β |
Day 10 | β | β |
Day 11 | β | β |
Day 12 | β | β |
Day 13 | β | β |
Day 14 | β | β |
Day 15 | β | β |
Day 16 | β | β |
Day 17 | β | β |
Day 18 | β | β |
Day | Part 1 | Part 2 |
---|---|---|
Day 11 | 96.0Β΅s |
95.8Β΅s |
Day 12 | 358.8Β΅s |
770.0Β΅s |
Day 13 | 412.9Β΅s |
894.6Β΅s |
Day 14 | 39.9Β΅s |
26.5ms |
Day 15 | 50.9Β΅s |
1.2ms |
Day 16 | 73.4Β΅s |
8.7ms |
Day 17 | 24.6ms |
89.6ms |
Day 18 | 43.9Β΅s |
58.5Β΅s |
Day 19 | 1.2ms |
964.4Β΅s |
Day 20 | 2.6ms |
10.4ms |
Day 21 | 140.5ms |
142.3ms |
Day 22 | 174.0ms |
187.1ms |
Day 23 | 1.0ms |
1.6ms |
Day 24 | 386.0Β΅s |
83.6Β΅s |
Day 25 | 4.1ms |
- |
Total: 819.73ms
# example: `cargo scaffold 1`
cargo scaffold <day>
# 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.
Individual solutions live in the ./src/bin/
directory as separate binaries. Inputs and examples live in the the ./data
directory.
Every solution has tests referencing its example file in ./data/examples
. Use these tests to develop and debug your solutions against the example input. In VS Code, rust-analyzer
will display buttons for running / debugging these unit tests above the unit test blocks.
Tip
If a day has different example inputs for both parts, you can use the read_file_part()
helper in your tests instead of read_file()
. For example, if this applies to day 1, you can create a second example file 01-2.txt
and invoke the helper like let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));
to read it in test_part_two
.
Important
This requires installing the aoc-cli crate.
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:
# example: `cargo download 1`
cargo download <day>
# output:
# [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".
# example: `cargo solve 01`
cargo solve <day>
# output:
# Finished dev [unoptimized + debuginfo] target(s) in 0.13s
# Running `target/debug/01`
# Part 1: 42 (166.0ns)
# Part 2: 42 (41.0ns)
The solve
command runs your solution against real puzzle inputs. To run an optimized build of your code, append the --release
flag as with any other rust program.
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.
Important
This requires installing the aoc-cli crate.
In order to submit part of a solution for checking, append the --submit <part>
option to the solve
command.
cargo all
# output:
# Running `target/release/advent_of_code`
# ----------
# | Day 01 |
# ----------
# Part 1: 42 (19.0ns)
# Part 2: 42 (19.0ns)
# <...other days...>
# Total: 0.20ms
This runs all solutions sequentially and prints output to the command-line. Same as for the solve
command, the --release
flag runs an optimized build and the --time
flag outputs benchmarks.
The template can write benchmark times to the README via the cargo time
command.
By default, this command checks for missing benchmarks, runs those solutions, and then updates the table. If you want to (re-)time all solutions, run cargo time --all
. If you want to (re-)time one specific solution, run cargo time <day>
.
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.
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
.
cargo fmt
cargo clippy
Important
This command requires installing the aoc-cli crate.
# example: `cargo read 1`
cargo read <day>
# output:
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
# ...the input...
Important
This command requires installing the aoc-cli crate.
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.
# 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...
- Install
aoc-cli
via cargo:cargo install aoc-cli --version 0.12.0
- 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 thesession
cookie value. 1
Once installed, you can use the download command, the read command, and automatically submit solutions via the --submit
flag.
This template includes a Github action that automatically updates the readme with your advent of code progress.
To enable it, complete the following steps:
Go to the leaderboard page of the year you want to track and click Private Leaderboard. If you have not created a leaderboard yet, create one by clicking Create It. Your leaderboard should be accessible under https://adventofcode.com/{year}/leaderboard/private/view/{aoc_user_id}
.
Go to the Secrets tab in your repository settings and create the following secrets:
AOC_USER_ID
: Go to this page 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 session2 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 thesession
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 totrue
to enable the progress tracker. After you complete AoC or no longer work on it, you can set this tofalse
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.
Uncomment the respective sections in the ci.yml
workflow.
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 integration to analyze it. In order to activate DHAT, call the solve
command with the --dhat
flag.
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 to view a detailed breakdown of heap allocations.
- Install rust-analyzer and CodeLLDB.
- Set breakpoints in your code. 3
- Click Debug next to the unit test or the main function. 4
- The debugger will halt your program at the specific line and allow you to inspect the local stack. 5
- itertools: Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles.
- regex: Official regular expressions implementation for Rust.
A curated list of popular crates can be found on blessred.rs.
Do you have aoc-specific crate recommendations? Share them!
- 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 inrelease
mode, leading to wrong output when running your solution.
Footnotes
-
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. β© -
The session cookie might expire after a while (~1 month) which causes the automated workflow to fail. To fix this issue, refresh the AOC_SESSION secret. β©
- β©
- β©
- β©