Solve Part 2
This commit is contained in:
parent
f95bd1267f
commit
5c63fdf294
36
src/main.rs
36
src/main.rs
@ -2,15 +2,21 @@
|
||||
|
||||
const DATA: &'static str = include_str!("input.txt");
|
||||
|
||||
fn parse_calories_per_elf(elf: &str) -> i32 {
|
||||
let mut elf_sum = 0;
|
||||
for food_item in elf.split("\n")
|
||||
{
|
||||
elf_sum += food_item.parse().unwrap_or(0);
|
||||
}
|
||||
elf_sum
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn part1() {
|
||||
let mut highest_sum = 0;
|
||||
for elf in DATA.split("\n\n")
|
||||
{
|
||||
let mut elf_sum = 0;
|
||||
for food_item in elf.split("\n")
|
||||
{
|
||||
elf_sum += food_item.parse().unwrap_or(0);
|
||||
}
|
||||
let elf_sum = parse_calories_per_elf(elf);
|
||||
if elf_sum > highest_sum {
|
||||
highest_sum = elf_sum;
|
||||
}
|
||||
@ -18,7 +24,25 @@ fn part1() {
|
||||
println!("The elf carrying the most calories carries {} calories", highest_sum);
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let mut elves: Vec<i32> = Vec::new();
|
||||
|
||||
for elf in DATA.split("\n\n")
|
||||
{
|
||||
let elf_sum = parse_calories_per_elf(elf);
|
||||
elves.push(elf_sum);
|
||||
}
|
||||
|
||||
elves.sort();
|
||||
|
||||
// Get the 3 elves carrying more calories
|
||||
let slice = &elves[elves.len()-3 ..];
|
||||
|
||||
println!("The 3 elves carrying the most calories carry {} calories", slice.iter().sum::<i32>());
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
part1();
|
||||
//part1();
|
||||
part2();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user