diff --git a/src/bin/d6p1.rs b/src/bin/d6p1.rs new file mode 100644 index 0000000..15e8c6e --- /dev/null +++ b/src/bin/d6p1.rs @@ -0,0 +1,8 @@ +use aoc22::{day6, util}; + +pub fn main() { + let input = util::parse_input(); + let start = day6::parse_datastream(input.as_str()); + + println!("Datastream starts after {} characters", start + 1); +} diff --git a/src/day6.rs b/src/day6.rs new file mode 100644 index 0000000..ba310b0 --- /dev/null +++ b/src/day6.rs @@ -0,0 +1,19 @@ +use std::collections::{HashSet, VecDeque}; + +pub fn parse_datastream(stream: &str) -> usize { + let mut chars: VecDeque = VecDeque::new(); + for (i, char) in stream.chars().enumerate() { + if i >= 4 { + chars.pop_front(); + } + chars.push_back(char); + if i >= 3 { + let char_set: HashSet = chars.iter().map(char::clone).collect(); + if char_set.len() == 4 { + return i; + } + } + } + + panic!("Couldn't find a start marker!") +} diff --git a/src/lib.rs b/src/lib.rs index 3655dcc..dc5c659 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,4 +3,5 @@ pub mod day2; pub mod day3; pub mod day4; pub mod day5; +pub mod day6; pub mod util;