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
#![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(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
enum D {
    N,
    E,
    S,
    W,
}

macro_rules! sret {
    ($a:ident -= $b:expr) => {
        $a = match $a.checked_sub($b) {
            Some(x) => x,
            None => break,
        }
    };
}

pub fn run(i: &str) -> impl Display {
    use D::*;
    let mat = i.行().collect::<Box<_>>();
    let mut e = vec![vec![false; mat.len()]; mat.len()];
    let mut been = HashSet::new();
    fn beam(
        mat: &[&[u8]],
        (mut x, mut y): (usize, usize),
        mut d: D,
        e: &mut [Vec<bool>],
        been: &mut HashSet<(usize, usize, D)>,
    ) {
        println!("new beam!");
        loop {
            if y >= mat.len() || x >= mat.len() {
                break;
            }
            e[y][x] = true;
            println!("hello {} (going {d:?})", mat[y][x] as char);
            match (mat[y][x], d) {
                (b'|', E | W) => {
                    if been.insert((x, y, N)) {
                        println!("splitting |");
                        if let Some(v) = y.checked_sub(1)
                            && been.insert((x, v, N))
                        {
                            beam(mat, (x, v), N, e, been);
                        }
                        d = S;
                        y += 1;
                    } else {
                        println!("beam death: repetition");
                        return;
                    }
                }
                (b'-', N | S) => {
                    if been.insert((x, y, N)) {
                        if let Some(v) = x.checked_sub(1) {
                            println!("splitting -");
                            beam(mat, (v, y), W, e, been);
                        }
                        d = E;
                        x += 1;
                    } else {
                        println!("beam death: repetition");
                        return;
                    }
                }
                (b'|' | b'.', N) => sret!(y -= 1),
                (b'-' | b'.', E) => x += 1,
                (b'|' | b'.', S) => y += 1,
                (b'-' | b'.', W) => sret!(x -= 1),
                (b'/', N) | (b'\\', S) => {
                    d = E;
                    x += 1;
                }
                (b'/', E) | (b'\\', W) => {
                    d = N;
                    sret!(y -= 1);
                }
                (b'/', S) | (b'\\', N) => {
                    d = W;
                    sret!(x -= 1);
                }
                (b'/', W) | (b'\\', E) => {
                    d = S;
                    y += 1;
                }
                _ => unreachable!(),
            }
        }
        println!("beam death");
    }
    beam(&mat, (0, 0), E, &mut e, &mut been);
    e.iter()
        .map(|x| x.iter().filter(|x| **x).count())
        .sum::<usize>()
}

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));
}