-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-1.rs
25 lines (19 loc) · 842 Bytes
/
day-1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::fs;
fn main () {
let file_str = fs::read_to_string("day-1.input").expect("Failed to read file");
let elf_str_sections : Vec<&str> = file_str.trim().split("\n\n").collect();
let mut elf_calories : Vec<i32> = elf_str_sections.iter().map(
|&elf_str_section| {
let elf_strs : Vec<&str> = elf_str_section.trim().split("\n").collect();
elf_strs.iter().map(
|&calorie_str| {
calorie_str.to_string().parse::<i32>().unwrap()
}
).sum()
}
).collect();
elf_calories.sort();
let elf_calories_rev : Vec<i32> = elf_calories.iter().rev().map(|&x| x).collect();
println!("Top elf: {}", elf_calories_rev[0]);
println!("Top 3 elves: {}", elf_calories_rev[0] + elf_calories_rev[1] + elf_calories_rev[2]);
}