finish day 1, part 2
This commit is contained in:
parent
69ff24d2cd
commit
907c7307ac
21
src/day_1.rs
21
src/day_1.rs
|
@ -1,8 +1,4 @@
|
|||
use std::fs;
|
||||
|
||||
pub fn day_1() -> String {
|
||||
let input = fs::read_to_string("input/day_1.txt").unwrap();
|
||||
|
||||
pub fn part_1(input: &String) -> String {
|
||||
let elves: Vec<&str> = input.split("\n\n").collect();
|
||||
|
||||
let mut highest_calorie_count: u64 = 0;
|
||||
|
@ -21,3 +17,18 @@ pub fn day_1() -> String {
|
|||
|
||||
format!("{highest_calorie_count}")
|
||||
}
|
||||
|
||||
pub fn part_2(input: &String) -> u64 {
|
||||
let mut elf_calorie_counts: Vec<u64> = input.split("\n\n").map(|elf| {
|
||||
elf.split_whitespace().map(|meal| meal.parse().unwrap_or(0)).sum()
|
||||
}).collect();
|
||||
|
||||
elf_calorie_counts.sort_unstable();
|
||||
|
||||
let start = elf_calorie_counts.len() - 3;
|
||||
let top_3 = elf_calorie_counts.drain(start..);
|
||||
|
||||
println!("Top 3 elves: {:?}", top_3);
|
||||
|
||||
top_3.sum()
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
mod day_1;
|
||||
|
||||
fn main() {
|
||||
println!("{}", day_1::day_1());
|
||||
let day_1_input = std::fs::read_to_string("input/day_1.txt").unwrap();
|
||||
println!("Day 1, part 1: {}", day_1::part_1(&day_1_input));
|
||||
println!("Day 1, part 2: {:?}", day_1::part_2(&day_1_input));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue