summaryrefslogtreecommitdiff
path: root/aoc22/day1/src/main.rs
diff options
context:
space:
mode:
authormathiasmagnusson <mathiasmagnussons@gmail.com>2022-12-09 18:00:02 +0100
committermathiasmagnusson <mathiasmagnussons@gmail.com>2022-12-09 18:00:02 +0100
commite41e6c8bc72e3300a0fa137f198454341bc315b1 (patch)
treea26e34327b7e68adf9b1dc802a897b9b0626de86 /aoc22/day1/src/main.rs
parent444d543b1af11ff8718cf07c690944ddacb29105 (diff)
downloadprogramming-problem-solving-e41e6c8bc72e3300a0fa137f198454341bc315b1.tar.gz
Add aoc
Diffstat (limited to 'aoc22/day1/src/main.rs')
-rw-r--r--aoc22/day1/src/main.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/aoc22/day1/src/main.rs b/aoc22/day1/src/main.rs
new file mode 100644
index 0000000..1754e1b
--- /dev/null
+++ b/aoc22/day1/src/main.rs
@@ -0,0 +1,38 @@
+use std::collections::BinaryHeap;
+
+fn main() {
+ let input = lib::read_input(1);
+
+ part1(&input);
+ part2(&input);
+}
+
+fn part1(input: &str) {
+ let ans: usize = input
+ .split("\n\n")
+ .map(|elf| {
+ elf.split("\n")
+ .flat_map(|item| item.parse::<usize>().ok())
+ .sum()
+ })
+ .max()
+ .unwrap();
+
+ println!("{}", ans);
+}
+
+fn part2(input: &str) {
+ let mut heap: BinaryHeap<usize> = input
+ .split("\n\n")
+ .map(|elf| {
+ elf.split("\n")
+ .flat_map(|item| item.parse::<usize>().ok())
+ .sum()
+ })
+ .collect();
+
+ println!(
+ "{}",
+ heap.pop().unwrap() + heap.pop().unwrap() + heap.pop().unwrap()
+ );
+}