Macro aoc::all_puzzles

source ·
macro_rules! all_puzzles {
    ($callback:path $(,$arg:tt)*$(,)?) => { ... };
}
Expand description

Macro which invokes a callback macro with a list of all implemented puzzle solutions.

This macro chains puzzles! macros in the re-exported year modules. The callback macro will be called once with all the solutions, which makes it easy to generate match statements or arrays.

Running cargo xtask update will automatically update the chain of year macros.

§Examples

Simple main function to run all examples:

macro_rules! callback {
    ($(
        $y:literal => $year:ident{$(
            $d:literal => $day:ident,
        )*}
    )*) => {$($(
        println!("{} {}", $y, $d);
        for (input_str, p1, p2) in aoc::$year::$day::EXAMPLES {
            let solution = aoc::$year::$day::new(input_str, InputType::Example).unwrap();
            println!("  parse({input_str}) = {solution:?}");
            if (p1.is_some()) { println!("  part1(...) = {}", solution.part1()); }
            if (p2.is_some()) { println!("  part2(...) = {}", solution.part2()); }
        }
    )*)*};
}
all_puzzles!(callback);

Generating a match statement:

fn example_count(year: Year, day: Day) -> Option<usize> {
    macro_rules! callback {
        ($(
            $y:literal => $year:ident{$(
                $d:literal => $day:ident,
            )*}
        )*) => {
            match (year, day) {
                $($(
                    (aoc::$year::$day::YEAR, aoc::$year::$day::DAY) => Some(aoc::$year::$day::EXAMPLES.len()),
                )*)*
                _ => None,
            }
        };
    }
    all_puzzles!{callback}
}