Complete part 2 of day 1
This commit is contained in:
parent
b6627e1e3b
commit
6ec6ac4200
@ -1,34 +1,47 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn get_elves(raw: &str) -> Vec<Vec<u32>> {
|
||||||
let file_str = fs::read_to_string("input.txt")?;
|
raw.split("\n\n")
|
||||||
let mut elves: Vec<Vec<u32>> = Vec::new();
|
.map(|raw_elf| {
|
||||||
|
raw_elf
|
||||||
|
.split('\n')
|
||||||
|
.filter(|value| value.len() > 0)
|
||||||
|
.map(move |value| value.parse::<u32>().unwrap())
|
||||||
|
.collect::<Vec<u32>>()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_elf_totals(elves: &Vec<Vec<u32>>) -> Vec<u32> {
|
||||||
let mut totals: Vec<u32> = Vec::new();
|
let mut totals: Vec<u32> = Vec::new();
|
||||||
|
|
||||||
for raw_elf in file_str.split("\n\n") {
|
for elf in elves {
|
||||||
let elf: Vec<u32> = raw_elf.split('\n')
|
|
||||||
.filter(|value| value.len() > 0)
|
|
||||||
.map(move |value| {
|
|
||||||
value.parse::<u32>().unwrap()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let sum = elf
|
let sum = elf
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.reduce(|accum, item| accum + item)
|
.reduce(|accum, item| accum + item)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
totals.push(sum)
|
||||||
elves.push(elf);
|
|
||||||
totals.push(sum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let most = totals.iter().max().unwrap();
|
totals
|
||||||
|
}
|
||||||
|
|
||||||
println!("{:?}{:?}", elves, totals);
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
println!("Max calories: {}", most);
|
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];
|
||||||
|
|
||||||
|
println!("Part 1: Most calories for one elf: {}", most);
|
||||||
|
println!("Part 2: Calories for top three elves: {}", top3);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user