1
0
Fork 0

Second star.

This commit is contained in:
Vivianne 2022-11-30 22:19:44 -08:00
parent 019dc4d0ce
commit cbaa0a3591
1 changed files with 15 additions and 3 deletions

View File

@ -1,6 +1,7 @@
use std::env;
use std::fs;
use std::io::{self, BufRead};
use std::collections::BinaryHeap;
fn main() {
let args: Vec<String> = env::args().collect();
@ -15,15 +16,26 @@ fn main() {
let file = fs::File::open(filename).unwrap();
let lines = io::BufReader::new(file).lines();
let mut max_cals: u32 = 0;
let mut heap = BinaryHeap::new();
let mut cur_cals: u32 = 0;
for l in lines {
if let Ok(cals) = l.unwrap().parse::<u32>() {
cur_cals += cals;
} else {
max_cals = max_cals.max(cur_cals);
println!("new cals: {}, max so far: {}", cur_cals, max_cals);
heap.push(cur_cals);
println!("new cals: {}, max so far: {}", cur_cals, heap.peek().unwrap());
cur_cals = 0;
};
}
if cur_cals > 0 {
heap.push(cur_cals);
}
let mut vec = heap.into_sorted_vec();
vec.dedup();
let total: u32 = vec.iter().rev().take(3).sum();
println!("calories of top 3 are: {}", total);
}