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
#![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;
use arrayvec::ArrayVec;
pub use util::prelude::*;

pub fn hash16(s: &[u8]) -> u16 {
    s.as_ref()
        .iter()
        .fold(0u16, |acc, &x| acc.wrapping_add(x.widen()).wrapping_mul(17))
}

pub fn hash(s: impl AsRef<[u8]>) -> u8 {
    s.as_ref()
        .iter()
        .fold(0u8, |acc, &x| acc.wrapping_add(x).wrapping_mul(17))
}

pub fn p2(i: &str) -> u32 {
    // can be 5
    let mut 品 = [const { ArrayVec::<_, 6>::new_const() }; 256];
    for i in i
        .as_bytes()
        .split(|&b| b == b',')
        .take(4000)
        .inspect(|x| shucks!(if x.len() > 8))
    {
        match i
            .split_once(|&b| b == b'=')
            .map(|x| x.mr(|x| C! { x[0] } - b'0'))
        {
            None => {
                let ι = &i[..i.len() - 1];
                let h = hash16(ι);
                let lh = h + C! { ι[0] }.widen();
                let β = &mut 品[(h as u8).nat()];
                β.retain(|(α, _)| *α != lh);
            }
            Some((ι, κ)) => {
                let h = hash16(ι);
                let lh = h + C! { ι[0] }.widen();
                let bx = &mut 品[(h as u8).nat()];
                if let Some((_, σ)) = bx.iter_mut().find(|(α, _)| *α == lh) {
                    *σ = κ;
                } else {
                    unsafe { bx.push_unchecked((lh, κ)) };
                }
            }
        }
    }
    品.into_iter()
        .ι1::<u32>()
        .map(|(bx, i)| {
            bx.iter()
                .map(|(_, x)| *x)
                .ι1::<u32>()
                .map(|(x, j)| x as u32 * j)
                .sum::<u32>()
                * i
        })
        .sum::<u32>()
}

#[no_mangle]
pub fn p1(i: &str) -> impl Display {
    i.as_bytes()
        .split(|&x| x == b',')
        .take(4000)
        .inspect(|x| shucks!(if x.len() > 8))
        .map(|x| hash(x) as u32)
        .sum::<u32>()
}

pub fn run(i: &str) -> impl Display {
    p2(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));
}