This repository has been archived on 2026-05-14. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
aoc_24/src/day1/mod.rs
T
cool-mist 583988cabf day2
2024-12-10 00:28:21 +05:30

51 lines
1.1 KiB
Rust

use std::collections::HashSet;
pub fn one(input: String) {
let mut left: Vec<i32> = Vec::new();
let mut right: Vec<i32> = Vec::new();
input.lines().for_each(|line| {
let mut parts = line.split_whitespace();
left.push(parts.next().unwrap().parse().unwrap());
right.push(parts.next().unwrap().parse().unwrap());
});
left.sort();
right.sort();
let res = left
.iter()
.zip(right.iter())
.map(|(l, r)| abs(*l, *r))
.sum::<i32>();
println!("{}", res);
}
pub fn two(input: String) {
let mut left: HashSet<i32> = HashSet::new();
let mut right: Vec<i32> = Vec::new();
input.lines().for_each(|line| {
let mut parts = line.split_whitespace();
left.insert(parts.next().unwrap().parse().unwrap());
right.push(parts.next().unwrap().parse().unwrap());
});
let mut acc = 0;
for r in right.iter() {
if left.contains(r) {
acc = acc + *r;
}
}
println!("{}", acc);
}
fn abs(a: i32, b: i32) -> i32 {
if a > b {
a - b
} else {
b - a
}
}