diff --git a/src/bin/day06.rs b/src/bin/day06.rs index d3ec75c..bf26a1f 100644 --- a/src/bin/day06.rs +++ b/src/bin/day06.rs @@ -1,9 +1,8 @@ use std::collections::HashSet; -use std::str::FromStr; use aoc_2022::*; -use color_eyre::eyre::{Report, Result}; +use color_eyre::eyre::Result; use itertools::Itertools; use tracing::*; use tracing_subscriber::EnvFilter; @@ -11,30 +10,25 @@ use tracing_subscriber::EnvFilter; type Input = String; type Output = usize; -fn part_1(input: &str) -> Output { - let chars: Vec<_> = input.chars().collect(); +fn unique_marker_index(buffer: &Input, n: usize) -> Output { + let chars: Vec<_> = buffer.chars().collect(); chars - .windows(4) + .windows(n) .enumerate() .map(|(i, chars)| (i, chars.iter().collect::>())) - .find_or_first(|(_, s)| s.len() == 4) + .find_or_first(|(_, s)| s.len() == n) .unwrap() .0 - + 4 + + n +} + +fn part_1(input: &Input) -> Output { + unique_marker_index(input, 4) } fn part_2(input: &Input) -> Output { - let chars: Vec<_> = input.chars().collect(); - - chars - .windows(14) - .enumerate() - .map(|(i, chars)| (i, chars.iter().collect::>())) - .find_or_first(|(_, s)| s.len() == 14) - .unwrap() - .0 - + 14 + unique_marker_index(input, 14) } fn parse_input(pathname: &str) -> Input {