2024-12-19 17:12:39 +01:00
|
|
|
use advent_of_code::include_aoc;
|
2024-12-08 02:04:16 +01:00
|
|
|
use advent_of_code::strings::{convert_to_array, parsenumber};
|
2024-12-01 16:30:48 +01:00
|
|
|
|
2024-12-19 17:12:39 +01:00
|
|
|
include_aoc!(2024, 01);
|
2024-12-01 16:30:48 +01:00
|
|
|
|
2024-12-08 02:04:16 +01:00
|
|
|
pub fn splitspace(input: &str) -> (u32, u32) {
|
|
|
|
println!("{}", input);
|
|
|
|
let mut output = input.split_ascii_whitespace();
|
2024-12-14 11:47:02 +01:00
|
|
|
(parsenumber(output.next().unwrap()), parsenumber(output.next().unwrap()))
|
2024-12-08 02:04:16 +01:00
|
|
|
}
|
|
|
|
|
2024-12-01 16:30:48 +01:00
|
|
|
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);
|
2024-12-07 12:47:53 +01:00
|
|
|
for (left, right) in convert_to_array::<_, _, '\n'>(DATA, splitspace) {
|
2024-12-08 02:04:16 +01:00
|
|
|
leftlist.push(left);
|
|
|
|
rightlist.push(right);
|
2024-12-01 16:30:48 +01:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|