aoc/
puzzles.rs

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