-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rs
56 lines (46 loc) · 1.05 KB
/
06.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![feature(test)]
type Input = Vec<usize>;
fn setup(input: &str) -> Input {
input.trim().bytes().map(|x| (x - b'a') as _).collect()
}
#[derive(Default)]
struct Counter {
counter: [usize; 26],
dup: usize,
}
impl Counter {
pub fn inc(&mut self, i: usize) {
self.counter[i] += 1;
if self.counter[i] == 2 {
self.dup += 1;
}
}
pub fn dec(&mut self, i: usize) {
self.counter[i] -= 1;
if self.counter[i] == 1 {
self.dup -= 1;
}
}
pub fn is_distinct(&self) -> bool {
self.dup == 0
}
}
fn solve(input: &Input, n: usize) -> usize {
let mut counter = Counter::default();
(0..n).for_each(|i| counter.inc(input[i]));
for i in n..input.len() {
if counter.is_distinct() {
return i;
}
counter.inc(input[i]);
counter.dec(input[i - n]);
}
panic!()
}
fn part1(input: &Input) -> usize {
solve(input, 4)
}
fn part2(input: &Input) -> usize {
solve(input, 14)
}
aoc::main!(2022, 6, ex: 1, 2, 3, 4, 5);