56 lines
1.2 KiB
Rust
56 lines
1.2 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, 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
|
|
}
|