2021: day01 part 1 & part 2

This commit is contained in:
Antoine Martin 2021-12-01 07:30:10 +01:00
parent 0272809c82
commit 3e991d5ac1
4 changed files with 2048 additions and 3 deletions

2000
aoc2021/input/day01.txt Normal file

File diff suppressed because it is too large Load diff

45
aoc2021/src/day01.rs Normal file
View file

@ -0,0 +1,45 @@
use std::fmt::Write;
use anyhow::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<usize> {
let measurements = input
.lines()
.map(|line| line.parse::<u64>().map_err(anyhow::Error::new))
.collect::<Result<Vec<_>>>()?;
Ok(measurements.windows(2).filter(|w| w[0] < w[1]).count())
}
fn part2(input: &str) -> Result<usize> {
let measurements = input
.lines()
.map(|line| line.parse::<u64>().map_err(anyhow::Error::new))
.collect::<Result<Vec<_>>>()?;
let mut increases = 0;
let mut prev: Option<u64> = None;
for window in measurements.windows(3) {
let sum = window.iter().sum();
if let Some(prev) = prev {
if prev < sum {
increases += 1;
}
}
prev = Some(sum);
}
Ok(increases)
}

View file

@ -1,3 +1,3 @@
#![warn(clippy::explicit_iter_loop, clippy::redundant_closure_for_method_calls)]
pub mod day00;
pub mod day01;

View file

@ -2,10 +2,10 @@ use anyhow::Result;
use aoc::DayFunc;
use aoc2021::day00;
use aoc2021::day01;
fn main() -> Result<()> {
let days: &[DayFunc] = &[day00::run];
let days: &[DayFunc] = &[day01::run];
aoc::run(days)
}