38 lines
830 B
Rust
38 lines
830 B
Rust
|
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)
|
||
|
}
|