33 lines
844 B
Rust
33 lines
844 B
Rust
#![feature(iterator_try_collect)]
|
|
|
|
use itertools::Itertools;
|
|
use std::{collections::VecDeque, fs};
|
|
|
|
const START_PACKET_SIZE: usize = 4;
|
|
const START_MESSAGE_SIZE: usize = 14;
|
|
|
|
fn main() {
|
|
let filename = "etc/p06.txt";
|
|
let string = fs::read_to_string(filename).expect("Can't open file");
|
|
|
|
get_unique_marker(&string, START_PACKET_SIZE);
|
|
get_unique_marker(&string, START_MESSAGE_SIZE);
|
|
}
|
|
|
|
fn get_unique_marker(string: &str, len: usize) -> Option<usize> {
|
|
// starting with naive solution.
|
|
let mut vec: VecDeque<char> = string.chars().take(len).collect();
|
|
let num = string.chars().skip(len).position(|c| {
|
|
if vec.iter().unique().count() >= len {
|
|
true
|
|
} else {
|
|
vec.push_back(c);
|
|
vec.pop_front();
|
|
false
|
|
}
|
|
})? + len;
|
|
|
|
dbg!(num);
|
|
Some(num)
|
|
}
|