40 lines
1.4 KiB
Rust
Executable File
40 lines
1.4 KiB
Rust
Executable File
//! Read a file containing sets of number
|
|
//! Get the three maximum sum of the sets
|
|
//! Print the sum of the three maximum
|
|
//!
|
|
//! # Example
|
|
//! cargo run -- ../input.txt # Execute the program with the input file
|
|
|
|
use std::fs;
|
|
use std::env;
|
|
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
let filename = &args[1];
|
|
let contents: String = fs::read_to_string(filename)
|
|
.expect("Something went wrong reading the file");
|
|
let mut current = 0;
|
|
let mut current_maximums = [0; 3]; // Store the three maximums
|
|
for line in contents.lines() {
|
|
// Check if line is empty
|
|
if line.is_empty() {
|
|
// Check if current is greater than the minimum of the maximums
|
|
if current > *current_maximums.iter().min().unwrap() {
|
|
// Replace each value of the maximums by the current value if it is greater
|
|
// And keep the maximums sorted with previous maximums kept
|
|
for i in 0..3 {
|
|
if current > current_maximums[i] {
|
|
current_maximums[i] = current;
|
|
current_maximums.sort();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
current = 0;
|
|
} else {
|
|
let number: i32 = line.parse().unwrap();
|
|
current += number;
|
|
}
|
|
}
|
|
println!("Max: {}", current_maximums.iter().sum::<i32>());
|
|
} |