dedupe and improve
This commit is contained in:
parent
1d58f1e24c
commit
bc0ba67c16
28
src/day_1.rs
28
src/day_1.rs
|
@ -1,27 +1,19 @@
|
||||||
|
fn elf_calorie_counts(input: &String) -> Vec<u64> {
|
||||||
|
input.split("\n\n").map(|elf| {
|
||||||
|
elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum()
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn part_1(input: &String) -> u64 {
|
pub fn part_1(input: &String) -> u64 {
|
||||||
let elves: Vec<&str> = input.split("\n\n").collect();
|
let mut elf_calorie_counts= elf_calorie_counts(input);
|
||||||
|
|
||||||
let mut highest_calorie_count: u64 = 0;
|
elf_calorie_counts.sort_unstable();
|
||||||
|
|
||||||
for meals in elves.iter() {
|
elf_calorie_counts.pop().unwrap()
|
||||||
let mut elf_calorie_count= 0;
|
|
||||||
|
|
||||||
for meal in meals.split_whitespace() {
|
|
||||||
elf_calorie_count += meal.parse().unwrap_or(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if elf_calorie_count > highest_calorie_count {
|
|
||||||
highest_calorie_count = elf_calorie_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
highest_calorie_count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part_2(input: &String) -> u64 {
|
pub fn part_2(input: &String) -> u64 {
|
||||||
let mut elf_calorie_counts: Vec<u64> = input.split("\n\n").map(|elf| {
|
let mut elf_calorie_counts= elf_calorie_counts(input);
|
||||||
elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum()
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
elf_calorie_counts.sort_unstable();
|
elf_calorie_counts.sort_unstable();
|
||||||
elf_calorie_counts.reverse();
|
elf_calorie_counts.reverse();
|
||||||
|
|
Loading…
Reference in New Issue