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
#![allow(
    confusable_idents,
    uncommon_codepoints,
    non_upper_case_globals,
    internal_features,
    mixed_script_confusables,
    static_mut_refs,
    incomplete_features
)]
#![feature(
    slice_swap_unchecked,
    generic_const_exprs,
    iter_array_chunks,
    get_many_mut,
    maybe_uninit_uninit_array,
    iter_collect_into,
    hint_assert_unchecked,
    let_chains,
    anonymous_lifetime_in_impl_trait,
    array_windows,
    try_blocks,
    slice_take,
    portable_simd,
    test,
    slice_as_chunks,
    array_chunks,
    slice_split_once,
    core_intrinsics
)]
extern crate test;
pub mod util;
pub use util::prelude::*;

const SIZE: usize = 50;
fn split(x: usize) -> (isize, isize) {
    (
        x as isize / (SIZE as isize + 1),
        x as isize % (SIZE as isize + 1),
    )
}
#[no_mangle]
pub fn run(i: &str) -> impl Display {
    let i = i.as_bytes();
    let mut anti = [0u64; SIZE];
    for character in (b'A'..=b'Z').chain(b'a'..=b'z').chain(b'0'..=b'9') {
        let a = match memchr::memchr(character, i) {
            None => continue,
            Some(a) => a,
        };
        let b = memchr::memchr(character, C! { &i[a + 1..] }).ψ() + a + 1;
        let c = memchr::memchr(character, C! { &i[b + 1..] }).ψ() + b + 1;
        let d = memchr::memchr(character, C! { &i[c + 1..] }).map(|x| x + c + 1);
        let mut anti = |(x1, y1), (x2, y2)| {
            let mut x3 = x2 + (x2 - x1);
            let mut y3 = y2 + (y2 - y1);
            *C! { &mut anti[y2 as usize] } |= 1 << x2 as u64;
            while (x3 >= 0) & (x3 < SIZE as isize) & (y3 >= 0) & (y3 < SIZE as isize) {
                anti[y3 as usize] |= 1 << x3 as u64;
                x3 += x2 - x1;
                y3 += y2 - y1;
            }
        };
        match d {
            Some(d) => {
                let [a, b, c, d] = [a, b, c, d].map(split);
                let mut one = |i, j| {
                    anti(i, j);
                    anti(j, i);
                };
                one(b, a);
                one(c, a);
                one(c, b);
                one(d, a);
                one(d, b);
                one(d, c);
            }
            None => {
                let [a, b, c] = [a, b, c].map(split);
                let mut one = |i: (isize, isize), j: (isize, isize)| {
                    anti(i, j);
                    anti(j, i);
                };
                one(b, a);
                one(c, a);
                one(c, b);
            }
        }
    }
    anti.into_iter().map(u64::count_ones).sum::<u32>()
}

pub fn p1(i: &str) -> u32 {
    let i = i.as_bytes();
    let mut anti = [0u64; SIZE];
    for character in (b'A'..=b'Z').chain(b'a'..=b'z').chain(b'0'..=b'9') {
        let a = match memchr::memchr(character, i) {
            None => continue,
            Some(a) => a,
        };
        let b = memchr::memchr(character, C! { &i[a + 1..] }).ψ() + a + 1;
        let c = memchr::memchr(character, C! { &i[b + 1..] }).ψ() + b + 1;
        let d = memchr::memchr(character, C! { &i[c + 1..] }).map(|x| x + c + 1);
        let mut anti = |(x1, y1), (x2, y2)| {
            let x3 = x2 + (x2 - x1);
            let y3 = y2 + (y2 - y1);
            if (x3 >= 0) & (x3 < SIZE as isize) & (y3 >= 0) & (y3 < SIZE as isize) {
                anti[y3 as usize] |= 1 << x3 as u64;
            }
        };
        match d {
            Some(d) => {
                for i in 0..4 {
                    for j in 0..i {
                        let i = split([a, b, c, d][i]);
                        let j = split([a, b, c, d][j]);
                        anti(i, j);
                        anti(j, i);
                    }
                }
            }
            None => {
                for i in 0..3 {
                    for j in 0..i {
                        let i = split([a, b, c][i]);
                        let j = split([a, b, c][j]);
                        anti(i, j);
                        anti(j, i);
                    }
                }
            }
        }
    }
    anti.into_iter().map(u64::count_ones).sum::<u32>()
}

fn main() {
    // (1..u32::MAX as u64).for_each(|a| assert_eq!(a.ilog10() + 1, digs(a)));
    // let mut s = String::new();
    // for i in 0..1280 {
    let i = include_str!("inp.txt");
    //     s.push_str(i);
    // }
    // std::fs::write("src/inp.txt", s);
    // println!("{}", p2(i));
    println!("{}", run(i));
    println!("{}", p1(i));
}

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