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

26 lines
651 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 06:27:08 +01:00
fn all_distinct<T: Ord>(slice: &[T]) -> bool {
slice
.iter()
.enumerate()
.all(|(i, x)| slice[i + 1..].iter().all(|y| y != x))
}
fn end_of_first_distinct_run<T: Ord>(data: &[T], len: usize) -> Option<usize> {
data.windows(len)
.position(|cs| all_distinct(cs))
.map(|i| i + len)
}
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
}