1
0
Fork 0

Optimized day 6 1/2 solutions

This commit is contained in:
Ishan Jain 2022-12-12 01:31:13 +05:30
parent 283b6faa4d
commit 8ac6d089e4
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 15 additions and 19 deletions

View File

@ -21,23 +21,16 @@ fn main() {
}
fn solution(input: &[u8]) -> usize {
let mut buffer = [0; 26];
let mut buffer = 0u32;
for &c in input.iter().take(4) {
buffer[(c - b'a') as usize] += 1;
}
for i in 0..input.len() {
buffer ^= 1 << (input[i] - b'a');
if buffer.iter().all(|&c| c == 0 || c == 1) {
return 0;
}
if i >= 4 {
buffer ^= 1 << (input[i - 4] - b'a');
}
for i in 4..input.len() {
let c = input[i];
buffer[(c - b'a') as usize] += 1;
let last = input[i - 4];
buffer[(last - b'a') as usize] -= 1;
if buffer.iter().all(|&c| c == 0 || c == 1) {
if buffer.count_ones() == 4 {
return i + 1;
}
}

View File

@ -21,15 +21,18 @@ fn main() {
}
fn solution<const N: usize>(input: &[u8]) -> usize {
for idx in 0..input.len() - N {
let mut buffer = 0u32;
let mut buffer = 0u32;
for idx in 0..input.len() {
// Set the bit when we enter
buffer ^= 1 << (input[idx] - b'a');
for &c in &input[idx..(idx + N)] {
buffer |= 1 << (c - b'a') as usize;
// Reset it when we leave
if idx >= N {
buffer ^= 1 << (input[idx - N] - b'a');
}
if buffer.count_ones() as usize == N {
return idx + N;
return idx + 1;
}
}