Make the code a bit more functional

This commit is contained in:
Timothy Warren 2022-12-01 15:35:51 -05:00
parent 6ec6ac4200
commit d4ab42872d
1 changed files with 10 additions and 14 deletions

View File

@ -14,29 +14,25 @@ fn get_elves(raw: &str) -> Vec<Vec<u32>> {
}
fn get_elf_totals(elves: &Vec<Vec<u32>>) -> Vec<u32> {
let mut totals: Vec<u32> = Vec::new();
for elf in elves {
let sum = elf
.clone()
.into_iter()
.reduce(|accum, item| accum + item)
.unwrap();
totals.push(sum)
}
totals
elves
.clone()
.into_iter()
.map(|elf| {
elf.into_iter()
.reduce(|accum, item| accum + item)
.unwrap()
})
.collect()
}
fn main() -> Result<(), Box<dyn Error>> {
let file_str = fs::read_to_string("input.txt")?;
let elves = get_elves(&file_str);
let mut totals: Vec<u32> = get_elf_totals(&elves);
totals.sort();
totals.reverse();
// let most = totals.iter().max().unwrap();
let most = totals[0];
let top3 = totals[0] + totals[1] + totals[2];