From 5433430628b23d4996657e9d6c77a0d0ee75f4b7 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Wed, 3 Dec 2025 01:22:09 -0500 Subject: [PATCH] Macro-ify benches and unify days list to one file Also update README with new options. --- README.md | 8 ++++++-- benches/aoc.rs | 31 +++++++++++++++++++------------ src/days.rs | 12 ++++++++++++ src/lib.rs | 1 + src/main.rs | 9 +++------ 5 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 src/days.rs diff --git a/README.md b/README.md index 614d3f3..e36c3b8 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,17 @@ Rusty and over-engineered edition. By request of AoC creator, I haven't included the input files (e.g. src/input/day01.txt). Log into the Advent of Code site and save the inputs there to the src/input/ folder. -Then to run: `cargo run`. +To run all days: `cargo run`. + +To run a specific day and/or part: `cargo run -- --day 1 --part 1`. To run in super-fast prod mode: `cargo run --release`. To run with debug logs enabled: `RUST_LOG=debug cargo run`. -To run the tests against included test input files: `RUST_LOG=debug cargo test -- --no-capture`. +To run all the tests against included test input files: `RUST_LOG=debug cargo test -- --no-capture`. + +To run the tests for a specific day and/or part: `RUST_LOG=debug cargo test day01::test::test_part1 -- --no-capture`. ## Benchmarks diff --git a/benches/aoc.rs b/benches/aoc.rs index 8eefa4b..e2fa851 100644 --- a/benches/aoc.rs +++ b/benches/aoc.rs @@ -1,16 +1,23 @@ -use aoc::day01; -use aoc::day02; use criterion::{Criterion, criterion_group, criterion_main}; -fn day01_benchmark(c: &mut Criterion) { - c.bench_function("day01 part1", |b| b.iter(|| day01::part1(day01::INPUT))); - c.bench_function("day01 part2", |b| b.iter(|| day01::part2(day01::INPUT))); +macro_rules! bench_days { + ($($day_num:literal => $day_mod:ident),* $(,)?) => { + $( + use aoc::$day_mod; + + fn $day_mod(c: &mut Criterion) { + c.bench_function(concat!(stringify!($day_mod), " part1"), |b| { + b.iter(|| $day_mod::part1($day_mod::INPUT)) + }); + c.bench_function(concat!(stringify!($day_mod), " part2"), |b| { + b.iter(|| $day_mod::part2($day_mod::INPUT)) + }); + } + )* + + criterion_group!(benches, $($day_mod),*); + criterion_main!(benches); + }; } -fn day02_benchmark(c: &mut Criterion) { - c.bench_function("day02 part1", |b| b.iter(|| day02::part1(day02::INPUT))); - c.bench_function("day02 part2", |b| b.iter(|| day02::part2(day02::INPUT))); -} - -criterion_group!(benches, day01_benchmark, day02_benchmark); -criterion_main!(benches); +aoc::all_days!(bench_days); diff --git a/src/days.rs b/src/days.rs new file mode 100644 index 0000000..ec8584f --- /dev/null +++ b/src/days.rs @@ -0,0 +1,12 @@ +// Single source of truth for all implemented days +// Add new days here and they'll automatically be available in both the runner and benchmarks + +#[macro_export] +macro_rules! all_days { + ($macro_name:path) => { + $macro_name! { + 1 => day01, + 2 => day02, + } + }; +} diff --git a/src/lib.rs b/src/lib.rs index 28326d5..cd2013a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod day01; pub mod day02; +pub mod days; diff --git a/src/main.rs b/src/main.rs index 23dcd11..0630544 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ +mod days; mod runner; -pub mod day01; -pub mod day02; +use aoc::{day01, day02}; use clap::Parser; use color_eyre::Result; @@ -24,10 +24,7 @@ struct Args { part: Option, } -runner::days! { - 1 => day01, - 2 => day02, -} +all_days!(runner::days); fn main() -> Result<()> { color_eyre::install()?;