Solutions for Advent of Code in Rust.
This section provides some guides on how to stand up your development environment.
This project is written in Rust and requires that it be installed prior to development. The recommended method is to use rustup, the official Rust toolchain installer. Alternatively, you can use a version manager like mise-en-place, or a package manager like brew or apt.
To scaffold a new day's solution, run the following command:
# 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.This creates a solution file from the template along with empty data files for inputs and examples.
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.
Important
This requires installing the aoc-cli crate via
cargo install aoc-cli --version 0.12.0. You will also need to create the file
<home_directory>/.adventofcode.session and paste your session cookie into it. To retrieve the session cookie, press
F12 anywhere on the Advent of Code website to open your browser developer tools. Look in Cookies under the
Application or Storage tab, and copy out the session cookie value.
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".Individual solutions live in the ./src/bin/ directory as separate binaries. Inputs and examples live in 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.
To run a solution for a specific day:
# 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, append the --release
flag as with any other rust program.
To run all solutions sequentially:
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.20msTo format the code:
cargo fmtTo lint the code:
cargo clippyTo run tests for the application, use the following command:
cargo testTo 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.
To benchmark your solutions:
# example: `cargo time 8 --store`
cargo time <day> [--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 the execution time of the first run, and print the
average execution time.
cargo time has three modes of execution:
cargo timewithout arguments incrementally benches solutions that have not been stored in the readme yet and skips the rest.cargo time <day>benches a single solution.cargo time --allbenches all solutions.
By default, cargo time does not write to the readme. In order to do so, append the --store flag:
cargo time --store.
Note
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.
| Day | Part 1 | Part 2 |
|---|---|---|
| Day 1 | ⭐ | ⭐ |
| Day 2 | ⭐ | ⭐ |
| Day 3 | ⭐ | ⭐ |
| Day 4 | ⭐ | ⭐ |
| Day 5 | ⭐ | ⭐ |
| Day 6 | ⭐ | ⭐ |
| Day 7 | ⭐ | ⭐ |
| Day 8 | ⭐ | ⭐ |
| Day | Part 1 | Part 2 |
|---|---|---|
| Day 1 | 54.8µs |
38.0µs |
| Day 2 | 27.5ms |
117.3ms |
| Day 3 | 34.8µs |
58.8µs |
| Day 4 | 123.5µs |
4.0ms |
| Day 5 | 90.8µs |
28.2µs |
| Day 6 | 146.8µs |
69.3µs |
| Day 7 | 155.3µs |
195.2µs |
| Day 8 | 27.3ms |
120.9ms |
Total: 298.00ms
