From 33a79a23bcebf0c6287fc826c405efd0962b2f72 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 7 Dec 2022 14:10:58 +1030 Subject: [PATCH] Precompute only 2char masks --- 2022/day6.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/2022/day6.rs b/2022/day6.rs index 69af4d7..c09fbb6 100644 --- a/2022/day6.rs +++ b/2022/day6.rs @@ -41,25 +41,26 @@ fn find_first_unique_runs(s: &Vec) -> (Int, Int) { for i in 0..4096 { masks[i] = 1 << (s[i]-b'a'); } - let mut m: [u32; 4096] = masks.clone(); - for n in 1..4 { - for i in n..4096 { - m[i] |= masks[i-n]; - } + let mut masks2: [u32; 4096] = masks.clone(); + for i in 1..4096 { + masks2[i] |= masks[i-1]; } for i in 4..4096 { - if m[i].count_ones() == 4 { + // let mut mask = masks[i]; + // for j in 1..4 {mask |= masks[i-j];} + let mut mask = masks2[i]; + for j in 1..2 {mask |= masks2[i-j*2];} + if mask.count_ones() == 4 { four = i; break; } } - for n in 4..14 { - for i in n..4096 { - m[i] |= masks[i-n]; - } - } for i in (four+9)..4096 { - if m[i].count_ones() == 14 { + // let mut mask = masks[i]; + // for j in 1..14 {mask |= masks[i-j];} + let mut mask = masks2[i]; + for j in 1..7 {mask |= masks2[i-j*2];} // (2..14).step_by(2) doesn't unroll, LLVM is garbage + if mask.count_ones() == 14 { fourteen = i; break; }