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 19 | β | β |
Day 20 | β | β |
This template supports all major OS (macOS, Linux, Windows).
- Open the template repository on Github.
- Click Use this template and create your repository.
- Clone your repository to your computer.
- Install the Rust toolchain.
- (recommended) Install the rust-analyzer extension for your code editor.
- (optional) Install a native debugger. If you are using VS Code, CodeLLDB is a good option.
β¨ You can start solving puzzles now! Head to the Usage section to see how to use this template. If you like, you can configure some optional features.
# example: `cargo scaffold 1`
cargo scaffold <day>
# output:
# Created module "src/bin/01.rs"
# Created empty input file "src/inputs/01.txt"
# Created empty example file "src/examples/01.txt"
# ---
# π Type `cargo solve 01` to run your solution.
Individual solutions live in the ./src/bin/
directory as separate binaries.
Every solution 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.
When editing a solution, rust-analyzer
will display buttons for running / debugging unit tests above the unit test blocks.
Note
This command requires installing the aoc-cli crate.
# example: `cargo download 1`
cargo download <day>
# 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!
# ---
# π Successfully wrote input to "src/inputs/01.txt"!
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.
# example: `cargo solve 01`
cargo solve <day>
# output:
# Running `target/debug/01`
# π Part 1 π
#
# 6 (elapsed: 37.03Β΅s)
#
# π Part 2 π
#
# 9 (elapsed: 33.18Β΅s)
solve
is an alias for cargo run --bin
. To run an optimized version for benchmarking, append the --release
flag.
Displayed timings show the raw execution time of your solution without overhead (e.g. file reads).
cargo all
# output:
# Running `target/release/advent_of_code`
# ----------
# | Day 01 |
# ----------
# π Part 1 π
#
# 0 (elapsed: 170.00Β΅s)
#
# π Part 2 π
#
# 0 (elapsed: 30.00Β΅s)
# <...other days...>
# Total: 0.20ms
all
is an alias for cargo run
. To run an optimized version for benchmarking, use the --release
flag.
Total timing is computed from individual solution timings and excludes as much overhead as possible.
cargo test
cargo fmt
cargo clippy
- Install
aoc-cli
via cargo:cargo install aoc-cli --version 0.5.0
. - Create an
.adventofcode.session
file in your home directory and paste your session cookie1 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 thesession
cookie value.
Once installed, you can use the download command.
Uncomment the clippy
job in the ci.yml
workflow to enable clippy checks in CI.
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_ENABLED
: This variable controls whether the workflow is enabled. Set it totrue
to enable the progress tracker.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.
- 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. β©
- β©
- β©
- β©