1
0
Fork 0

Switch to nightly and use BTreeSet instead!

This commit is contained in:
Vivianne 2022-11-30 23:25:39 -08:00
parent 7bbba7414a
commit 0900b54c13
2 changed files with 7 additions and 9 deletions

1
rust-toolchain Normal file
View File

@ -0,0 +1 @@
nightly

View File

@ -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);
}