added 2024 part 2
This commit is contained in:
1
2024/01/Rust/.gitignore
vendored
Normal file
1
2024/01/Rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
2024/01/Rust/Cargo.lock
generated
Normal file
7
2024/01/Rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day_1"
|
||||
version = "0.1.0"
|
||||
6
2024/01/Rust/Cargo.toml
Normal file
6
2024/01/Rust/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day_1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
6
2024/01/Rust/src/example.txt
Normal file
6
2024/01/Rust/src/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
1000
2024/01/Rust/src/input.txt
Normal file
1000
2024/01/Rust/src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
42
2024/01/Rust/src/main.rs
Normal file
42
2024/01/Rust/src/main.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
fn main() {
|
||||
let file_name = "./src/example.txt";
|
||||
let input = fs::read_to_string(file_name)
|
||||
.expect("File was not able to be read");
|
||||
let input_list:Vec<&str> = input.split("\r\n").collect();
|
||||
let mut left_list: Vec<u32> = Vec::new();
|
||||
let mut right_list: Vec<u32> = Vec::new();
|
||||
input_list.into_iter().for_each(|row|{
|
||||
let row_list:Vec<&str> = row.split(" ").collect();
|
||||
left_list.push(row_list[0].parse().expect("Error Number conversion"));
|
||||
right_list.push(row_list[1].parse().expect("Error Number conversion"));
|
||||
});
|
||||
left_list.sort();
|
||||
right_list.sort();
|
||||
let frequency_list = right_list
|
||||
.iter()
|
||||
.copied()
|
||||
.fold(HashMap::new(), |mut map, val|{
|
||||
map.entry(val)
|
||||
.and_modify(|frq| *frq+=1)
|
||||
.or_insert(1);
|
||||
map
|
||||
});
|
||||
let result = left_list
|
||||
.iter()
|
||||
.zip(right_list.iter())
|
||||
.map(|(l, r)| i32::abs(*r as i32 - *l as i32))
|
||||
//.collect::<Vec<i32>>();
|
||||
.sum::<i32>();
|
||||
let result_2 = left_list
|
||||
.iter()
|
||||
.map(|v| v * frequency_list.get(v).unwrap_or(&0))
|
||||
//.collect::<Vec<u32>>();
|
||||
.sum::<u32>();
|
||||
println!("Content:\n{:#?}", left_list);
|
||||
//println!("Content:\n{:#?}", right_list);
|
||||
println!("Content:\n{:#?}", frequency_list.clone());
|
||||
println!("Content:\n{:#?}", result);
|
||||
println!("Content:\n{:#?}", result_2);
|
||||
}
|
||||
Reference in New Issue
Block a user