From 1d39665c5ae23ddb3c5648d4844d15255d40b566 Mon Sep 17 00:00:00 2001 From: cos Date: Wed, 8 Dec 2021 21:53:26 +0100 Subject: Minor simplification to 2021day01 --- 2021/rust/day01/src/main.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/2021/rust/day01/src/main.rs b/2021/rust/day01/src/main.rs index 559f98c..d0ac932 100644 --- a/2021/rust/day01/src/main.rs +++ b/2021/rust/day01/src/main.rs @@ -32,7 +32,7 @@ enum Change { } fn count_increases<'a, I: IntoIterator>(values: I) -> usize { - let changes: Vec = values.into_iter().scan(None, |state, val| { + values.into_iter().scan(None, |state, val| { let next = match state { None => Change::NoPrevVal, Some(prev) if val < *prev => Change::Decreased, @@ -42,11 +42,7 @@ fn count_increases<'a, I: IntoIterator>(values: I) -> usize { }; *state = Some(val); Some(next) - }).collect(); - - changes.iter().fold(0, |count, change| - if *change == Change::Increased { count + 1 } else { count } - ) + }).filter(|change| change == &Change::Increased).count() } fn part1<'a, I: IntoIterator>(input: I) -> Result { -- cgit v1.2.3