Day 6, puzzle 1
This commit is contained in:
parent
0506dc670a
commit
e3c356b1ac
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
use std::collections::{HashSet, VecDeque};
|
||||
|
||||
pub fn parse_datastream(stream: &str) -> usize {
|
||||
let mut chars: VecDeque<char> = 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<char> = chars.iter().map(char::clone).collect();
|
||||
if char_set.len() == 4 {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Couldn't find a start marker!")
|
||||
}
|
|
@ -3,4 +3,5 @@ pub mod day2;
|
|||
pub mod day3;
|
||||
pub mod day4;
|
||||
pub mod day5;
|
||||
pub mod day6;
|
||||
pub mod util;
|
||||
|
|
Loading…
Reference in New Issue