Day 1, puzzle 1

This commit is contained in:
jazzpi
2022-12-12 19:51:33 +01:00
commit 1a5b2ddfdd
4 changed files with 48 additions and 0 deletions

28
src/d1p1.rs Normal file
View File

@ -0,0 +1,28 @@
use std::io;
fn main() {
println!("What's the calorie list?");
let lines = io::stdin().lines();
let mut was_empty = false;
let mut elves = vec![0];
for line in lines {
let line_s = line.unwrap();
if line_s.is_empty() {
if was_empty {
break;
}
elves.push(0);
was_empty = true;
continue;
} else {
was_empty = false;
}
let calories: u32 = line_s.parse().expect("Wanted a number");
let prev_calories = elves.pop().unwrap();
elves.push(prev_calories + calories);
}
let max = elves.iter().max().unwrap();
println!("The elf with the most calories is carrying {} cal", max);
}