1
Fork 0
advent-of-code/source/year_2021/day_01/mod.rs

36 lines
739 B
Rust

use crate::prelude::*;
pub fn solution() -> Solution {
Solution::new(Day::new(1, 2021), part_1, part_2).with_expected(1451, 1395)
}
fn part_1(input: &str) -> Result<String> {
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(previous, next)| next > previous)
.count()
.to_string(),
)
}
fn part_2(input: &str) -> Result<String> {
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(a, b, c, d)| b + c + d > a + b + c)
.count()
.to_string(),
)
}
fn parse_measurements(input: &str) -> Result<Vec<i32>> {
input
.lines()
.map(|line| line.parse::<i32>())
.collect::<Result<_, _>>()
.map_err(Into::into)
}