-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
59 lines (46 loc) · 1.27 KB
/
mod.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
57
58
59
use crate::utils::ParseError;
use std::{collections::HashSet, num::ParseIntError};
#[aoc_generator(day06)]
pub fn input_generator(input: &str) -> Result<Vec<char>, ParseIntError> {
Ok(input.chars().collect::<Vec<_>>())
}
fn solve(input: &[char], length: usize) -> usize {
let mut result = length;
for w in input.windows(length) {
let h: HashSet<char> = HashSet::from_iter(w.iter().cloned());
if h.len() == length {
break;
}
result += 1;
}
result
}
#[aoc(day06, part1)]
pub fn solve_part1(input: &[char]) -> Result<usize, ParseError> {
Ok(solve(input, 4))
}
#[aoc(day06, part2)]
pub fn solve_part2(input: &[char]) -> Result<usize, ParseError> {
Ok(solve(input, 14))
}
#[cfg(test)]
mod test {
use super::*;
use crate::utils::ParseError;
fn sample() -> &'static str {
"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"
}
fn input() -> Result<Vec<char>, ParseError> {
Ok(input_generator(sample())?)
}
#[test]
fn part1_sample() -> Result<(), ParseError> {
let data = input()?;
Ok(assert_eq!(10, solve_part1(&data)?))
}
#[test]
fn part2_sample() -> Result<(), ParseError> {
let data = input()?;
Ok(assert_eq!(29, solve_part2(&data)?))
}
}