Switch to nightly and use BTreeSet instead!
This commit is contained in:
parent
7bbba7414a
commit
0900b54c13
|
@ -0,0 +1 @@
|
|||
nightly
|
|
@ -1,4 +1,4 @@
|
|||
use std::collections::BinaryHeap;
|
||||
use std::collections::BTreeSet;
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
|
@ -7,7 +7,7 @@ fn main() {
|
|||
let file = fs::File::open(filename).expect("Can't open file");
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
|
||||
let mut heap = BinaryHeap::new();
|
||||
let mut set = BTreeSet::new();
|
||||
let mut cur_cals: u32 = 0;
|
||||
for l in lines {
|
||||
match l.expect("Can't read line.").parse::<u32>() {
|
||||
|
@ -15,11 +15,11 @@ fn main() {
|
|||
cur_cals += cals;
|
||||
}
|
||||
_ => {
|
||||
heap.push(cur_cals);
|
||||
set.insert(cur_cals);
|
||||
println!(
|
||||
"new cals: {}, max so far: {}",
|
||||
cur_cals,
|
||||
heap.peek().unwrap()
|
||||
set.last().unwrap()
|
||||
);
|
||||
cur_cals = 0;
|
||||
}
|
||||
|
@ -27,12 +27,9 @@ fn main() {
|
|||
}
|
||||
|
||||
if cur_cals > 0 {
|
||||
heap.push(cur_cals);
|
||||
set.insert(cur_cals);
|
||||
}
|
||||
|
||||
let mut vec = heap.into_sorted_vec();
|
||||
vec.dedup();
|
||||
|
||||
let total: u32 = vec.iter().rev().take(3).sum();
|
||||
let total: u32 = set.iter().rev().take(3).sum();
|
||||
println!("calories of top 3 are: {}", total);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue