From: Greg Burri Date: Sat, 4 Dec 2021 16:42:34 +0000 (+0100) Subject: Use of sum from std::Iter::Iterator X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=88bae683c564a44d2012dfb703e53c15ff103c2e;p=advent_of_code_2021.git Use of sum from std::Iter::Iterator --- 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; } }