diff --git a/Cargo.lock b/Cargo.lock index 00ec0ec..b0ed567 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,21 @@ version = 3 [[package]] name = "aoc_2022" version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] diff --git a/Cargo.toml b/Cargo.toml index 8ec5b28..1f6084f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +itertools = "0.10.5" diff --git a/src/day1.rs b/src/day1.rs index 7d3ef6c..b9caa48 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::fs; pub fn run() { @@ -11,33 +12,30 @@ pub fn run() { fn part1(food_input: &str) -> usize { food_input .split("\n\n") - .collect::>() .into_iter() .map(|elf| { elf.split_whitespace() .map(|food| food.parse::().unwrap()) .sum() }) - .collect::>() .into_iter() .max() .unwrap() } fn part2(food_input: &str) -> usize { - let mut elves_calories = food_input + food_input .split("\n\n") .collect::>() .into_iter() .map(|elf| { elf.split_whitespace() .map(|food| food.parse::().unwrap()) - .sum() + .sum::() }) - .collect::>(); - - elves_calories.sort_by(|l, r| r.cmp(l)); - - elves_calories[..3].iter().sum::() + .sorted() + .rev() + .take(3) + .sum() } #[cfg(test)]