Fixed bug where it would ignore last elf.

Also did fmt, and forgot to separate the commits so now the diff is ugly.
This commit is contained in:
Scramble 2022-12-02 12:36:48 +00:00
parent cd57ddfcdb
commit 0e78baf6bc
3 changed files with 8 additions and 6 deletions

View File

@ -3,5 +3,3 @@ I just overwrote the first puzzle's solution with the second's, I'll have to be
First puzzle was just a single `let max: i32 = 0` instead of the `top` Vec, so same thing just a bit simpler
For the second part I keep the top 3 values and add a new one whenever it's at least greater than the smallest one, and keep those 3 values sorted so the smallest one is always first
Also I have a bug because the last elf is never taken into account, I should just append an empty line to the iterator

Binary file not shown.

View File

@ -1,4 +1,8 @@
use std::{fs::File, io::{BufReader, BufRead}};
use std::{
fs::File,
io::{BufRead, BufReader},
iter,
};
fn main() -> () {
let path = "input.txt";
@ -9,7 +13,7 @@ fn main() -> () {
let mut top = [0; 3];
let mut cur = 0;
for line in buf.lines() {
for line in buf.lines().chain(iter::once(Ok(String::from("")))) {
let line = line.unwrap();
if line == "" {
top[0] = top[0].max(cur);
@ -19,6 +23,6 @@ fn main() -> () {
cur += line.parse::<i32>().unwrap();
}
}
println!("{}", top.iter().fold(0, |acc, el| {acc + el}));
println!("{}", top.iter().fold(0, |acc, el| { acc + el }));
}