This repository contains my solutions for Advent of Code, structured as a single Cargo project.
You can run any solution by passing the year, day, and an optional mode (test or final/input):
cargo run -- <year> <day> [mode]<year>: The 4-digit year (e.g.,2015,2024)<day>: The day number (e.g.,1for day 1,25for day 25)[mode]: (Optional) The name of the input file to run against, without the.txtextension. Defaults totest. Common modes aretestandfinal(orinput).
Examples:
# Runs year 2015, day 1 using test.txt
cargo run -- 2015 1
# Runs year 2015, day 1 using test.txt
cargo run -- 2015 1 test
# Runs year 2015, day 1 using final.txt (or input.txt)
cargo run -- 2015 1 final To add a solution for a new day in a year that already exists (e.g., Day 12 of 2015):
- Create the Day Directory: Navigate to the specific year's folder (
src/aoc_YYYY) and create a new folder nameddayXX(padded with a leading zero, e.g.,day12). - Create Required Files: Inside the
dayXXfolder, create:mod.rstest.txt(for example inputs)final.txtorinput.txt(for the real puzzle input)
- Implement the Solution: In
dayXX/mod.rs, write your solution logic. It must expose arun(file_path: &str)function:use std::fs; pub fn run(file_path: &str) { let contents = fs::read_to_string(file_path).expect("File not found"); // Add your solution logic here println!("Part 1 Answer:"); println!("Part 2 Answer:"); }
- Register the Day: Open the year's module file (e.g.,
src/aoc_YYYY/mod.rs) and do two things:- Declare the module at the top:
pub mod dayXX; - Add it to the
runmethod's match statement:(Make sure to match the padded string, e.g.,"XX" => dayXX::run(path),
"12","09")
- Declare the module at the top:
To add the first solution for an entirely new year (e.g., 2025):
- Create the Year Directory: Under
src/, create a new directory namedaoc_YYYY(e.g.,aoc_2025). - Create the Year Module: Give this directory a
mod.rsfile (src/aoc_YYYY/mod.rs). Set it up to route days:pub mod day01; pub fn run(day: &str, path: &str) { match day { "01" => day01::run(path), _ => println!("waiting for the solution"), } }
- Create the First Day: Inside the new
aoc_YYYYdirectory, create aday01folder and follow the steps from "Adding a New Solution for an Existing Year" to addmod.rs,test.txt, etc. - Register the Year: Open
src/main.rs:- Declare the new year module at the top of the file:
mod aoc_YYYY; - Update the
match year.as_str()block inside themainfunction to route to your new year:"YYYY" => aoc_YYYY::run(&day_padded, &file_path),
- Declare the new year module at the top of the file: