2022 day6/rust: cleanup
This commit is contained in:
parent
8859be752a
commit
236b80b45c
1 changed files with 16 additions and 20 deletions
|
@ -1,29 +1,25 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
io::{stdin, Read},
|
||||
};
|
||||
use std::io::{stdin, Read};
|
||||
|
||||
use aoc::*;
|
||||
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)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut data = String::new();
|
||||
stdin().read_to_string(&mut data).unwrap();
|
||||
let data = data.as_bytes();
|
||||
|
||||
let i = data
|
||||
.as_bytes()
|
||||
.windows(4)
|
||||
.position(|cs| HashSet::<u8>::from_iter(cs.into_iter().copied()).len() == 4)
|
||||
.unwrap();
|
||||
|
||||
println!("{:?}", i + 4);
|
||||
|
||||
let i = data
|
||||
.as_bytes()
|
||||
.windows(14)
|
||||
.position(|cs| HashSet::<u8>::from_iter(cs.into_iter().copied()).len() == 14)
|
||||
.unwrap();
|
||||
|
||||
println!("{:?}", i + 14);
|
||||
println!("{:?}", end_of_first_distinct_run(data, 4).unwrap());
|
||||
println!("{:?}", end_of_first_distinct_run(data, 14).unwrap());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue