53 Zeilen
1,4 KiB
Rust
53 Zeilen
1,4 KiB
Rust
|
use advent_of_code::strings::{parsenumber, splitspace};
|
||
|
use adventofcode_macros::include_data;
|
||
|
|
||
|
include_data!(2024 01);
|
||
|
|
||
|
fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
|
||
|
let mut distance = 0;
|
||
|
for i in 0..leftlist.len() {
|
||
|
let left = leftlist[i];
|
||
|
let right = rightlist[i];
|
||
|
distance += match right > left {
|
||
|
true => right - left,
|
||
|
false => left - right,
|
||
|
};
|
||
|
}
|
||
|
distance
|
||
|
}
|
||
|
|
||
|
fn similarity(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
|
||
|
let mut similarity = 0;
|
||
|
let mut count: u32;
|
||
|
for i in leftlist {
|
||
|
if rightlist.contains(i) {
|
||
|
count = 0;
|
||
|
for j in rightlist {
|
||
|
if *i == *j {
|
||
|
count += 1;
|
||
|
}
|
||
|
}
|
||
|
similarity += i * count;
|
||
|
}
|
||
|
}
|
||
|
similarity
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let mut leftlist = Vec::<u32>::with_capacity(1000);
|
||
|
let mut rightlist = Vec::<u32>::with_capacity(1000);
|
||
|
for (left, right) in DATA.split("\n").map(splitspace) {
|
||
|
leftlist.push(parsenumber(left));
|
||
|
rightlist.push(parsenumber(right));
|
||
|
}
|
||
|
leftlist.sort();
|
||
|
rightlist.sort();
|
||
|
let length = leftlist.len();
|
||
|
if length != rightlist.len() {
|
||
|
println!("Not matching lists");
|
||
|
return;
|
||
|
}
|
||
|
println!("Maximum Distance: {}", distance(&leftlist, &rightlist));
|
||
|
println!("Similarity: {}", similarity(&leftlist, &rightlist))
|
||
|
}
|