Day 1, puzzle 1
This commit is contained in:
28
src/d1p1.rs
Normal file
28
src/d1p1.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user