/// --- Part Two --- /// /// Your device's communication system is correctly detecting packets, but still isn't working. It /// looks like it also needs to look for messages. /// /// A start-of-message marker is just like a start-of-packet marker, except it consists of 14 /// distinct characters rather than 4. /// /// Here are the first positions of start-of-message markers for all of the above examples: /// /// mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19 /// bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23 /// nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23 /// nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29 /// zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26 /// /// How many characters need to be processed before the first start-of-message marker is detected? use clap::Parser; use itertools::Itertools; use std::fs::File; use std::io::prelude::*; use std::io::BufReader; use std::iter::*; use std::path::PathBuf; const FILEPATH: &'static str = "examples/input.txt"; const CONS_CHARS: usize = 14; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Cli { #[clap(short, long, default_value = FILEPATH)] file: PathBuf, } fn main() { let args = Cli::parse(); let file = File::open(&args.file).unwrap(); let reader = BufReader::new(file); let line = reader.lines().next().unwrap().unwrap(); let res = line .into_bytes() .windows(CONS_CHARS) .position(|b| b.iter().all_unique()) .unwrap() + CONS_CHARS; println!("{:?}", res); }