heh
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 159 |
1 files changed, 58 insertions, 101 deletions
diff --git a/src/main.rs b/src/main.rs index 6bde11c..94605aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ let_chains, anonymous_lifetime_in_impl_trait, array_windows, + try_blocks, slice_take, test, slice_as_chunks, @@ -28,108 +29,70 @@ extern crate test; pub mod util; use atools::prelude::*; pub use util::prelude::*; +const SIZE: usize = 140; -fn manual_n<const N: usize>(n: [&u8; N]) -> u32 { - n.iter() - .map(|&&x| (x - b'0') as u32) - .fold(0, |acc, x| acc * 10 + x) -} - -pub fn p1(i: &str) -> impl Display { - let mut i = i.as_bytes(); +#[no_mangle] +pub fn run(i: &str) -> impl Display { + let grid = unsafe { i.as_bytes().as_chunks_unchecked::<{ SIZE + 1 }>() }; + let get = |x: isize, y: isize| -> Option<u8> { + (x >= 0 && y >= 0 && x < SIZE as isize && y < SIZE as isize) + .then(|| unsafe { grid.get_unchecked(y as usize)[x as usize] }) + }; + macro_rules! ck { + ($x:expr,$y:expr, $eq:literal) => {{ + get($x, $y) == Some($eq) + }}; + } let mut sum = 0; - while let Some(idx) = memchr::memchr(b'm', i) { - i = C! { &i[idx + 1..] }; - match i { - [b'u', b'l', b'(', rest @ ..] => { - macro_rules! cases { - ($([$($i:ident)+,$($t:ident)+])+) => { - match rest { - $( - [$($i @ b'0'..=b'9'),+, b',', $($t @ b'0'..=b'9'),+, b')', rest @ ..] => { - (manual_n([$($i),+]) * manual_n([$($t),+]), rest) - } - )+ - _ => (0, i), - } - - }; - } - let (res, rest) = cases!( - [a b c , d e f] - [a b c , d e] - [a b c , d] - [a b , d e f] - [a b , d e] - [a b , d] - [a , d e f] - [a , d e] - [a , d] - ); - sum += res; - i = rest; - } - _ => {} + for (x, y) in C! { &grid[..SIZE] }.iter().enumerate().flat_map(|(y, e)| { + memchr::memchr_iter(b'X', &e[..SIZE]).map(move |x| (x as isize, y as isize)) + }) { + macro_rules! triple { + ($x: literal, $y:literal) => { + (ck!(x + ($x * 3), y + ($y * 3), b'S') + && ck!(x + ($x * 2), y + ($y * 2), b'A') + && ck!(x + $x, y + $y, b'M')) as u32 + }; } - } + sum += 0 // a + + triple!(1, 0) + triple!(-1, 0) + + triple!(0, 1) + triple!(0, -1) + + triple!(1, 1) + triple!(-1, 1) + triple!(-1, -1) + triple!(1, -1); + } sum } -pub fn run(i: &str) -> impl Display { - let mut i = i.as_bytes(); +pub fn two(i: &str) -> impl Display { + let grid = unsafe { i.as_bytes().as_chunks_unchecked::<{ SIZE + 1 }>() }; + let get = |x: isize, y: isize| -> Option<u8> { + (x >= 0 && y >= 0 && x < SIZE as isize && y < SIZE as isize) + .then(|| unsafe { grid.get_unchecked(y as usize)[x as usize] }) + }; let mut sum = 0; - let mut on = 1; - while let Some(idx) = memchr::memchr2(b'm', b'd', i) { - i = C! { &i[idx + 1..] }; - match i { - [b'u', b'l', b'(', rest @ ..] => { - macro_rules! cases { - ($([$($i:ident)+,$($t:ident)+])+) => { - match rest { - $( - [$($i @ b'0'..=b'9'),+, b',', $($t @ b'0'..=b'9'),+, b')', rest @ ..] => { - (manual_n([$($i),+]) * manual_n([$($t),+]) * on, rest) - } - )+ - _ => (0, i), - } - - }; - } - let (res, rest) = cases!( - [a b c , d e f] - [a b c , d e] - [a b c , d] - [a b , d e f] - [a b , d e] - [a b , d] - [a , d e f] - [a , d e] - [a , d] - ); - sum += res; - i = rest; - } - _ => mat! { on { - 0 => match i { - [b'o', b'(', rest @ ..] => { - on = 1; - i = rest; - } - _ => {} - }, - 1 => match i { - [b'o', b'n', rest @ ..] => { - on = 0; - i =rest; - }, - _ => {}, - }, - }}, - } + for (x, y) in grid[..SIZE - 1] + .iter() + .enumerate() + .skip(1) + .flat_map(|(y, e)| { + memchr::memchr_iter(b'A', &e[1..SIZE - 1]).map(move |x| (x as isize + 1, y as isize)) + }) + { + // println!("{x} {y}"); + // for (y, x) in (0..SIZE as isize).flat_map(|x| (0..SIZE as isize).map(move |y| (x, y))) { + // if get(x, y) == Some(b'A') { + let n: Option<u32> = try { + let a = get(x - 1, y - 1)?; + let b = get(x + 1, y + 1)?; + ((a == b'M' && b == b'S') || (a == b'S' && b == b'M')).then(|| { + let a = get(x + 1, y - 1).ψ(); + let b = get(x - 1, y + 1).ψ(); + (a == b'M' && b == b'S') || (a == b'S' && b == b'M') + })? as u32 + }; + sum += n.unwrap_or(0); + // } } - sum } @@ -140,9 +103,9 @@ fn main() { // s.push_str(i); // } // std::fs::write("src/inp.txt", s); - println!("{}", p1(i)); + // prinan!("{}", p1(i)); println!("{}", run(i)); - // println!("{}", p2(i)); + println!("{}", two(i)); } #[bench] @@ -150,9 +113,3 @@ fn bench(b: &mut test::Bencher) { let i = boxd(include_str!("inp.txt").trim()); b.iter(|| run(i)); } - -#[bench] -fn bench2(b: &mut test::Bencher) { - let i = boxd(include_str!("inp.txt").trim()); - b.iter(|| run(i)); -} |