Skip to content

Instantly share code, notes, and snippets.

@bojan88
Created December 1, 2021 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojan88/d2ae7abafbae987cca892ca1d43f50a7 to your computer and use it in GitHub Desktop.
Save bojan88/d2ae7abafbae987cca892ca1d43f50a7 to your computer and use it in GitHub Desktop.
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