Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tyrone-wu/advent-of-code-2023-rust
https://github.com/tyrone-wu/advent-of-code-2023-rust
advent-of-code-2023 rust
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/tyrone-wu/advent-of-code-2023-rust
- Owner: tyrone-wu
- License: mit
- Created: 2023-12-03T05:19:12.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-12-31T06:01:38.000Z (11 months ago)
- Last Synced: 2024-01-01T00:37:07.132Z (11 months ago)
- Topics: advent-of-code-2023, rust
- Language: Rust
- Homepage:
- Size: 1.99 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🎄 Advent of Code 2023
Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/).
---
## Template setup
This template supports all major OS (macOS, Linux, Windows).
### Create your repository 📝
1. Open [the template repository](https://github.com/fspoettel/advent-of-code-rust) on Github.
2. Click [Use this template](https://github.com/fspoettel/advent-of-code-rust/generate) 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](https://www.rust-lang.org/tools/install).
2. (recommended) Install the [rust-analyzer](https://rust-analyzer.github.io/manual.html) extension for your code editor.
3. (optional) Install a native debugger. If you are using VS Code, [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb) is a good option.---
✨ You can start solving puzzles now! Head to the [Usage section](#usage) to see how to use this template. If you like, you can configure [some optional features](#optional-template-features).
## Usage
### Scaffold a day
```sh
# example: `cargo scaffold 1`
cargo scaffold# 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](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template/commands/scaffold.rs#L9-L35) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input.
> [!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 `test_part_two`.> [!TIP]
> when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks.### Download input & description for a day
> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).```sh
# example: `cargo download 1`
cargo download# 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
```sh
# example: `cargo solve 01`
cargo solve# 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 command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).In order to submit part of a solution for checking, append the `--submit ` option to the `solve` command.
### Run all solutions
```sh
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 all --release --time`. If everything goes well, the command will output "_Successfully updated README with benchmarks._" after the execution finishes and the readme will be updated.
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
```sh
cargo test
```To run tests for a specific day, append `--bin `, 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
```sh
cargo fmt
```### Lint code
```sh
cargo clippy
```### Read puzzle description in terminal
> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).```sh
# example: `cargo read 1`
cargo read# output:
# Loaded session cookie from "/Users//.adventofcode.session".
# Fetching puzzle for day 1, 2022...
# ...the input...
```## Optional template features
### Configure aoc-cli integration
1. Install [`aoc-cli`](https://github.com/scarvalhojr/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](#download-input--description-for-a-day), the read command, and automatically submit solutions via the [`--submit` flag](#submitting-solutions).
### Automatically track ⭐️ progress in the readme
This template includes [a Github action](https://github.com/k2bd/advent-readme-stars) 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](https://adventofcode.com/settings) 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](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) and [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb).
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](https://crates.io/crates/itertools): Extends iterators with extra methods and adaptors. Frequently useful for aoc puzzles.
- [regex](https://crates.io/crates/regex): Official regular expressions implementation for Rust.A curated list of popular crates can be found on [blessred.rs](https://blessed.rs/crates).
Do you have aoc-specific crate recommendations? [Share them!](https://github.com/fspoettel/advent-of-code-rust/edit/main/README.md)
## 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](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-overflow) in `release` mode, leading to wrong output when running your solution.
## 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.
[^2]: 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.
[^3]:
[^4]:
[^5]: