AOC2022/rust/1/main.rs
Scramble 0e78baf6bc Fixed bug where it would ignore last elf.
Also did fmt, and forgot to separate the commits so now the diff is ugly.
2022-12-02 12:36:48 +00:00

29 lines
605 B
Rust

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