🎄Solutions for Advent of Code 2023

Overview

🎄 Advent of Code 2023

Solutions for Advent of Code in Rust.

2023 Results

Day Part 1 Part 2
Day 1
Day 2
Day 3
Day 4
Day 5
Day 6
Day 7
Day 8
Day 9
Day 10

Benchmarks

Day Part 1 Part 2
Day 1 33.4µs 156.3µs
Day 2 32.6µs 29.7µs
Day 3 152.8µs 252.0µs
Day 4 124.3µs 117.5µs
Day 5 21.5µs 1.5s
Day 6 240.0ns 259.0ns
Day 7 280.0µs 272.8µs
Day 8 466.0µs 1.4ms
Day 9 210.2µs 202.7µs
Day 10 578.6µs 2.5ms

Total: 1506.83ms


Template readme

Template setup

This template supports all major OS (macOS, Linux, Windows).

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

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

Scaffold a day

# 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 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 different example inputs for both parts, you can use the read_file_part() helper in your tests instead of read_file(). For example, if this 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)); to read it in part_two_example.

Download input & description for a day

[!IMPORTANT] This requires installing the aoc-cli crate.

You can automatically download puzzle inputs 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".

Run solutions for a 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 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.

Submitting solutions

[!IMPORTANT] This 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.

Run all solutions

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, the --release flag runs an optimized build.

Update readme benchmarks

The template can output a table with solution times to your readme. In order to generate a benchmarking table, run cargo time. By default, this command checks for missing benchmarks, runs those solutions, and updates the table. If you want to (re-)time all solutions, run cargo time --force flag. If you want to (re-)time a specific solution, run cargo time <day>.

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.

Run all tests

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.

Format code

cargo fmt

Lint code

cargo clippy

Read puzzle description in terminal

[!IMPORTANT] 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...

Optional template features

Configure aoc-cli integration

  1. Install aoc-cli via cargo: cargo install aoc-cli --version 0.12.0
  2. 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 the session cookie value. [^1]

Once installed, you can use the download command, the read command, and automatically submit solutions via the --submit flag.

Automatically track ⭐️ progress in the readme

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 a 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_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[^2] 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.

Go to the Variables tab in your repository settings and create the following variable:

  • AOC_ENABLED: This variable controls whether the workflow is enabled. Set it to true to enable the progress tracker. After you complete AoC or no longer work on it, you can set this to false to disable the CI.

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

Check code formatting / clippy lints in CI

Uncomment the respective sections in the ci.yml workflow.

Use VS Code to debug your code

  1. Install rust-analyzer and CodeLLDB.
  2. Set breakpoints in your code. [^3]
  3. Click Debug next to the unit test or the main function. [^4]
  4. The debugger will halt your program at the specific line and allow you to inspect the local stack. [^5]

Useful crates

  • 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!

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.
You might also like...
My solutions for the Advent of Code 2021 in Scala, Python, Haskell and Rust.

Advent of Code 2021 These are my Advent of Code 2021 solutions written in Scala 3, Haskell, Python and Rust. Day Title L1 L2 L3 L4 01 Sonar Sweep Scal

My solutions for Advent of Code 2021, in Rust

Advent of Code 2021 These are my solutions. I have decided to use Rust for now. I'm new to Rust, so it might be some of the worst Rust code you've see

My solutions for the 2021 edition of the Advent of Code, using Rust and SOM (Simple Object Machine)

Advent of Code 2021 These are my solutions for the 2021 edition of the Advent of Code. The solutions are all implemented using both Rust and SOM (Simp

Remmy's Advent of Code solutions

Advent of Code in Rust - remmycat's solutions Benchmarks on MacBook Pro (13-inch, 2019), 2.8 GHz Quad-Core Intel Core i7 (Day 15 and 19, yikes…) Emoji

Solutions to Advent of Code 2021, coded in rust

Advent of Code 2021 (aoc-2021-rust) Solutions to Advent of Code 2021 (https://adventofcode.com/2021), coded as part of my efforts to learn Rust Run co

🎄 My solutions to Advent of Code 2022.

🎄 advent-of-code-2022 Overview This repository contains my solutions to Advent of Code 2022. I decided to take this year as an opportunity to learn R

My Rusty Solutions For Advent of Code

My Rusty Solutions For Advent of Code Name Part 1 Part 2 Language Day 1 Snack Calorie Counting ✅ ✅ Rust Day 2 Rock Paper Scissors ✅ ✅ Rust Day 3 Food

Solutions to Advent of Code 2022 puzzles

Solutions to Advent of Code 2022 puzzles

Advent of Code 2022 Solutions (in Rust)

Advent of Code 2022 My solutions for Advent of Code 2022, written in Rust. This repository provides a good template for anyone interested in writing t

Owner
Felix Spöttel
Felix Spöttel
Solutions of Advent-of-Code 2023 in Rust

Advent of Code 2023 in Rust If you've never heard of Advent of Code, you're missing out. Check it out now at adventofcode.com! I mostly do AoC puzzles

Aurélien Geron 4 Dec 8, 2023
Solutions to Advent of Code 2023 in Rust 🎄

Advent of Code 2023 Solutions to Advent of Code 2023 in Rust ?? Day Part 1 Part 2 Thoughts 1 ⭐ ⭐ As with most early advent of code challenges, it's mo

Pedro Godinho 6 Dec 6, 2023
🎄🎅 My Advent of Code 2023 solutions 🎁⛄

???? My AOC 2023 solutions ??⛄ ?? Table of contents ?? Table of the days ?? Goals ⛄ Quickstart ?? Table of the days Day Part 1 Part 2 Language 01 Toki

rei 6 Dec 11, 2023
Solutions to Advent of Code 2023 in Rust! :crab:

AdventOfCode2023 ?? Solutions to AoC 2023 in Rust ?? Usage # to be able to download inputs (see fetch.sh) export AOC_SESSION=[value from session cooki

Axel Lindeberg 27 Dec 17, 2023
Advent of Code 2023 solutions - #15 on global leaderboard

My solutions for Advent of Code 2023, written in Rust. The code requires Rust nightly to run. Sample inputs from each day are provided. To run with ac

Daniel Huang 28 Dec 17, 2023
My Advent of Code 2023 solutions in the Rust programming language.

Advent of Code 2023 in Rust My Advent of Code 2023 solutions in the Rust programming language. This repository holds a separate Rust project for each

Tim Visée 65 Dec 17, 2023
Advent of Code 2021 puzzles & solutions in Rust

Advent of Code 2021 These are puzzles for the Advent of Code 2021 challenge, written and solved in the Rust programming language. The puzzle for each

Chevy Ray Johnston 8 Dec 19, 2021
Repository with my Advent of Code 2021 puzzle solutions 🎄

?? Advent of Code 2021 ?? I decided to stick with Rust this year and try to improve a bit on it, I basically haven't used it since last year's AoC, so

fratorgano 2 Dec 1, 2022
My solutions to Advent of Code 2021 (mostly in rust)

Advent of Code 2021 Small code to solve problems at https://adventofcode.com/2021. Most of the code are written in Rust. How to run solutions For exam

“Plane” Abhabongse  Janthong 3 Apr 22, 2022
🎄My Advent of Code 2021 solutions in the Rust programming language

Advent of Code 2021 in Rust My Advent of Code 2021 solutions in the Rust programming language. This repository holds a separate Rust project for each

Tim Visée 227 Dec 16, 2022