This commit is contained in:
Aaron Manning 2024-12-06 18:18:25 +11:00
commit c2575cbcea
4 changed files with 58 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
input.txt
target/

7
day-01/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day-01"
version = "0.1.0"

6
day-01/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day-01"
version = "0.1.0"
edition = "2021"
[dependencies]

43
day-01/src/main.rs Normal file
View File

@ -0,0 +1,43 @@
const INPUT: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/input.txt"));
fn main() {
let (mut first, mut second): (Vec<_>, Vec<_>) =
INPUT.lines()
.filter_map(|line| {
let mut parts = line.split_whitespace();
let first = parts.next()?;
let second = parts.next()?;
Some((
str::parse::<u32>(first).ok()?,
str::parse::<u32>(second).ok()?,
))
})
.unzip();
first.sort();
second.sort();
let difference = first.iter().zip(second.iter().copied())
.map(|(x, y)| x.abs_diff(y))
.sum::<u32>();
println!("Part 1: {}", difference);
let mut similarity = 0;
let mut idx = 0;
for element in first {
while second.get(idx).is_some_and(|x| *x < element) {
idx += 1;
}
let mut occurances = 0;
while second.get(idx) == Some(&element) {
idx += 1;
occurances += 1;
}
similarity += occurances * element;
}
println!("Part 2: {}", similarity);
}