From: Greg Burri Date: Thu, 1 Dec 2022 23:31:53 +0000 (+0100) Subject: Use Reverse + k_smallest (code simplification) X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=4efcec7be9d74d792f70cfba682bac8290f00832;p=advent_of_code_2022.git Use Reverse + k_smallest (code simplification) --- diff --git a/src/day01.rs b/src/day01.rs index c1875bb..d88d000 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,4 +1,6 @@ -use std::{iter::Iterator, io::BufRead, ops::AddAssign}; +use std::{iter::Iterator, io::BufRead, ops::AddAssign, cmp::Reverse}; + +use itertools::*; pub fn read_calories(reader: R) -> Vec where @@ -22,9 +24,7 @@ pub fn get_most_calories(calories: &[i64]) -> i64 { } pub fn get_sum_most_three_calories(calories: &[i64]) -> i64 { - let mut calories = Vec::from(calories); - calories.sort_by(|a, b| b.cmp(a)); - calories.iter().take(3).sum() + calories.iter().map(Reverse).k_smallest(3).map(|n| n.0).sum() }