Hi everyone.
Welcome to Advent of Code 2024.
first off, I'm going to be doing this for a bit, so if you find this annoying, feel free to filter #aoc24 which will always appear in these posts.
I'm using Rust, as usual.
I solved both tasks. Let's start with task 1:
use std::cmp::{max, min};
use std::fs::read_to_string;
fn main() {
let mut l1 = Vec::<u32>::new();
let mut l2 = Vec::<u32>::new();
let f = read_to_string("input.txt").unwrap();
let f = f.split('\n');
for i in f {
if i.is_empty() {
break;
}
let mut s = i.split_whitespace().map(|s| s.parse::<u32>().unwrap());
l1.push(s.next().unwrap());
l2.push(s.next().unwrap());
}
l1.sort();
l2.sort();
let mut dist = 0;
for i in 0..l1.len() {
let n1 = l1[i];
let n2 = l2[i];
dist += max(n1, n2) - min(n1, n2);
}
println!("{}", dist);
}
Pretty straightforward, though I can already see some places that could be optimised, for example, n1 and n2 don't need to be used at all, and I could directly min and max the vector elements. Likewise, I could use a single comparison instead of both max and min, but I was lazy.
modulux
in reply to modulux • • •Sensitive content
Now comes day 1, task 2.
Also pretty straightforward.
The problem is I didn't remember tons of rust, for example how to select a type for Vec::new() or how to do the type casting, so it took me a bit longer than I hoped.
But I still can code! Programmer forever!
#aoc24
Sugui
in reply to modulux • • •modulux
in reply to Sugui • • •