summaryrefslogtreecommitdiff
path: root/src/bin/day1.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2022-12-01 20:30:21 +0100
committerReiner Herrmann <reiner@reiner-h.de>2022-12-01 20:30:21 +0100
commit956a62f94ec913fda4a2e7e15c47cb7e07c44de5 (patch)
tree8b1e20181127d11494d720ce743ffe9daa08e255 /src/bin/day1.rs
parent422145f1d54f4706e047fb6bfe3ea35ef30e4c3a (diff)
day1
Diffstat (limited to 'src/bin/day1.rs')
-rw-r--r--src/bin/day1.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/bin/day1.rs b/src/bin/day1.rs
new file mode 100644
index 0000000..93d21d6
--- /dev/null
+++ b/src/bin/day1.rs
@@ -0,0 +1,51 @@
+static DAY: u8 = 1;
+
+fn main() {
+ let input = advent::read_lines(DAY);
+ println!("{DAY}a: {}", most_calories_elf(&input, 1));
+ println!("{DAY}b: {}", most_calories_elf(&input, 3));
+}
+
+fn most_calories_elf(input: &[String], elf_count: usize) -> u32 {
+ let mut input = input.to_vec();
+ let mut calories = Vec::new();
+ let mut current = 0;
+ input.push("".to_string());
+ for line in input {
+ if line.as_str().is_empty() {
+ calories.push(current);
+ current = 0;
+ continue;
+ }
+ current += line.parse::<u32>().unwrap();
+ }
+ calories.sort();
+ calories.iter().rev().take(elf_count).sum()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ let input = [
+ "1000".to_string(),
+ "2000".to_string(),
+ "3000".to_string(),
+ "".to_string(),
+ "4000".to_string(),
+ "".to_string(),
+ "5000".to_string(),
+ "6000".to_string(),
+ "".to_string(),
+ "7000".to_string(),
+ "8000".to_string(),
+ "9000".to_string(),
+ "".to_string(),
+ "10000".to_string(),
+ ];
+ assert_eq!(most_calories_elf(&input, 1), 24000);
+ assert_eq!(most_calories_elf(&input, 3), 45000);
+ }
+}