-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.rs
35 lines (30 loc) · 978 Bytes
/
day6.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::collections::{HashSet, VecDeque};
fn subroutine<const L: usize>(input: &str) -> Option<usize> {
// find the index of the first time `L` different characters appear after each other
let mut sequence = VecDeque::from(vec![' '; L]);
for (i, c) in input.chars().enumerate() {
assert_eq!(sequence.len(), L);
sequence.pop_front();
sequence.push_back(c);
// check if all characters are different
let set: HashSet<_> = sequence.iter().collect();
if set.len() == L {
dbg!(set);
return Some(i + 1);
}
}
None
}
fn part1(input: &str) -> String {
let index = subroutine::<4>(input).unwrap();
index.to_string()
}
fn part2(input: &str) -> String {
let index = subroutine::<14>(input).unwrap();
index.to_string()
}
fn main() {
let input = include_str!("../input/day6.txt");
println!("Part 1: {}", part1(input));
println!("Part 2: {}", part2(input));
}