From 49316e33a7e343006411c58cb8eb66c7bb276fdf Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 9 Dec 2022 11:11:25 +0100 Subject: [PATCH] Solve Part 2 --- src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6d9cb31..467e5bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ fn has_duplicates(slice: &[char]) -> bool (1..slice.len()).any(|i| slice[i..].contains(&slice[i - 1])) } +#[allow(dead_code)] fn part1() { let mut last_char = 0usize; @@ -36,6 +37,37 @@ fn part1() } } -fn main() { - part1(); +fn part2() +{ + let mut last_char = 0usize; + + let mut buffer: Vec = Vec::with_capacity(14); + + for c in DATA.chars() + { + last_char += 1; + + if buffer.len() == 14 + { + buffer.remove(0); + } + + buffer.push(c); + + if last_char < 14 { + continue; + } + + if !has_duplicates(buffer.as_slice()) + { + println!("First start-of-packet marker at {}", last_char); + break; + } + } +} + + +fn main() { + //part1(); + part2(); }