heh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#![allow(confusable_idents, uncommon_codepoints, mixed_script_confusables)]
#![feature(
    inline_const,
    slice_flatten,
    iter_collect_into,
    let_chains,
    anonymous_lifetime_in_impl_trait,
    unchecked_math,
    array_windows,
    slice_take,
    test,
    slice_as_chunks,
    array_chunks,
    slice_split_once,
    byte_slice_trim_ascii
)]
extern crate test;
pub mod util;

pub use util::prelude::*;

#[derive(Eq, Debug, PartialEq)]
enum ModuleT<'a> {
    Flip(bool),
    Cj(HashMap<&'a [u8], bool>),
    Untyped,
}

struct Module<'a> {
    ty: ModuleT<'a>,
    output: Box<[&'a [u8]]>,
}

impl<'a> Module<'a> {
    pub fn pass<'b>(
        &mut self,
        myname: &'a [u8],
        from: &[u8],
        x: bool,
        stack: &'b mut VecDeque<(&'a [u8], &'a [u8], bool)>,
    ) {
        match self.ty {
            ModuleT::Flip(ref mut state) => {
                if x {
                    return;
                }
                *state = !*state;
                for o in &*self.output {
                    stack.push_back((myname, o, *state));
                }
            }
            ModuleT::Cj(ref mut mem) => {
                *mem.get_mut(from).unwrap() = x;
                let s = !mem.values().all(|&x| x);
                for o in &*self.output {
                    stack.push_back((myname, o, s));
                }
            }
            ModuleT::Untyped => {
                for x in &*self.output {
                    stack.push_back((myname, x, false));
                }
            }
        }
    }
}

fn split<'a>(
    i: impl Iterator<Item = &'a [u8]>,
) -> impl Iterator<Item = (&'a [u8], impl Iterator<Item = &'a [u8]>)> {
    i.map(|mut x| {
        let n = x.iter().position(|&x| x == b' ').unwrap();
        let a = &x[..n];
        x.skip(n + 4);
        (a, x.split(|&x| x == b',').map(<[u8]>::trim_ascii))
    })
}

fn parse<'a>(
    i: impl Iterator<Item = (&'a [u8], impl Iterator<Item = &'a [u8]>)>,
) -> HashMap<&'a [u8], Module<'a>> {
    let mut modules = HashMap::new();
    let mut rest = vec![];
    i.map(|(mut from, to)| {
        let to: Box<_> = to.collect();
        match from[0] {
            b'%' => {
                from.skip(1);
                modules.insert(
                    from,
                    Module {
                        ty: ModuleT::Flip(false),
                        output: to,
                    },
                );
            }
            b'&' => {
                from.skip(1);
                rest.push((from, to));
                return;
            }
            _ => {
                modules.insert(
                    from,
                    Module {
                        ty: ModuleT::Untyped,
                        output: to,
                    },
                );
            }
        };
    })
    .Θ();
    let r = rest.clone();
    for (name, output) in rest {
        let mut inps: HashMap<&[u8], bool> = modules
            .iter()
            .filter(|(_, x)| x.output.contains(&name))
            .map(|(x, _)| (*x, false))
            .collect();
        inps.extend(
            r.iter()
                .filter(|(x, _)| *x != name)
                .filter(|(_, x)| x.contains(&name))
                .map(|(x, _)| (*x, false)),
        );
        modules.insert(
            name,
            Module {
                ty: ModuleT::Cj(inps),
                output,
            },
        );
    }
    modules
}

fn p1(i: &str) -> usize {
    let mut modules = parse(split(i.行()));
    fn push(modules: &mut HashMap<&[u8], Module<'_>>) -> (usize, usize) {
        let (mut lo, mut hi) = (0, 0);
        let mut stack = VecDeque::new();
        stack.push_back((&b"upstairs"[..], &b"broadcaster"[..], false));
        while let Some((m, to, x)) = stack.pop_front() {
            if x {
                hi += 1
            } else {
                lo += 1;
            }
            if let Some(o) = modules.get_mut(to) {
                o.pass(to, m, x, &mut stack)
            };
        }
        (lo, hi)
    }

    let (lo, hi) = (0..1000).fold((0, 0), |(lo, hi), _| {
        let (lo2, hi2) = push(&mut modules);
        (lo + lo2, hi + hi2)
    });
    lo * hi
}

fn p2(i: &str) -> u64 {
    let mut modules = parse(split(i.行()));
    let mut from = HashMap::from([
        (&b"xp"[..], None::<u64>),
        (&b"fc"[..], None),
        (&b"dd"[..], None),
        (&b"fh"[..], None),
    ]);

    let mut lens = vec![];
    for when in 0.. {
        let mut stack = VecDeque::new();
        stack.push_back((&b"upstairs"[..], &b"broadcaster"[..], false));
        while let Some((m, to, x)) = stack.pop_front() {
            if !x && let Some(x) = from.get_mut(to) {
                if let Some(y) = x {
                    lens.push(when - *y);
                    from.remove(to);
                    if from.len() == 0 {
                        return lens.iter().product();
                    }
                } else {
                    *x = Some(when);
                }
            }
            if let Some(o) = modules.get_mut(to) {
                o.pass(to, m, x, &mut stack)
            };
        }
    }
    dang!()
}

pub fn run(i: &str) -> impl Display {
    p1(i)
}

fn main() {
    let i = include_str!("inp.txt").trim();
    println!("{}", run(i));
}

#[bench]
fn bench(b: &mut test::Bencher) {
    let i = boxd(include_str!("inp.txt").trim());
    b.iter(|| run(i));
}