From 9c60991d6cb00d9cbe6d9b4ce32ed4e1b10d78cc Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 8 Dec 2022 09:42:04 -0300 Subject: [PATCH] Itertools for smaller code --- day2/Cargo.lock | 16 ++++++++++++++++ day2/Cargo.toml | 1 + day2/src/main.rs | 13 ++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/day2/Cargo.lock b/day2/Cargo.lock index 6da394b..8f068b0 100644 --- a/day2/Cargo.lock +++ b/day2/Cargo.lock @@ -41,8 +41,15 @@ name = "day2" version = "0.1.0" dependencies = [ "clap", + "itertools", ] +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + [[package]] name = "errno" version = "0.2.8" @@ -95,6 +102,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "libc" version = "0.2.138" diff --git a/day2/Cargo.toml b/day2/Cargo.toml index b6d6876..4455d1a 100644 --- a/day2/Cargo.toml +++ b/day2/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] clap = "4.0.29" +itertools = "0.10.5" diff --git a/day2/src/main.rs b/day2/src/main.rs index c3cca6d..293753b 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -6,6 +6,8 @@ use std::io::Lines; use clap::Arg; use clap::Command; +use itertools::Itertools; + struct Accumulator { lines: Lines, current: u32, @@ -52,7 +54,12 @@ fn main() { let file = File::open(filename).expect("Can't read file"); let reader = BufReader::new(file); - let mut calories = reader.lines().elf().collect::>(); - calories.sort_by(|a, b| b.cmp(a)); - println!("Sum: {:?}", calories.iter().take(3).sum::()) + let calories = reader + .lines() + .elf() + .sorted_by(|a, b| b.cmp(a)) + .take(3) + .sum::(); + + println!("Sum: {:?}", calories) }