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

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
for elf in elves {
let sum = elf
.clone() .clone()
.into_iter() .into_iter()
.map(|elf| {
elf.into_iter()
.reduce(|accum, item| accum + item) .reduce(|accum, item| accum + item)
.unwrap(); .unwrap()
})
totals.push(sum) .collect()
}
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];