From b26dc3438d406715c38748fdf52039155371b530 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Wed, 6 Dec 2023 11:53:00 +0100 Subject: [PATCH 1/2] dry solution! macro --- src/template/mod.rs | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/template/mod.rs b/src/template/mod.rs index 49402b2..72cddac 100644 --- a/src/template/mod.rs +++ b/src/template/mod.rs @@ -39,34 +39,23 @@ pub fn read_file_part(folder: &str, day: Day, part: u8) -> String { #[macro_export] macro_rules! solution { ($day:expr) => { - /// The current day. - const DAY: advent_of_code::template::Day = advent_of_code::day!($day); - - fn main() { - use advent_of_code::template::runner::*; - let input = advent_of_code::template::read_file("inputs", DAY); - run_part(part_one, &input, DAY, 1); - run_part(part_two, &input, DAY, 2); - } + $crate::solution!(@impl $day, [part_one, 1] [part_two, 2]); }; ($day:expr, 1) => { - /// Allows solving part one in isolation - const DAY: advent_of_code::template::Day = advent_of_code::day!($day); - - fn main() { - use advent_of_code::template::runner::*; - let input = advent_of_code::template::read_file("inputs", DAY); - run_part(part_one, &input, DAY, 1); - } + $crate::solution!(@impl $day, [part_one, 1]); }; ($day:expr, 2) => { - /// Allows solving part two in isolation + $crate::solution!(@impl $day, [part_two, 2]); + }; + + (@impl $day:expr, $( [$func:expr, $part:expr] )*) => { + /// The current day. const DAY: advent_of_code::template::Day = advent_of_code::day!($day); fn main() { use advent_of_code::template::runner::*; let input = advent_of_code::template::read_file("inputs", DAY); - run_part(part_two, &input, DAY, 2); + $( run_part($func, &input, DAY, $part); )* } }; } From ad43af3e71c1b41ba12a19d1f3481e5dc5e50c19 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Wed, 6 Dec 2023 12:00:31 +0100 Subject: [PATCH 2/2] hygienize `solution!` macro --- src/template/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/template/mod.rs b/src/template/mod.rs index 72cddac..837fe97 100644 --- a/src/template/mod.rs +++ b/src/template/mod.rs @@ -50,11 +50,11 @@ macro_rules! solution { (@impl $day:expr, $( [$func:expr, $part:expr] )*) => { /// The current day. - const DAY: advent_of_code::template::Day = advent_of_code::day!($day); + const DAY: $crate::template::Day = $crate::day!($day); fn main() { - use advent_of_code::template::runner::*; - let input = advent_of_code::template::read_file("inputs", DAY); + use $crate::template::runner::*; + let input = $crate::template::read_file("inputs", DAY); $( run_part($func, &input, DAY, $part); )* } };