summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 54ce211180eeb7fd738158aa85877f1277d4ab40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#![feature(
    iterator_try_collect,
    iter_map_windows,
    iter_array_chunks,
    array_windows,
    iter_intersperse,
    impl_trait_in_assoc_type,
    macro_metavar_expr,
    slice_group_by
)]

pub mod day_01;
pub mod day_02;
pub mod day_03;
pub mod day_04;
pub mod day_05;
pub mod day_06;
pub mod day_07;
pub mod day_08;
pub mod day_09;
pub mod day_10;
pub mod day_11;
pub mod day_12;
pub mod day_13;
pub mod day_14;
pub mod day_15;
pub mod day_16;
pub mod day_17;
pub mod day_18;
pub mod printer;

pub use printer::Printer;

pub type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

#[macro_export]
macro_rules! get_days {
    // `()` indicates that the macro takes no argument.
    ($($day:path),*) => {
        pub fn get_days() -> [(&'static str, [Box<dyn Fn(&str) -> anyhow::Result<usize>>; 2]); ${count(day)}] {
            [
                $((<$day>::INPUT,
                    [
                    Box::new(<$day>::part_1),
                    Box::new(<$day>::part_2),
                ])
    ,)*
            ]
        }
    };
}

get_days!(
    day_01::Day01,
    day_02::Day02,
    day_03::Day03,
    day_04::Day04,
    day_05::Day05,
    day_06::Day06,
    day_07::Day07,
    day_08::Day08,
    day_09::Day09,
    day_10::Day10,
    day_11::Day11,
    day_12::Day12,
    day_13::Day13,
    day_14::Day14,
    day_15::Day15,
    day_16::Day16,
    day_17::Day17,
    day_18::Day18
);

pub trait Problem {
    const DAY: u8;

    const INPUT: &'static str;
}

pub trait Solution: Problem {
    type Answer1: std::fmt::Display + Default;

    type Answer2: std::fmt::Display + Default;

    fn part_1(input: &str) -> anyhow::Result<Self::Answer1>;

    fn part_2(input: &str) -> anyhow::Result<Self::Answer2>;

    fn solve() -> anyhow::Result<()> {
        print!("Day {}.1", Self::DAY);
        let timer = std::time::SystemTime::now();
        let answer = Self::part_1(Self::INPUT)?;
        let duration = timer.elapsed()?;
        println!(" ({:.2}ms)", duration.as_micros() as f64 / 100f64);
        println!("{answer}\n");

        print!("Day {}.2", Self::DAY);
        let timer = std::time::SystemTime::now();
        let answer = Self::part_2(Self::INPUT)?;
        let duration = timer.elapsed()?;
        println!(" ({:.2}ms)", duration.as_micros() as f64 / 100f64);
        println!("{answer}\n");

        Ok(())
    }
}