meck / AoC2022-rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Advent of Code 2022

Solutions for Advent of Code in Rust.

Usage

Get a dev shell

# Get rust environment via nix
nix develop

Scaffold a 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 solve 01` to run your solution.

Individual solutions live in the ./src/bin/ directory as separate binaries.

Download input for a day

# 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)

Run solutions for a day

# 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).

Run all solutions

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.

Run all solutions against the example input

cargo test

Format code

cargo fmt

Lint code

cargo clippy

Optional template features

Download puzzle inputs via aoc-cli

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

In a dev nix shell, you can use the download command.

Common pitfalls

  • 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 in release mode, leading to wrong output when running your solution.

Footnotes

Footnotes

  1. 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. ↩

About

License:MIT License


Languages

Language:Rust 92.7%Language:Nix 7.3%