1
0

Part 2 is solved

This commit is contained in:
Pcornat 2022-12-01 22:34:04 +01:00
parent 3af4d351a3
commit 1f09c78916
Signed by: Pcornat
GPG Key ID: 2F3932FF46D9ECA0
2 changed files with 23 additions and 3 deletions

View File

@ -5,6 +5,6 @@ fn main() {
let file_path = args
.get(1)
.expect("Please provide an argument on command-line.");
let max = prob1::solve(file_path);
let max = prob1::solve_part2(file_path);
println!("{max}");
}

View File

@ -1,7 +1,9 @@
pub fn solve(file_path: &str) -> i32 {
use std::cmp::Reverse;
pub fn solve_part1(file_path: &str) -> i32 {
let contents =
std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
let lines: Vec<&str> = contents.split("\n").collect();
let lines: Vec<&str> = contents.split('\n').collect();
let mut max = 0;
let mut accumulated = 0;
@ -15,3 +17,21 @@ pub fn solve(file_path: &str) -> i32 {
});
max
}
pub fn solve_part2(file_path: &str) -> i32 {
let contents =
std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
let lines: Vec<&str> = contents.split('\n').collect();
let mut numbers: Vec<i32> = Vec::with_capacity(lines.len());
let mut accumulated = 0;
lines.iter().for_each(|number| {
if let Ok(yay) = number.parse::<i32>() {
accumulated += yay;
} else {
numbers.push(accumulated);
accumulated = 0;
}
});
numbers.sort_by_key(|w| Reverse(*w));
numbers[0] + numbers[1] + numbers[2]
}