38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
use std::fs::File;
|
|
use std::io::{self, BufRead};
|
|
use std::path::Path;
|
|
|
|
fn main() {
|
|
let file_path = String::from("/home/wafoo/advent_of_code_2022/aoc_01/input");
|
|
let mut result_vec = Vec::<u32>::new();
|
|
|
|
result_vec.push(0);
|
|
|
|
println!("In file {}", file_path);
|
|
|
|
if let Ok(lines) = read_lines(file_path) {
|
|
// Consumes the iterator, returns an (Optional) String
|
|
for line in lines {
|
|
if let Ok(ip) = line {
|
|
match ip.as_str() {
|
|
"" => result_vec.push(0),
|
|
_ => {
|
|
let index = result_vec.len() - 1;
|
|
result_vec[index] += ip.parse::<u32>().unwrap();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let index = result_vec.len() - 1;
|
|
result_vec.sort();
|
|
let result = result_vec[index] + result_vec[index - 1] + result_vec[index - 2];
|
|
println!("{}", result);
|
|
}
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
where P: AsRef<Path>, {
|
|
let file = File::open(filename)?;
|
|
Ok(io::BufReader::new(file).lines())
|
|
} |