From 39debb68757a40a36def610a1fc046e2b9703998 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Tue, 6 Dec 2022 21:38:57 +0100 Subject: [PATCH] Even more optimization --- 2022/src/bin/day6.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/2022/src/bin/day6.rs b/2022/src/bin/day6.rs index b460eec..d9eb528 100644 --- a/2022/src/bin/day6.rs +++ b/2022/src/bin/day6.rs @@ -68,32 +68,25 @@ mod tests { } // -- Helpers -- -fn is_start_marker(window: &[char]) -> bool { +fn is_start_marker((index, window): (usize, &[u8])) -> Option { for i in 0..window.len() { for j in i+1..window.len() { if window[i] == window[j] { - return false; + return None; } } } - return true; + return Some(index + window.len()); } fn solution(input: &str, length: usize) -> usize { input - .chars() - .collect::>() + .as_bytes() .windows(length) - .map(is_start_marker) .enumerate() - .find_map(|(i, a)| { - if a { - Some(i+length) - } else { - None - } - }).expect("Invalid input") + .find_map(is_start_marker) + .expect("Invalid input") } // -- Solution --