day 6
This commit is contained in:
parent
a183a6dd1a
commit
094ab63ceb
43
src/days/day6.rs
Normal file
43
src/days/day6.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
use aoc_runner_derive::aoc;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
#[aoc(day6, part1, tuple_windows)]
|
||||||
|
fn part1_tuple_windows(input: &str) -> usize {
|
||||||
|
input
|
||||||
|
.char_indices()
|
||||||
|
.tuple_windows()
|
||||||
|
.find_map(|((_, a), (_, b), (_, c), (i, d))| {
|
||||||
|
if [a, b, c, d].iter().duplicates().count() == 0 {
|
||||||
|
Some(i + d.len_utf8())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day6, part1, indexing)]
|
||||||
|
fn part1_indexing(input: &str) -> usize {
|
||||||
|
const N: usize = 4;
|
||||||
|
|
||||||
|
for i in N..=input.len() {
|
||||||
|
let s = &input[i - N..i];
|
||||||
|
if s.chars().duplicates().count() == 0 {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usize::MAX
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day6, part2)]
|
||||||
|
fn part2(input: &str) -> usize {
|
||||||
|
const N: usize = 14;
|
||||||
|
|
||||||
|
for i in N..=input.len() {
|
||||||
|
let s = &input[i - N..i];
|
||||||
|
if s.chars().duplicates().count() == 0 {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usize::MAX
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ mod days {
|
||||||
mod day3;
|
mod day3;
|
||||||
mod day4;
|
mod day4;
|
||||||
mod day5;
|
mod day5;
|
||||||
|
mod day6;
|
||||||
}
|
}
|
||||||
|
|
||||||
aoc_lib! { year = 2022 }
|
aoc_lib! { year = 2022 }
|
||||||
|
|
Loading…
Reference in a new issue