From c02cab107528dbeee88dd49def0b141c867311c9 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 6 Dec 2022 08:32:16 -0700 Subject: [PATCH] Add solution for day 6 --- rust/src/bin/day06.rs | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 rust/src/bin/day06.rs diff --git a/rust/src/bin/day06.rs b/rust/src/bin/day06.rs new file mode 100644 index 0000000..a7b2032 --- /dev/null +++ b/rust/src/bin/day06.rs @@ -0,0 +1,54 @@ +use rust::util::read_input; +use std::collections::{hash_map::RandomState, HashSet, VecDeque}; + +fn part1(input: &str) -> usize { + let mut previous_three: VecDeque = VecDeque::new(); + for (i, c) in input.char_indices() { + if previous_three.len() < 3 { + previous_three.push_back(c); + continue; + } + + let set: HashSet<&char, RandomState> = HashSet::from_iter(previous_three.iter()); + if previous_three.contains(&c) || set.len() < 3 { + previous_three.pop_front(); + previous_three.push_back(c); + continue; + } + + return i + 1; + } + 0 +} + +fn part2(input: &str) -> usize { + let mut previous_three: VecDeque = VecDeque::new(); + for (i, c) in input.char_indices() { + if previous_three.len() < 13 { + previous_three.push_back(c); + continue; + } + + let set: HashSet<&char, RandomState> = HashSet::from_iter(previous_three.iter()); + if previous_three.contains(&c) || set.len() < 13 { + previous_three.pop_front(); + previous_three.push_back(c); + continue; + } + + return i + 1; + } + 0 +} + +fn main() { + let test_input = read_input("Day06_test").expect("Failed to open test input"); + let test_input_first = test_input.first().expect("Received empty input"); + assert_eq!(7, part1(&test_input_first)); + assert_eq!(19, part2(&test_input_first)); + + let input = read_input("Day06").expect("Failed to open input"); + let input_first = input.first().expect("Received empty input"); + println!("{:?}", part1(&input_first)); + println!("{:?}", part2(&input_first)); +}