Even more optimization

This commit is contained in:
Dreaded_X 2022-12-06 21:38:57 +01:00
parent 714c699f9b
commit 39debb6875
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9

View File

@ -68,32 +68,25 @@ mod tests {
} }
// -- Helpers -- // -- Helpers --
fn is_start_marker(window: &[char]) -> bool { fn is_start_marker((index, window): (usize, &[u8])) -> Option<usize> {
for i in 0..window.len() { for i in 0..window.len() {
for j in i+1..window.len() { for j in i+1..window.len() {
if window[i] == window[j] { if window[i] == window[j] {
return false; return None;
} }
} }
} }
return true; return Some(index + window.len());
} }
fn solution(input: &str, length: usize) -> usize { fn solution(input: &str, length: usize) -> usize {
input input
.chars() .as_bytes()
.collect::<Vec<_>>()
.windows(length) .windows(length)
.map(is_start_marker)
.enumerate() .enumerate()
.find_map(|(i, a)| { .find_map(is_start_marker)
if a { .expect("Invalid input")
Some(i+length)
} else {
None
}
}).expect("Invalid input")
} }
// -- Solution -- // -- Solution --