rust day 1 part 2

This commit is contained in:
Felix Pankratz 2022-12-01 21:11:17 +01:00
parent 475c8a02a3
commit a9c7c09d2b

36
2022/01/02.rs Executable file
View File

@ -0,0 +1,36 @@
use std::io;
fn main() -> io::Result<()> {
// read from stdin
let stdin = io::stdin();
let lines = stdin.lines();
let mut elves = Vec::new();
let mut c_sum = 0;
for line in lines {
let content = line.unwrap();
if content.is_empty() {
elves.push(c_sum);
c_sum = 0;
} else {
let new_val = match content.parse::<i32>() {
Ok(v) => v,
Err(_e) =>{ println!("Error parsing input"); 0 }
};
c_sum += new_val;
}
}
elves.sort();
elves.reverse();
let mut result = 0;
if elves.len() > 3 {
for n in 0..3 {
result += elves[n];
}
println!("{}", result);
} else {
println!("Not enough elves");
}
Ok(())
}