Part 2 is solved
This commit is contained in:
parent
3af4d351a3
commit
1f09c78916
@ -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}");
|
||||
}
|
||||
|
@ -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]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user