Solve Part 2

This commit is contained in:
apio 2022-12-09 11:11:25 +01:00
parent 6ce82f8d4d
commit 49316e33a7
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -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<char> = 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();
}