fn main() {
let values: Vec<u64> = vec![9, 7, 6, 3, 4, 0, 1, 7, 3, 9];
let (_, groups): (Option<u64>, Vec<Vec<u64>>) = values
.into_iter()
.filter(|value| *value > 0)
.fold((None, vec![]), |(previous, mut groups), current| {
match previous {
Some(previous) if previous >= current => {
if let Some(last_group) = groups.last_mut() {
last_group.push(current);
} else {
groups.push(vec![current]);
}
}
_ => groups.push(vec![current]),
};
(Some(current), groups)
});
dbg!(&groups);
let sums: Vec<u64> = groups
.into_iter()
.map(|group| group.into_iter().sum())
.collect();
dbg!(&sums);
}
https://play.rust-lang.org/?version=stable&mode=de...