Advent of Code 2024 - Day 1 & 2

This commit is contained in:
Jos van Goor 2024-12-02 16:45:15 +01:00
parent 7865e624c5
commit 7ed2746251
15 changed files with 2245 additions and 1 deletions

View file

@ -0,0 +1,26 @@
pub fn solve(input: &String) {
let all_numbers = input.split_whitespace()
.filter(|str| !str.is_empty())
.map(|str| str.parse::<i32>().unwrap())
.collect::<Vec<_>>();
let mut left = Vec::new();
let mut right = Vec::new();
for idx in (0..all_numbers.len()).step_by(2) {
left.push(all_numbers[idx]);
right.push(all_numbers[idx + 1]);
}
left.sort();
right.sort();
let mut sum_of_distances = 0;
for idx in 0..left.len() {
sum_of_distances += (left[idx] - right[idx]).abs();
}
println!("sum of distances: {}", sum_of_distances);
}