From d4ab42872dcba58750fe5e8d7a6a74dcf782e6c3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 1 Dec 2022 15:35:51 -0500 Subject: [PATCH] Make the code a bit more functional --- day1/src/main.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/day1/src/main.rs b/day1/src/main.rs index e31b1b9..d5c75ea 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -14,29 +14,25 @@ fn get_elves(raw: &str) -> Vec> { } fn get_elf_totals(elves: &Vec>) -> Vec { - let mut totals: Vec = 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> { let file_str = fs::read_to_string("input.txt")?; let elves = get_elves(&file_str); + let mut totals: Vec = 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];