diff --git a/.gitignore b/.gitignore index ca3c9ea..14f9f9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea *.swp +.vscode diff --git a/2024/rust1/.gitignore b/2024/rust1/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/2024/rust1/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/2024/rust1/Cargo.toml b/2024/rust1/Cargo.toml new file mode 100644 index 0000000..32169df --- /dev/null +++ b/2024/rust1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/rust1/src/main.rs b/2024/rust1/src/main.rs new file mode 100644 index 0000000..20acaad --- /dev/null +++ b/2024/rust1/src/main.rs @@ -0,0 +1,31 @@ +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 = vec![]; + let mut list2: Vec = 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); +}