2022-6
This commit is contained in:
parent
b7a13cbc27
commit
61268dc5ec
1 changed files with 44 additions and 0 deletions
44
src/bin/2022-6.rs
Normal file
44
src/bin/2022-6.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let input = aoc::input!();
|
||||
println!("part 1: {}", part1(&input));
|
||||
println!("part 1: {}", part2(&input));
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> usize {
|
||||
input
|
||||
.as_bytes()
|
||||
.windows(4)
|
||||
.position(|w| w.iter().all_unique())
|
||||
.unwrap()
|
||||
+ 4
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> usize {
|
||||
input
|
||||
.as_bytes()
|
||||
.windows(14)
|
||||
.position(|w| w.iter().all_unique())
|
||||
.unwrap()
|
||||
+ 14
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn examples() {
|
||||
assert_eq!(part1("bvwbjplbgvbhsrlpgdmjqwftvncz"), 5);
|
||||
assert_eq!(part1("nppdvjthqldpwncqszvftbrmjlhg"), 6);
|
||||
assert_eq!(part1("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), 10);
|
||||
assert_eq!(part1("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), 11);
|
||||
|
||||
assert_eq!(part2("mjqjpqmgbljsphdztnvjfqwrcgsmlb"), 19);
|
||||
assert_eq!(part2("bvwbjplbgvbhsrlpgdmjqwftvncz"), 23);
|
||||
assert_eq!(part2("nppdvjthqldpwncqszvftbrmjlhg"), 23);
|
||||
assert_eq!(part2("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), 29);
|
||||
assert_eq!(part2("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), 26);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue