advent of code 2022 in rust
Go to file
Felix Spöttel afa39070de fix: better error messages in download script 2022-10-17 15:18:32 +02:00
.assets chore: mv `assets` => `.assets` 2021-12-30 11:14:53 +01:00
.cargo feat: implement bash scripts in rust 2022-10-17 14:43:04 +02:00
.github/workflows feat: implement bash scripts in rust 2022-10-17 14:43:04 +02:00
.vscode initial commit 2021-12-29 14:12:01 +01:00
src fix: better error messages in download script 2022-10-17 15:18:32 +02:00
.editorconfig initial commit 2021-12-29 14:12:01 +01:00
.gitignore feat: use `cargo --bin` instead of solutions crate 2021-12-29 19:45:58 +01:00
Cargo.lock feat: implement bash scripts in rust 2022-10-17 14:43:04 +02:00
Cargo.toml feat: implement bash scripts in rust 2022-10-17 14:43:04 +02:00
LICENSE initial commit 2021-12-29 14:12:01 +01:00
README.md docs: update setup docs 2022-10-17 15:12:59 +02:00

README.md

🎄 Advent of Code

Cross-platform template for solving Advent of Code in Rust.

Setup

Create your repository

  1. Open the template repository on Github.
  2. Click Use this template and create your repository.
  3. Clone your repository to your computer.

Setup rust

  1. Install the Rust toolchain.
  2. (recommended) Install the rust-analyzer extension for your code editor.
  3. (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.

Usage

Setup new day

# 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 run --bin 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 example inputs. 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 these actions above the unit tests.

Download inputs for a day

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 flag. (example: cargo download 1 --year 2020)

Puzzle inputs are not checked into git. Reasoning.

Run solutions for a day

# example: `cargo run --bin 01`
cargo run --bin <day>

# output:
#     Running `target/debug/01`
# 🎄 Part 1 🎄
#
# 6 (elapsed: 37.03µs)
#
# 🎄 Part 2 🎄
#
# 9 (elapsed: 33.18µs)

To run an optimized version for benchmarking, use the --release flag or the alias cargo rr --bin <day>.

Displayed timings show the raw execution time of your solution without overhead (e.g. file reads).

Run solutions for all days

cargo run

# output:
#     Running `target/release/aoc`
# ----------
# | Day 01 |
# ----------
# 🎄 Part 1 🎄
#
# 0 (elapsed: 170.00µs)
#
# 🎄 Part 2 🎄
#
# 0 (elapsed: 30.00µs)
# <...other days...>
# Total: 0.20ms

To run an optimized version for benchmarking, use the --release flag or the alias cargo rr.

Total timing is computed from individual solution timings and excludes as much overhead as possible.

Run all solutions against example input

cargo test

Format code

cargo fmt

Lint code

cargo clippy

Optional template features

Download inputs via aoc-cli

  1. Install aoc-cli via cargo: cargo install aoc-cli.
  2. Create an .adventofcode.session file in your home directory and paste your session cookie 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 the session cookie value.

Once installed, you can use the download command.

Note
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.

CI: enable clippy lints

Uncomment the clippy job in the ci.yml workflow to enable clippy checks in CI.

Readme progress tracker

This template includes a Github action that automatically updates the readme with your advent of code progress.

To enable it, complete the following steps:

1. Create private leaderboard

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}.

2. Set repository secrets

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 to true 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 session 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 the session cookie.

Note
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.

Useful crates

  • itertools: Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles.
  • hashbrown: Faster hashing functions for HashMap and HashSet.

Do you have aoc-specific crate recommendations? Share them!