From a36e74d094e27631dd73e382752379e2b30e05c7 Mon Sep 17 00:00:00 2001 From: Viacheslav Slinko Date: Sun, 22 Dec 2024 16:08:25 +0300 Subject: [PATCH] day22 --- src/day22.rs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/day22.rs b/src/day22.rs index a3c63ff..83404de 100644 --- a/src/day22.rs +++ b/src/day22.rs @@ -21,6 +21,24 @@ fn iter(number: i64) -> i64 { prune(mix(number, number * 2048)) } +macro_rules! read_unsigned { + ($input:expr, $i:expr) => {{ + let mut num = 0; + loop { + match $input.get_unchecked($i) { + b'0'..=b'9' => { + num = num * 10 + ($input.get_unchecked($i) - b'0') as i64; + $i += 1; + } + _ => { + break; + } + } + } + num + }}; +} + unsafe fn parse(input: &str) -> i64 { input.parse::().unwrap_unchecked() } @@ -35,8 +53,8 @@ pub fn part1(input: &str) -> i64 { )] #[cfg_attr(avx512_available, target_feature(enable = "avx512vl"))] unsafe fn inner1(input: &str) -> i64 { - let mut initial_numbers = input.lines().map(|line| parse(line)).collect::>(); - initial_numbers.extend(vec![0; (64 - initial_numbers.len() % 64) % 64]); + let input = input.as_bytes(); + let mut i = 0; let _15 = i64x64::splat(15); let _32 = i64x64::splat(32); @@ -49,10 +67,19 @@ unsafe fn inner1(input: &str) -> i64 { let _100000000 = i64x64::splat(100000000); let mut result = 0; - let mut i = 0; + let mut nums = [0; 64]; + + while i < input.len() { + for j in 0..64 { + if i < input.len() { + *nums.get_unchecked_mut(j) = read_unsigned!(input, i); + i += 1; + } else { + *nums.get_unchecked_mut(j) = 0; + } + } - while i < initial_numbers.len() { - let mut nums = i64x64::from_slice(&initial_numbers[i..i + 64]); + let mut nums = i64x64::from_slice(&nums); macro_rules! mix { ($expr:expr) => {{ @@ -81,7 +108,6 @@ unsafe fn inner1(input: &str) -> i64 { } result += nums.reduce_sum(); - i += 64; } result @@ -152,12 +178,7 @@ unsafe fn inner2(input: &str) -> i64 { for _ in 4..2000 { next!(); - - a = b; - b = c; - c = d; - d = diff; - + (a, b, c, d) = (b, c, d, diff); remember_seq!(); } }