Day 6 - add 2nd solving strategy

This commit is contained in:
Jonathan Flueren 2022-12-06 22:02:11 +01:00
parent f5a7defd00
commit 82ede0ec38

View file

@ -18,6 +18,8 @@ pub fn solve() {
println!("Result 2: {}", res); println!("Result 2: {}", res);
} }
// Finds 1st occurence of l unique characters in chars and returns // Finds 1st occurence of l unique characters in chars and returns
// the index of the last of them or -1 // the index of the last of them or -1
fn find_marker(l: usize, chars: &Vec<char>) -> i32 { fn find_marker(l: usize, chars: &Vec<char>) -> i32 {
@ -34,3 +36,13 @@ fn find_marker(l: usize, chars: &Vec<char>) -> i32 {
-1 -1
} }
fn _find_marker_v2(l: usize, chars: &Vec<char>) -> usize {
chars.iter()
.filter(|c| **c != '\n')
.collect::<Vec<&char>>()
.windows(l)
.position(|window| window.iter().collect::<HashSet<_>>().len() == l)
.map(|pos| pos+l)
.unwrap()
}