Adventofcode/2024/rust/day_1/main.rs

42 lines
1.3 KiB
Rust

use std::fs;
fn main() {
let args:Vec<String> = std::env::args().collect();
if args.len() <= 1 {
println!("Please, provide the input file path to be processed!");
return;
}
let firstArg = &args[1];
let contents = fs::read_to_string(firstArg)
.expect("Expected to open the file");
let splitted_content : std::str::Lines = contents.lines();
//DONE: Split string lines
//DONE: get the values from each column
//DONE: calculate the distance between the columns
//DONE: sum the distances
println!("With text:\n{contents}");
//println!("\n{splittedContent:?}");
let numbers:i32 = splitted_content
//.lines()
//.into_iter()
.filter_map(|x| {
let mut nums = x.split_whitespace().filter_map(|i| i.parse::<i32>().ok());
Some ((nums.next()?,nums.next()?))
})
.filter_map(|(a,b)| {
Some(b - a)
})
//;
.sum();
println!("Distance sum: {numbers}");
/*for number in numbers {
println!("\n number -> {number:?}");
}*/
/*for line in splitted_content {
for word in line.split_whitespace() {
let x = word.parse::<i32>().unwrap();
let y = x.to_string();
println!("Word: {word} -> {y}");
}
}*/
}