advent-of-code/2022/day6/rust/src/main.rs

24 lines
636 B
Rust
Raw Normal View History

2022-12-06 06:12:56 +01:00
#![warn(clippy::pedantic)]
2022-12-06 06:27:08 +01:00
use std::io::{stdin, Read};
2022-12-06 06:12:56 +01:00
2022-12-06 17:32:44 +01:00
fn all_distinct<T: PartialEq>(slice: &[T]) -> bool {
2022-12-06 06:27:08 +01:00
slice
.iter()
.enumerate()
.all(|(i, x)| slice[i + 1..].iter().all(|y| y != x))
}
2022-12-06 17:32:44 +01:00
fn end_of_first_distinct_run<T: PartialEq>(data: &[T], len: usize) -> Option<usize> {
data.windows(len).position(all_distinct).map(|i| i + len)
2022-12-06 06:27:08 +01:00
}
2022-12-06 06:12:56 +01:00
fn main() {
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
2022-12-06 06:27:08 +01:00
let data = data.as_bytes();
2022-12-06 06:12:56 +01:00
2022-12-06 06:27:08 +01:00
println!("{:?}", end_of_first_distinct_run(data, 4).unwrap());
println!("{:?}", end_of_first_distinct_run(data, 14).unwrap());
2022-12-06 06:12:56 +01:00
}