Solutions for Advent of Code in Rust.
This is the first year I am solving Advent of Code in Rust. My solutions might not be the most idiomatic, but I am trying to learn as much as possible along the way. Having written a lot of Clojure and TypeScript I tend to rely on iterators and functional programming patterns.
Note that per the template I am using and the wishes of the Advent of Code author I do not include my puzzle inputs in this repository. You can find the puzzle inputs for each day on the Advent of Code website, or you can use the aoc-cli as described below.
Day | Part 1 | Part 2 |
---|---|---|
Day 1 | 179.1Β΅s |
564.4Β΅s |
Day 2 | 72.1Β΅s |
71.8Β΅s |
Day 3 | 684.0Β΅s |
630.5Β΅s |
Day 4 | 190.6Β΅s |
180.7Β΅s |
Day 5 | 24.5Β΅s |
2.0s |
Day 6 | 329.0ns |
17.5ms |
Day 7 | 531.9Β΅s |
9.5ms |
Day 8 | 810.1Β΅s |
790.2Β΅s |
Day 9 | 268.9Β΅s |
265.0Β΅s |
Total: 2032.26ms
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.
- 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.
- 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 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 unit tests referencing its example file. Use these unit tests to develop and debug your solutions against the example input.
Tip: 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:
# [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.
Note This command 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, --release
controls whether real inputs will be used.
The template can output a table with solution times to your readme. Please note that these are not "scientific" benchmarks, understand them as a fun approximation. π
In order to generate a benchmarking table, run cargo all --release --time
. If everything goes well, the command will output "Successfully updated README with benchmarks." after the execution finishes.
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
Note 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...
- 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 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_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.
β¨ 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.
- 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. β©
- β©
- β©
- β©