Day 01 - Historian Hysteria
Language: Rust
Problem https://adventofcode.com/2024/day/1
Part 1: Two columns of numbers. Parse them into two separate vecs, sort both, then pair them up smallest to smallest and sum the absolute differences:
for pair in input.lines() {
let mut parts = pair.split_whitespace();
if let (Some(a), Some(b)) = (parts.next(), parts.next()) {
if let (Ok(num_a), Ok(num_b)) = (a.parse::<i32>(), b.parse::<i32>()) {
v1.push(num_a);
v2.push(num_b);
}
}
}
v1.sort();
v2.sort();
let ans: i32 = v1.iter()
.zip(v2.iter())
.map(|(&a, &b)| (a - b).abs())
.sum::<i32>();
Part 2:
For each number in the left list, count how many times it appears in the right list and
multiply. I iterate v2 for each element in v1 and accumulate the score:
for num in v1 {
let count = v2.iter().filter(|x| **x == num).count() as i32;
sum += num * count;
}
Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2024/day01.rs
No C writeup yet.