From 5055786d3db80e45a5c3f128f41866ea78865c45 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 1 Dec 2022 17:43:04 +0300 Subject: 2022, day1: done --- 2022/day1/part2/Cargo.toml | 9 +++++++++ 2022/day1/part2/example | 14 ++++++++++++++ 2022/day1/part2/src/main.rs | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 2022/day1/part2/Cargo.toml create mode 100644 2022/day1/part2/example create mode 100644 2022/day1/part2/src/main.rs (limited to '2022/day1/part2') diff --git a/2022/day1/part2/Cargo.toml b/2022/day1/part2/Cargo.toml new file mode 100644 index 0000000..69fa504 --- /dev/null +++ b/2022/day1/part2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "part2" +version = "0.1.0" +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/2022/day1/part2/example b/2022/day1/part2/example new file mode 100644 index 0000000..2094f91 --- /dev/null +++ b/2022/day1/part2/example @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 diff --git a/2022/day1/part2/src/main.rs b/2022/day1/part2/src/main.rs new file mode 100644 index 0000000..2a65d1a --- /dev/null +++ b/2022/day1/part2/src/main.rs @@ -0,0 +1,21 @@ +use itertools::Itertools; + +fn main() { + let input: String = include_str!("../../input").to_string(); + let mut calories: Vec = Vec::new(); + + for (key, group) in &input.lines().group_by(|line| *line != "") { + if key { + calories.push(group.into_iter().map(|i| i.parse::().unwrap()).sum()); + } + } + + println!( + "{}", + calories + .iter() + .sorted_by(|a, b| Ord::cmp(b, a)) + .take(3) + .sum::() + ); +} -- cgit v1.2.3-70-g09d2