1
0

Better version

This commit is contained in:
Pcornat 2022-12-01 22:18:54 +01:00
parent edf113b698
commit 1e7a441ba8
Signed by: Pcornat
GPG Key ID: 2F3932FF46D9ECA0
3 changed files with 21 additions and 14 deletions

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod prob1;

View File

@ -1,21 +1,10 @@
use _1_calorie_counting::prob1::solve;
fn main() {
let args: Vec<String> = std::env::args().collect();
let file_path = args
.get(1)
.expect("Please provide an argument on command-line.");
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 max = 0;
let mut accumulated = 0;
lines.iter().for_each(|number| {
if let Ok(yay) = number.parse::<i32>() {
accumulated += yay;
} else {
max = max.max(accumulated);
accumulated = 0;
}
});
let max = solve(file_path);
println!("{max}");
}

17
src/prob1/mod.rs Normal file
View File

@ -0,0 +1,17 @@
pub fn solve(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 max = 0;
let mut accumulated = 0;
lines.iter().for_each(|number| {
if let Ok(yay) = number.parse::<i32>() {
accumulated += yay;
} else {
max = max.max(accumulated);
accumulated = 0;
}
});
max
}