Solutions for Advent of Code in Rust.
| Year | Stars |
|---|---|
| 2025 | 24/24 ⭐ |
| 2024 | 04/50 ⭐ |
| 2015 | 14/50 ⭐ |
- 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 <year> <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 2025 01` to run your solution.Individual solutions live in the ./src/year<year>/day<day>.rs. 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 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", YEAR, DAY, 2));. This supports an arbitrary number of example files.
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 <year> <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 <year> <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.
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.20msThis runs all solutions sequentially and prints output to the command-line. Same as for the solve command, the --release flag runs an optimized build.
cargo testImportant
This command requires installing the aoc-cli crate.
# example: `cargo read 2025 01`
cargo read <year> <day>
# output:
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
# ...the input...cargo fmtcargo clippy- Install
aoc-clivia cargo:cargo install aoc-cli --version 0.12.0 - Create the file
<home_directory>/.adventofcode.sessionand 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 thesessioncookie value. [^1]
Once installed, you can use the download command, and the read command.
- 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 blessed.rs.
