diff --git a/src/bin/2022-6.rs b/src/bin/2022-6.rs new file mode 100644 index 0000000..fe8c7e2 --- /dev/null +++ b/src/bin/2022-6.rs @@ -0,0 +1,44 @@ +use itertools::Itertools; + +fn main() { + let input = aoc::input!(); + println!("part 1: {}", part1(&input)); + println!("part 1: {}", part2(&input)); +} + +fn part1(input: &str) -> usize { + input + .as_bytes() + .windows(4) + .position(|w| w.iter().all_unique()) + .unwrap() + + 4 +} + +fn part2(input: &str) -> usize { + input + .as_bytes() + .windows(14) + .position(|w| w.iter().all_unique()) + .unwrap() + + 14 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn examples() { + assert_eq!(part1("bvwbjplbgvbhsrlpgdmjqwftvncz"), 5); + assert_eq!(part1("nppdvjthqldpwncqszvftbrmjlhg"), 6); + assert_eq!(part1("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), 10); + assert_eq!(part1("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), 11); + + assert_eq!(part2("mjqjpqmgbljsphdztnvjfqwrcgsmlb"), 19); + assert_eq!(part2("bvwbjplbgvbhsrlpgdmjqwftvncz"), 23); + assert_eq!(part2("nppdvjthqldpwncqszvftbrmjlhg"), 23); + assert_eq!(part2("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), 29); + assert_eq!(part2("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), 26); + } +}