some formatting and some optimizations

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-12 16:03:42 -05:00
parent 4ef68d5f36
commit b85cc13465
3 changed files with 47 additions and 47 deletions

View File

@@ -1,25 +1,22 @@
use std::fs::File;
use std::io::{prelude::*, BufReader};
fn main() -> std::io::Result<()>{
fn main() -> std::io::Result<()> {
let file = File::open("input")?;
let reader = BufReader::new(file);
let mut elves = reader.lines()
.fold(Vec::new(), |mut acc, line| {
let line = line.unwrap();
//empty lines mean new elf
if line.is_empty() || acc.is_empty() {
acc.push(0_u64);
}
let mut elves = reader.lines().fold(vec![0_u64], |mut acc, line| {
let line = line.unwrap();
//empty lines mean new elf
if line.is_empty() {
acc.push(0_u64);
} else {
// the first time through is an edge case preventing an else here
if ! line.is_empty() {
let last = acc.last_mut().unwrap();
*last += line.parse::<u64>().unwrap();
}
acc
});
let last = acc.last_mut().unwrap();
*last += line.parse::<u64>().unwrap();
}
acc
});
//order the elves since we don't care about position anymore
elves.sort();
@@ -29,7 +26,7 @@ fn main() -> std::io::Result<()>{
println!("Part 1: {}", elves[max]);
//Part 2 is get the sum of the largest 3
let counts: u64 = elves[(max-2)..].iter().sum();
let counts: u64 = elves[(max - 2)..].iter().sum();
println!("Part 2: {counts}");
Ok(())