From 0900b54c13bb4a2d8ccca60155991a1fe32a1735 Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Wed, 30 Nov 2022 23:25:39 -0800 Subject: [PATCH] Switch to nightly and use BTreeSet instead! --- rust-toolchain | 1 + src/bin/p01.rs | 15 ++++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) create mode 100644 rust-toolchain diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..07ade69 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly \ No newline at end of file diff --git a/src/bin/p01.rs b/src/bin/p01.rs index 3cf10c3..46f5d2d 100644 --- a/src/bin/p01.rs +++ b/src/bin/p01.rs @@ -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::() { @@ -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); }