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

42 lines
917 B
Rust
Raw Permalink Normal View History

2024-01-14 21:04:10 +00:00
//! Day 01 of 2021.
use crate::prelude::*;
2021-12-01 11:50:38 +00:00
2024-01-14 21:04:10 +00:00
/// Get the solution for day 01 of 2021.
pub fn solution() -> Solution {
Solution::new(Day::new(1, 2021), part_1, part_2).with_expected(1451, 1395)
2021-12-01 11:50:38 +00:00
}
2024-01-14 21:04:10 +00:00
/// The logic to solve part one.
fn part_1(input: &str) -> Result<String> {
2021-12-01 11:50:38 +00:00
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(previous, next)| next > previous)
.count()
.to_string(),
2021-12-01 11:50:38 +00:00
)
}
2024-01-14 21:04:10 +00:00
/// The logic to solve part two.
fn part_2(input: &str) -> Result<String> {
2021-12-01 11:50:38 +00:00
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(a, b, c, d)| b + c + d > a + b + c)
.count()
.to_string(),
2021-12-01 11:50:38 +00:00
)
}
2024-01-14 21:04:10 +00:00
/// Parse the measurements from the puzzle input.
2021-12-01 11:50:38 +00:00
fn parse_measurements(input: &str) -> Result<Vec<i32>> {
input
.lines()
.map(|line| line.parse::<i32>())
.collect::<Result<_, _>>()
.map_err(Into::into)
}