Make the code a bit more functional
This commit is contained in:
parent
6ec6ac4200
commit
d4ab42872d
@ -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];
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user