summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-12-06 22:05:49 +0100
committerSanto Cariotti <santo@dcariotti.me>2022-12-06 22:05:49 +0100
commitb23c4ab4c0669ee3af5dd9f2c4e33f51c776fc17 (patch)
treecb42a5f121d6c8fd5fa99712c82034a9849ff31b
parent0965ff1b6dca8ef6beb0f654716195be68bacdea (diff)
day 1 more readble
-rw-r--r--2022/day1/src/lib.rs12
1 files changed, 5 insertions, 7 deletions
diff --git a/2022/day1/src/lib.rs b/2022/day1/src/lib.rs
index 26df697..9459264 100644
--- a/2022/day1/src/lib.rs
+++ b/2022/day1/src/lib.rs
@@ -1,16 +1,16 @@
pub fn get_sums(input: &str) -> Vec<i32> {
- let groups: Vec<&str> = input.split("\n\n").collect::<Vec<&str>>();
- let data_groups = groups
+ let groups: Vec<&str> = input.split("\n\n").collect();
+ let data_groups: Vec<i32> = groups
.into_iter()
.map(|x| {
let numbers: i32 = x
- .split('\n')
+ .lines()
.map(|x| x.parse::<i32>().unwrap_or_default())
.sum();
return numbers;
})
- .collect::<Vec<i32>>();
+ .collect();
data_groups
}
@@ -25,9 +25,7 @@ pub fn part2(input: &str) -> i32 {
let mut sums = get_sums(input);
sums.sort();
- sums.reverse();
-
- sums[0] + sums[1] + sums[2]
+ sums.into_iter().rev().take(3).sum::<i32>()
}
#[cfg(test)]