back adding day 1:

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-12 08:27:00 -05:00
parent b00279e804
commit 5522d57c90
4 changed files with 2291 additions and 0 deletions

36
day1/src/main.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::fs::File;
use std::io::{prelude::*, BufReader};
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);
}
// 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
});
//order the elves since we don't care about position anymore
elves.sort();
let max = elves.len() - 1;
//part 1 is get the max
println!("Part 1: {}", elves[max]);
//Part 2 is get the sum of the largest 3
let counts: u64 = elves[(max-2)..].iter().sum();
println!("Part 2: {counts}");
Ok(())
}