You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
880 B
Rust

use std::fs;
fn main() {
let contents = fs::read_to_string("data.txt").expect("Failed to read file");
// break into groups divided by empty lines
let groups: Vec<&str> = contents.trim().split("\n\n").collect();
// turn into vectors of vectors of strings
let groups: Vec<Vec<&str>> = groups.iter().map(|&x| x.split("\n").collect()).collect();
// map parsing to u32
let nums: Vec<Vec<u32>> = groups.iter().map(|x| x.iter().map(|&y| y.parse().unwrap()).collect()).collect();
// sum each group
let mut sums: Vec<u32> = nums.iter().map(|x| x.iter().sum()).collect();
// find the max
let max = sums.iter().max().unwrap();
println!("Max Calories: {:?}", max);
sums.sort();
sums.reverse();
let (top3, _) = sums.split_at(3);
let top3_sum = top3.iter().sum::<u32>();
println!("Top 3 Max Calories: {:?}", top3_sum);
}