Browse Source

Itertools for smaller code

main
Julio Biason 2 years ago
parent
commit
9c60991d6c
  1. 16
      day2/Cargo.lock
  2. 1
      day2/Cargo.toml
  3. 13
      day2/src/main.rs

16
day2/Cargo.lock generated

@ -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"

1
day2/Cargo.toml

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
clap = "4.0.29"
itertools = "0.10.5"

13
day2/src/main.rs

@ -6,6 +6,8 @@ use std::io::Lines;
use clap::Arg;
use clap::Command;
use itertools::Itertools;
struct Accumulator<B> {
lines: Lines<B>,
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::<Vec<u32>>();
calories.sort_by(|a, b| b.cmp(a));
println!("Sum: {:?}", calories.iter().take(3).sum::<u32>())
let calories = reader
.lines()
.elf()
.sorted_by(|a, b| b.cmp(a))
.take(3)
.sum::<u32>();
println!("Sum: {:?}", calories)
}

Loading…
Cancel
Save