| -rw-r--r-- | src/lib.rs | 190 |
1 files changed, 92 insertions, 98 deletions
@@ -24,116 +24,110 @@ core_intrinsics )] mod util; -pub mod day2 { -use crate::util; +pub mod day3 { use crate::util::prelude::*; - #[no_mangle] - fn check(x: &[i8]) -> bool { - if x.len() > 8 { - unsafe { std::hint::unreachable_unchecked() } - } - let state = unsafe { x.first_chunk::<2>().map(|[a, b]| a < b).unwrap_unchecked() }; - x.array_windows::<2>().all(|[a, b]| match state { - true if !(1..=3).contains(&(b - a)) => return false, - false if !(1..=3).contains(&(a - b)) => return false, - _ => true, - }) + + 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 part1(i: &str) -> impl Display { - let mut items = [0; 8]; - i.行() - .filter(|x| { - let mut len = 0; - let mut s = 0; - for &b in *x { - match b { - b' ' => { - C! { items[len] = s as i8 }; - len += 1; - s = 0; - } - b => s = s * 10 + (b - b'0'), + let mut i = i.as_bytes(); + 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; } - C! { items[len] = s as i8 }; - let slice = C! { &items[..len + 1] }; - check(slice) - // (0..items.len()).any(|n| { - // let mut items = items.clone(); - // items.remove(n); - // check2(&items) - // }) - }) - .count() - } - - #[no_mangle] - fn check_pof(x: &[i8]) -> Result<(), u8> { - if x.len() > 8 { - unsafe { std::hint::unreachable_unchecked() } - } - let state = match unsafe { - x.first_chunk::<2>() - .map(|[a, b]| a.cmp(b)) - .unwrap_unchecked() - } { - std::cmp::Ordering::Equal => return Err(0), - std::cmp::Ordering::Greater => false, - std::cmp::Ordering::Less => true, - }; - // windows at home 😔 - for i in 1..x.len() as u8 - 1 { - let [a, b] = util::nail(&x[i as usize..]); - match state { - true if !(1..=3).contains(&(b - a)) => return Err(i), - false if !(1..=3).contains(&(a - b)) => return Err(i), - _ => (), + _ => {} } } - Ok(()) + + sum } - #[no_mangle] - pub fn part2(i: &str) -> impl Display { - let mut items = [0; 8]; - i.行() - .filter(|x| { - let mut len = 0; - let mut s = 0; - for &b in *x { - match b { - b' ' => { - C! { items[len] = s as i8 }; - len += 1; - s = 0; - } - b => { - s = s * 10 + (b - b'0'); + pub fn part2(i: &str) -> impl Display { + let mut i = i.as_bytes(); + 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), } - } + + }; } - C! { items[len] = s as i8 }; - let slice = C! { &items[..len + 1] }; - match check_pof(slice) { - Ok(()) => true, - Err(i) => { - let i = i as usize; - let mut place = [0i8; 7]; - macro_rules! rmv { - ($i:expr, $si: expr) => {{ - place[..$i].copy_from_slice(&slice[..$i]); - let put = &slice[$si..]; - place[$i..$i + put.len()].copy_from_slice(put); - &place[..slice.len() - 1] - }}; - } - check(rmv!(i, i + 1)) // [1, 2, >i<, 4, 5] - || check(rmv!(i + 1, i + 2)) // [1, 2, i, >4<, 5] - || (i > 0 && check(rmv!(i - 1, i))) // [1, >2<, i, 4, 5] - } + 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; } - }) - .count() + _ => 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; + }, + _ => {}, + }, + }}, + } + } + + sum } } |