Created
December 1, 2021 15:21
-
-
Save bojan88/d2ae7abafbae987cca892ca1d43f50a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use aoc_runner_derive::{aoc, aoc_generator, aoc_lib}; | |
use std::error::Error; | |
#[aoc_generator(day1)] | |
fn parse_input(input: &str) -> Result<Vec<usize>, Box<dyn Error>> { | |
Ok(input | |
.lines() | |
.map(|line| line.parse::<usize>().unwrap()) | |
.collect()) | |
} | |
fn count_increases(input: Vec<usize>) -> usize { | |
input | |
.iter() | |
.fold((usize::MAX, 0), |(prev, sum), &val| { | |
(val, sum + if val > prev { 1 } else { 0 }) | |
}) | |
.1 | |
} | |
#[aoc(day1, part1)] | |
pub fn solve_part1(input: &[usize]) -> usize { | |
count_increases(Vec::from(input)) | |
} | |
#[aoc(day1, part2)] | |
pub fn solve_part2(input: &[usize]) -> usize { | |
let measures = (0..input.len() - 2) | |
.map(|i| input[i..i + 3].iter().sum()) | |
.collect(); | |
count_increases(measures) | |
} | |
aoc_lib! { year = 2021 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment