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