| -rw-r--r-- | src/lib.rs | 56 | ||||
| -rw-r--r-- | src/util.rs | 4 |
2 files changed, 35 insertions, 25 deletions
@@ -26,20 +26,22 @@ mod util; pub mod day1 { use crate::util::prelude::*; - pub fn part1(input: &str) -> impl std::fmt::Display { + + pub fn part1(i: &str) -> impl Display { static mut a: [u32; 1000] = [0; 1000]; static mut b: [u32; 1000] = [0; 1000]; - input - .行() - .map(crate::util::nail::<{ 5 + 5 + 3 }>) - .map(|x| (reading::all(&x[0..5]), reading::all(&x[8..13]))) - .enumerate() - .for_each(|(i, (x, y))| { - C! { a[i] = x }; - C! { b[i] = y }; - }); unsafe { + i.as_bytes() + .as_chunks_unchecked::<14>() + .into_iter() + .map(|x| (reading::all(&x[0..5]), reading::all(&x[8..13]))) + .enumerate() + .for_each(|(i, (x, y))| { + *a.get_unchecked_mut(i) = x; + *b.get_unchecked_mut(i) = y; + }); + a.sort_unstable(); b.sort_unstable(); a.iter() @@ -52,21 +54,29 @@ pub mod day1 { pub fn part2(i: &str) -> impl Display { static mut a: [u32; 1000] = [0; 1000]; - let mut map = HashMap::<u32, u32>::with_capacity_and_hasher( - 1000, - rustc_hash::FxBuildHasher::default(), - ); - i.行() - .map(crate::util::nail::<{ 5 + 5 + 3 }>) - .map(|x| (reading::all(&x[0..5]), reading::all(&x[8..13]))) - .enumerate() - .for_each(|(i, (x, y))| { - C! { a[i] = x }; - map.entry(y).and_modify(|x| *x += 1).or_insert(1); - }); + static mut map: [u32; 100000] = [0; 100000]; + let i = i.as_bytes(); + unsafe { + let x = C! { &i[..14] }; + let (x, y) = (reading::all(&x[0..5]), reading::all::<u32>(&x[8..13])); + *a.get_unchecked_mut(0) = x; + *map.get_unchecked_mut(y as usize) += 1; + + for n in 1..1000 { + let x = crate::util::reading::八( + u64::from_le_bytes(crate::util::nail::<8>(&i[n * 14 - 3..])) + & 0xffffffffff000000, + ); + let y = crate::util::reading::八(u64::from_le_bytes(crate::util::nail::<8>( + &i[n * 14 + 5..], + ))); + *a.get_unchecked_mut(n) = x as u32; + *map.get_unchecked_mut(y as usize) += 1; + } a.iter() - .map(|x| x * map.get(x).copied().unwrap_or(0)) + .copied() + .map(|x| x * map.get_unchecked(x as usize)) .sum::<u32>() } } diff --git a/src/util.rs b/src/util.rs index 008f59a..f99e88e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -897,9 +897,9 @@ pub fn nail<const N: usize>(x: &[u8]) -> [u8; N] { } pub mod reading { - pub fn 八(s: [u8; 8]) -> u64 { + pub fn 八(n: u64) -> u64 { // reinterpret as u64 ("92233721" => 92233721) - let n = u64::from_le_bytes(s); + // let n = u64::from_le_bytes(s); // combine 4 pairs of single digits: // split pieces into odd and even // 1_7_3_2_ (le repr) |