32 lines
793 B
Rust
32 lines
793 B
Rust
|
use std::io::{BufRead, BufReader};
|
||
|
use std::fs::File;
|
||
|
|
||
|
fn main (){
|
||
|
// let filename = "../1/input_example.txt";
|
||
|
let filename = "../1/input.txt";
|
||
|
let file = BufReader::new(File::open(filename).expect("Unable to open file"));
|
||
|
let mut accum: u32 = 0;
|
||
|
|
||
|
let mut list1: Vec<u32> = vec![];
|
||
|
let mut list2: Vec<u32> = vec![];
|
||
|
|
||
|
for line in file.lines() {
|
||
|
let wline = line.unwrap();
|
||
|
let nums: Vec<&str> = wline.split(' ')
|
||
|
.filter(|&x| !x.is_empty())
|
||
|
.collect();
|
||
|
|
||
|
list1.push(nums[0].parse().unwrap());
|
||
|
list2.push(nums[1].parse().unwrap());
|
||
|
}
|
||
|
|
||
|
list1.sort();
|
||
|
list2.sort();
|
||
|
let zlist = list1.into_iter().zip(list2);
|
||
|
for t in zlist {
|
||
|
accum += t.0.abs_diff(t.1);
|
||
|
}
|
||
|
|
||
|
println!("{}", accum);
|
||
|
}
|