From b6c8dd434b79b46908a45294d8b9eea973eaf06f Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Fri, 2 Dec 2022 00:31:53 +0100 Subject: [PATCH] Use Reverse + k_smallest (code simplification) --- src/day01.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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() } -- 2.45.2