Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🎄Starter template for solving Advent of Code in Rust.

License

NotificationsYou must be signed in to change notification settings

Rettend/advent-of-code-rust

Repository files navigation

🎄 Advent of Code {year}

Solutions forAdvent of Code inRust.


Template setup

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

Create your repository 📝

  1. Openthe template repository on Github.
  2. ClickUse this template and create your repository.
  3. Clone your repository to your computer.

Setup rust 💻

  1. Install theRust toolchain.
  2. (recommended) Install therust-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 theUsage section to see how to use this template. If you like, you can configuresome optional features.

Usage

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.

Everysolution hasunit tests referencing itsexample file. Use these unit tests to develop and debug your solution against the example input. 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 running / debugging unit tests above the unit test blocks.

Download input & description for a day

Note
This command requiresinstalling the aoc-cli crate.

# example: `cargo download 1`cargo download<day># output:# Loaded session cookie from "/Users/<snip>/.adventofcode.session".# Fetching puzzle for day 1, 2022...# Saving puzzle description to "src/puzzles/01.md"...# Downloading input for day 1, 2022...# Saving puzzle input to "src/inputs/01.txt"...# Done!# ---# 🎄 Successfully wrote input to "src/inputs/01.txt".# 🎄 Successfully wrote puzzle to "src/puzzles/01.md".

To download inputs for previous years, append the--year/-y flag.(example:cargo download 1 --year 2020)

Puzzle descriptions are stored insrc/puzzles as markdown files. Puzzle inputs are not checked into git.Reasoning.

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 forcargo run --bin. To run an optimized version for benchmarking, append the--release flag.

Displayedtimings 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 forcargo run. To run an optimized version for benchmarking, use the--release flag.

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

Run all solutions against the example input

cargotest

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

Note
This command requiresinstalling the aoc-cli crate.

# example: `cargo read 1`cargoread<day># output:# Loaded session cookie from "/Users/<snip>/.adventofcode.session".# Fetching puzzle for day 1, 2022...# ...the input...

To read inputs for previous years, append the--year/-y flag.(example:cargo read 1 --year 2020)

Optional template features

Download puzzle inputs via aoc-cli

  1. Installaoc-cli via cargo:cargo install aoc-cli --version 0.7.0
  2. 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 thesession cookie value.

Once installed, you can use thedownload command.

Check code formatting in CI

Uncomment theformat job in theci.yml workflow to enable fmt checks in CI.

Enable clippy lints in CI

Uncomment theclippy job in theci.yml workflow to enable clippy checks in CI.

Automatically track ⭐️ progress in the readme

This template includesa 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 clickPrivate Leaderboard. If you have not created a leaderboard yet, create one by clickingCreate It. Your leaderboard should be accessible underhttps://adventofcode.com/{year}/leaderboard/private/view/{aoc_user_id}.

2. Set repository secrets

Go to theSecrets tab in your repository settings and create the following secrets:

  • AOC_ENABLED: This variable controls whether the workflow is enabled. Set it totrue to enable the progress tracker.
  • AOC_USER_ID: Go tothis 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 session2 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 thesession cookie.

✨ You can now run this action manually via theRun workflow button on the workflow page. If you want the workflow to run automatically, uncomment theschedule section in thereadme-stars.yml workflow file or add apush trigger.

Use VS Code to debug your code

  1. Installrust-analyzer andCodeLLDB.
  2. Set breakpoints in your code.3
  3. ClickDebug next to the unit test or themain 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 onblessred.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 indebug mode, integerswrap inrelease 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.

  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. Set a breakpoint
  4. Run debugger
  5. Inspect debugger state

About

🎄Starter template for solving Advent of Code in Rust.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages

  • Rust100.0%

[8]ページ先頭

©2009-2025 Movatter.jp