From: Greg Burri Date: Thu, 1 Dec 2022 23:31:53 +0000 (+0100) Subject: Use Reverse + k_smallest (code simplification) X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=b6c8dd434b79b46908a45294d8b9eea973eaf06f;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() }