dcb22a8a0c
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.
78 lines
1.8 KiB
Rust
78 lines
1.8 KiB
Rust
use aoc_runner_derive::{aoc, aoc_generator};
|
|
|
|
use itertools::Itertools;
|
|
|
|
#[aoc_generator(day1)]
|
|
pub fn parse_measurements(input: &str) -> Vec<usize> {
|
|
input
|
|
.split('\n')
|
|
.map(|input| input.parse().unwrap())
|
|
.collect()
|
|
}
|
|
|
|
#[aoc(day1, part1)]
|
|
pub fn count_increases_indexed(input: &[usize]) -> usize {
|
|
let mut count = 0;
|
|
for i in 1..input.len() {
|
|
if input[i - 1] < input[i] {
|
|
count += 1;
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
#[aoc(day1, part1, tuple_windows)]
|
|
pub fn count_increases_tuple_windows(input: &[usize]) -> usize {
|
|
let mut count = 0;
|
|
for (a, b) in input.iter().tuple_windows() {
|
|
if a < b {
|
|
count += 1;
|
|
}
|
|
}
|
|
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;
|
|
for i in 3..input.len() {
|
|
if input[i - 3] + input[i - 2] + input[i - 1] < input[i - 2] + input[i - 1] + input[i] {
|
|
count += 1;
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
#[aoc(day1, part2, tuple_windows)]
|
|
pub fn count_sliding_increases_tuple_windows(input: &[usize]) -> usize {
|
|
let mut count = 0;
|
|
for (a, b, c, d) in input.iter().tuple_windows() {
|
|
if a + b + c < b + c + d {
|
|
count += 1;
|
|
}
|
|
}
|
|
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
|
|
}
|