2024 - Day 1 part 1 - rust

-- init rust setup
This commit is contained in:
kleph 2024-12-02 00:24:33 +01:00
parent 014b656137
commit 8ce7e826bf
4 changed files with 40 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.idea
*.swp
.vscode

2
2024/rust1/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target
Cargo.lock

6
2024/rust1/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "rust1"
version = "0.1.0"
edition = "2021"
[dependencies]

31
2024/rust1/src/main.rs Normal file
View file

@ -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<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);
}