add: files so far

This commit is contained in:
Charles
2024-12-02 23:04:39 -08:00
commit 79d1618ede
34 changed files with 953 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
Advent of code 2024
Day 1
Usage:
rustc d1p1.rs
cat input.txt | ./d1p1
Part 1:
Parse each line into a pair of ints; store them in a pair of heaps.
Convert heaps into sorted lists. Iterate through them together
and find the absolute difference between each pair of numbers.
Part 2:
Parse each line into a pair of ints; store the first in an array,
place the second into a map that accumulate the number of times
a value is seen.
Iterate through the array, adding the number times the number of
times it was seen in the map.
+22
View File
@@ -0,0 +1,22 @@
use std::collections::BinaryHeap;
use std::io;
fn main() -> io::Result<()> {
let mut l1 = BinaryHeap::<u64>::new();
let mut l2 = BinaryHeap::<u64>::new();
for line in io::stdin().lines() {
let line = line?;
let parts: Vec<&str> = line.trim().split_whitespace().collect();
let (n1, n2) = (parts[0], parts[1]);
l1.push(n1.parse().unwrap());
l2.push(n2.parse().unwrap());
}
let l1 = l1.into_sorted_vec();
let l2 = l2.into_sorted_vec();
let mut sum = 0;
for (n1, n2) in l1.into_iter().zip(l2.into_iter()) {
sum += n1.abs_diff(n2);
}
println!("{}", sum);
Ok(())
}
+6
View File
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
+21
View File
@@ -0,0 +1,21 @@
use std::collections::HashMap;
use std::io;
fn main() -> io::Result<()> {
let mut l1: Vec<u64> = vec!();
let mut l2: HashMap<u64, u64> = HashMap::default();
for line in io::stdin().lines() {
let line = line?;
let parts: Vec<&str> = line.trim().split_whitespace().collect();
let (n1, n2) = (parts[0], parts[1]);
l1.push(n1.parse().unwrap());
let v = l2.entry(n2.parse::<u64>().unwrap()).or_insert(0);
*v += 1;
}
let mut sum = 0;
for n1 in l1.into_iter() {
sum += n1*l2.get(&n1).unwrap_or(&0);
}
println!("{}", sum);
Ok(())
}