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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#![allow(
    confusable_idents,
    uncommon_codepoints,
    non_upper_case_globals,
    internal_features,
    mixed_script_confusables,
    incomplete_features
)]
#![feature(
    slice_swap_unchecked,
    generic_const_exprs,
    iter_array_chunks,
    get_many_mut,
    maybe_uninit_uninit_array,
    iter_collect_into,
    let_chains,
    anonymous_lifetime_in_impl_trait,
    array_windows,
    slice_take,
    test,
    slice_as_chunks,
    array_chunks,
    slice_split_once,
    core_intrinsics
)]
mod util;
pub mod day3 {
    use crate::util::prelude::*;

    fn manual_n<const N: usize>(n: [&u8; N]) -> u32 {
        n.iter()
            .map(|&&x| (x - b'0') as u32)
            .fold(0, |acc, x| acc * 10 + x)
    }

    pub fn part1(i: &str) -> impl Display {
        let mut i = i.as_bytes();
        let mut sum = 0;
        while let Some(idx) = memchr::memchr(b'm', i) {
            i = C! { &i[idx + 1..] };
            match i {
                [b'u', b'l', b'(', rest @ ..] => {
                    macro_rules! cases {
                        ($([$($i:ident)+,$($t:ident)+])+) => {
                            match rest {
                                $(
                                    [$($i @ b'0'..=b'9'),+, b',', $($t @ b'0'..=b'9'),+, b')', rest @ ..] => {
                                        (manual_n([$($i),+]) * manual_n([$($t),+]), rest)
                                    }
                                )+
                                _ => (0, i),
                            }

                        };
                    }
                    let (res, rest) = cases!(
                        [a b c , d e f]
                        [a b c , d e]
                        [a b c , d]
                        [a b , d e f]
                        [a b , d e]
                        [a b , d]
                        [a , d e f]
                        [a , d e]
                        [a , d]
                    );
                    sum += res;
                    i = rest;
                }
                _ => {}
            }
        }

        sum
    }

    pub fn part2(i: &str) -> impl Display {
        let mut i = i.as_bytes();
        let mut sum = 0;
        let mut on = 1;
        while let Some(idx) = memchr::memchr2(b'm', b'd', i) {
            i = C! { &i[idx + 1..] };
            match i {
                [b'u', b'l', b'(', rest @ ..] => {
                    macro_rules! cases {
                    ($([$($i:ident)+,$($t:ident)+])+) => {
                        match rest {
                            $(
                                [$($i @ b'0'..=b'9'),+, b',', $($t @ b'0'..=b'9'),+, b')', rest @ ..] => {
                                    (manual_n([$($i),+]) * manual_n([$($t),+]) * on, rest)
                                }
                            )+
                            _ => (0, i),
                        }

                    };
                }
                    let (res, rest) = cases!(
                        [a b c , d e f]
                        [a b c , d e]
                        [a b c , d]
                        [a b , d e f]
                        [a b , d e]
                        [a b , d]
                        [a , d e f]
                        [a , d e]
                        [a , d]
                    );
                    sum += res;
                    i = rest;
                }
                _ => mat! { on {
                    0 => match i {
                        [b'o', b'(', rest @ ..] => {
                            on = 1;
                            i = rest;
                        }
                        _ => {}
                    },
                    1 => match i {
                        [b'o', b'n', rest @ ..] => {
                            on = 0;
                            i =rest;
                        },
                        _ => {},
                    },
                }},
            }
        }

        sum
    }
}