From 88bae683c564a44d2012dfb703e53c15ff103c2e Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 4 Dec 2021 17:42:34 +0100 Subject: [PATCH] Use of sum from std::Iter::Iterator --- src/day01.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/day01.rs b/src/day01.rs index d4f1889..d2b4a8f 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,16 +1,9 @@ +use std::iter::Iterator; + pub fn count_number_of_decreased_values(report: &[i32], window_size: usize) -> i32 { let mut n = 0; - - let sum = |i: usize| -> i32 { - let mut s = 0; - for j in i..i+window_size { - s += report[j]; - } - s - }; - for i in 0..report.len() - window_size { - if sum(i+1) > sum(i) { + if report[i + 1..i + window_size + 1].iter().sum::() > report[i..i + window_size].iter().sum::() { n += 1; } } -- 2.45.2