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
#![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::*;

pub fn p1(i: &str) -> u16 {
    let mut y = 0i32;
    let mut a = 0i32;
    let mut b = 0u16;
    let mut i = i.as_bytes();
    loop {
        let d = unsafe {
            rint::<_, Dir>(match i.by() {
                Ok(x) => x,
                Err(_) => break,
            })
        };
        let c = match unsafe { i.rd::<3>().unwrap_unchecked() } {
            [_, b, b' '] => {
                i = C! { &i[10..] };
                b - b'0'
            }
            [_, a, b] => {
                _ = i.read(&mut [0; 11]);
                // i = C! { &i[11..] };
                (a - b'0') * 10 + (b - b'0')
            }
        };
        b += c as u16;
        match d {
            Dir::N => y += c as i32,
            Dir::E => a += c as i32 * y,
            Dir::S => y -= c as i32,
            Dir::W => a -= c as i32 * y,
        }
    }

    // use shoelace formula to get the area, then use picks formula to count the number of inner points
    a.abs() as u16 + (1 + b / 2)
}

pub fn p2(i: &str) -> u64 {
    let mut y = 0;
    let mut a = 0;
    let mut b = 0;
    let mut i = i.as_bytes();
    loop {
        let Ok(_) = i.by() else { break };
        i.skip(2);
        if unsafe { i.by().unwrap_unchecked() == b' ' } {
            i.skip(2);
        } else {
            i.skip(3);
        }
        let dat = unsafe { i.rd::<6>().unwrap_unchecked() };
        _ = i.read(&mut [0; 2]);
        let c = 読む::hex5(dat[0..5].try_into().unwrap());
        const A: [i64; 4] = [1, 0, -1, 0];
        const Y: [i64; 4] = [0, -1, 0, 1];
        let d = dat[5] - b'0';
        a += C! { A[d.nat()] } * c as i64 * y;
        y += C! { Y[d.nat()] } * c as i64;
        b += c as u64;
    }
    a.abs() as u64 + (1 + b / 2)
}

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