advent/2021/src/day1.rs
2021-12-07 10:26:22 -08:00

30 lines
871 B
Rust

use color_eyre::eyre;
use crate::lib::ParseLines;
pub fn run(data: &str) -> eyre::Result<(i32, i32)> {
let mut single_increases = 0;
let mut window_increases = 0;
let mut prev = (None, None, None);
for res in data.parse_lines::<i32>() {
let n = res?;
if let Some(p) = prev.2 {
// apparently we can't combine "if let" and regular boolean conditions
if n > p {
single_increases += 1;
}
}
if let (Some(first), Some(second), Some(third)) = prev {
if (n + second + third) > (first + second + third) {
window_increases += 1;
}
}
// would have forgotten this if not for the warning triggered by "let mut" without mutation
prev = (prev.1, prev.2, Some(n));
}
Ok((single_increases, window_increases))
}