add slice window variants for day 1
because it's based on slices, it's more performant than the tuple variant, but it's more error prone, because the bounds checking happens at runtime instead.
This commit is contained in:
parent
be518eca39
commit
dcb22a8a0c
|
@ -32,6 +32,17 @@ pub fn count_increases_tuple_windows(input: &[usize]) -> usize {
|
|||
count
|
||||
}
|
||||
|
||||
#[aoc(day1, part1, slice_windows)]
|
||||
pub fn count_increases_slice_windows(input: &[usize]) -> usize {
|
||||
let mut count = 0;
|
||||
for window in input.windows(2) {
|
||||
if window[0] < window[1] {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
pub fn count_sliding_increases_indexed(input: &[usize]) -> usize {
|
||||
let mut count = 0;
|
||||
|
@ -53,3 +64,14 @@ pub fn count_sliding_increases_tuple_windows(input: &[usize]) -> usize {
|
|||
}
|
||||
count
|
||||
}
|
||||
|
||||
#[aoc(day1, part2, slice_windows)]
|
||||
pub fn count_sliding_increases_slice_windows(input: &[usize]) -> usize {
|
||||
let mut count = 0;
|
||||
for w in input.windows(4) {
|
||||
if w[0] + w[1] + w[2] < w[1] + w[2] + w[3] {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue