From d4af3e7c264a62611bbb7c34457fffdb8688ce41 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 6 Dec 2022 17:09:58 -0800 Subject: [PATCH] done with part 2 --- 2022-aoc/src/d6.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/2022-aoc/src/d6.rs b/2022-aoc/src/d6.rs index e5cd635..be4941a 100644 --- a/2022-aoc/src/d6.rs +++ b/2022-aoc/src/d6.rs @@ -7,15 +7,23 @@ type Beep = HashSet; fn parse_input_day1(input: &str) -> String { input.to_string() } + #[aoc_run(day6, part1)] fn part1(input: &str) -> usize { - let input = input.trim(); - let mut out = 4; - for (i, c) in input.as_bytes().windows(4).enumerate() { + get_token(input.trim(), 4) +} + +#[aoc_run(day6, part2)] +fn part2(input: &str) -> usize { + get_token(input.trim(), 14) +} + +fn get_token(input: &str, len: usize) -> usize { + let mut out = 0; + for (i, c) in input.as_bytes().windows(len).enumerate() { let window = Beep::from_iter(c.iter().map(|c| *c as char)); - out = i + 4; - dbg!(i, std::str::from_utf8(c).unwrap(), &window); - if window.len() == 4 { + out = i + len; + if window.len() == len { break; } }