mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 18:01:34 +01:00
Solve 2024 day 1 in rust
Co-Authored-By: NebelToast <>
This commit is contained in:
parent
cf1df57e6f
commit
fcff9f95a3
4 changed files with 1081 additions and 0 deletions
7
2024/rust/day-01/Cargo.lock
generated
Normal file
7
2024/rust/day-01/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-01"
|
||||
version = "0.1.0"
|
||||
6
2024/rust/day-01/Cargo.toml
Normal file
6
2024/rust/day-01/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
1000
2024/rust/day-01/input
Normal file
1000
2024/rust/day-01/input
Normal file
File diff suppressed because it is too large
Load diff
68
2024/rust/day-01/src/main.rs
Normal file
68
2024/rust/day-01/src/main.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
fs,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
let (list1, list2) = read_input();
|
||||
|
||||
println!("Total Distance: {}", get_total_distance(&list1, &list2));
|
||||
println!("Similarity Score: {}", get_similarity_score(&list1, &list2));
|
||||
}
|
||||
|
||||
fn get_total_distance(list1: &Vec<u32>, list2: &Vec<u32>) -> u32 {
|
||||
let mut list = vec![];
|
||||
for (i, _) in list1.iter().enumerate() {
|
||||
list.push((list1[i], list2[i]));
|
||||
}
|
||||
|
||||
list.iter().map(|x| x.0.abs_diff(x.1)).sum()
|
||||
}
|
||||
|
||||
fn get_similarity_score(list1: &Vec<u32>, list2: &Vec<u32>) -> u32 {
|
||||
let mut scores = HashMap::new();
|
||||
|
||||
for entry in list1 {
|
||||
scores.insert(entry, 0);
|
||||
}
|
||||
|
||||
for entry in list2 {
|
||||
match scores.entry(entry) {
|
||||
Entry::Occupied(mut value) => {
|
||||
*value.get_mut() += 1;
|
||||
}
|
||||
Entry::Vacant(_) => (),
|
||||
}
|
||||
}
|
||||
|
||||
scores.iter().map(|x| *x.0 * *x.1).sum()
|
||||
}
|
||||
|
||||
fn read_input() -> (Vec<u32>, Vec<u32>) {
|
||||
let contents = fs::read_to_string("./input").unwrap();
|
||||
let lines = contents.lines();
|
||||
|
||||
let mut list1 = vec![];
|
||||
let mut list2 = vec![];
|
||||
|
||||
for line in lines {
|
||||
let parts: Vec<&str> = line.split(" ").collect();
|
||||
assert_eq!(parts.len(), 2);
|
||||
|
||||
let num1: u32 = parts[0].parse().unwrap();
|
||||
let num2: u32 = parts[1].parse().unwrap();
|
||||
|
||||
list1.push(num1);
|
||||
list2.push(num2);
|
||||
}
|
||||
|
||||
list1.sort();
|
||||
list2.sort();
|
||||
|
||||
(list1, list2)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 9 8 7 6 5
|
||||
Loading…
Add table
Add a link
Reference in a new issue