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

38 lines
830 B
Rust
Raw Normal View History

2021-12-01 11:50:38 +00:00
use color_eyre::Result;
use itertools::Itertools;
pub fn solve() -> Result<()> {
let input_data = include_str!("../../data/day_01.txt").trim();
println!("Day 01 Part 1: {}", part_1(input_data)?);
println!("Day 01 Part 2: {}", part_2(input_data)?);
Ok(())
}
fn part_1(input: &str) -> Result<usize> {
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(previous, next)| next > previous)
.count(),
)
}
fn part_2(input: &str) -> Result<usize> {
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(a, b, c, d)| b + c + d > a + b + c)
.count(),
)
}
fn parse_measurements(input: &str) -> Result<Vec<i32>> {
input
.lines()
.map(|line| line.parse::<i32>())
.collect::<Result<_, _>>()
.map_err(Into::into)
}