summaryrefslogtreecommitdiffstats
path: root/2022/day1/part2
diff options
context:
space:
mode:
authorYigit Sever2022-12-01 17:43:04 +0300
committerYigit Sever2022-12-01 17:43:04 +0300
commit5055786d3db80e45a5c3f128f41866ea78865c45 (patch)
tree19f8df50614798285ffdd8556e28bb7840c02c34 /2022/day1/part2
parent00731fd04963873b99e8a1d2571f27c87d8b7658 (diff)
downloadaoc-5055786d3db80e45a5c3f128f41866ea78865c45.tar.gz
aoc-5055786d3db80e45a5c3f128f41866ea78865c45.tar.bz2
aoc-5055786d3db80e45a5c3f128f41866ea78865c45.zip
2022, day1: done
Diffstat (limited to '2022/day1/part2')
-rw-r--r--2022/day1/part2/Cargo.toml9
-rw-r--r--2022/day1/part2/example14
-rw-r--r--2022/day1/part2/src/main.rs21
3 files changed, 44 insertions, 0 deletions
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 @@
1[package]
2name = "part2"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "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 @@
11000
22000
33000
4
54000
6
75000
86000
9
107000
118000
129000
13
1410000
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 @@
1use itertools::Itertools;
2
3fn main() {
4 let input: String = include_str!("../../input").to_string();
5 let mut calories: Vec<usize> = Vec::new();
6
7 for (key, group) in &input.lines().group_by(|line| *line != "") {
8 if key {
9 calories.push(group.into_iter().map(|i| i.parse::<usize>().unwrap()).sum());
10 }
11 }
12
13 println!(
14 "{}",
15 calories
16 .iter()
17 .sorted_by(|a, b| Ord::cmp(b, a))
18 .take(3)
19 .sum::<usize>()
20 );
21}