aoc/
puzzles.rs

1use crate::all_puzzles;
2use utils::date::{Day, Year};
3use utils::input::strip_final_newline;
4
5// These imports are unused if none of the year features are enabled
6#[allow(clippy::allow_attributes, unused_imports)]
7use utils::{
8    Puzzle,
9    input::{InputError, InputType},
10};
11
12/// Represents a wrapper function around a puzzle solution.
13///
14/// See [`PUZZLES`].
15pub type PuzzleFn = fn(&str) -> Result<(String, String), InputError>;
16
17macro_rules! matcher {
18    ($(
19        $y:literal => $year:ident{$(
20            $d:literal => $day:ident,
21        )*}
22    )*) => {
23        /// Slice containing all supported puzzle solutions.
24        ///
25        /// Each puzzle is represented by a tuple of [`Year`], [`Day`] and [`PuzzleFn`], which takes
26        /// a input string and returns the part 1 and 2 solutions as strings, or an [`InputError`].
27        ///
28        /// Generated from [`all_puzzles!`].
29        pub static PUZZLES: &[(Year, Day, PuzzleFn)] = &[$($(
30            (crate::$year::$day::YEAR, crate::$year::$day::DAY, |input: &str| {
31                let input = strip_final_newline(input);
32                let solution = crate::$year::$day::new(input, InputType::Real)?;
33                let part1 = solution.part1();
34                let part2 = solution.part2();
35                Ok((part1.to_string(), part2.to_string()))
36            }),
37        )*)*];
38    };
39}
40all_puzzles!(matcher);