mirror of
https://github.com/RussellChamp/advent-of-code-2024-rust.git
synced 2026-10-28 07:52:30 +00:00
days 1-3
This commit is contained in:
1 parent
35caf9bf69
commit
c97610ef61
10 files changed
+305
No files matched your search
@@ -0,0 +1,71 @@
|
||||
advent_of_code::solution!(1);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
let mut vec_a = Vec::new();
|
||||
let mut vec_b = Vec::new();
|
||||
let mut result = 0;
|
||||
|
||||
for line in input.split('\n').filter(|l| !l.is_empty()) {
|
||||
let vals: Vec<&str> = line.split(" ").collect();
|
||||
|
||||
let a = vals[0].parse::<u32>().unwrap();
|
||||
let b = vals[1].parse::<u32>().unwrap();
|
||||
vec_a.push(a);
|
||||
vec_b.push(b);
|
||||
}
|
||||
vec_a.sort();
|
||||
vec_b.sort();
|
||||
|
||||
for i in 0..vec_a.len() {
|
||||
let diff = vec_a[i].abs_diff(vec_b[i]);
|
||||
result += diff;
|
||||
// println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff)
|
||||
}
|
||||
// println!("Total {result}");
|
||||
return Some(result);
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
let mut vec_a = Vec::new();
|
||||
let mut vec_b = Vec::new();
|
||||
let mut result = 0;
|
||||
|
||||
for line in input.split('\n').filter(|l| !l.is_empty()) {
|
||||
let vals: Vec<&str> = line.split(" ").collect();
|
||||
|
||||
let a = vals[0].parse::<u32>().unwrap();
|
||||
let b = vals[1].parse::<u32>().unwrap();
|
||||
vec_a.push(a);
|
||||
vec_b.push(b);
|
||||
}
|
||||
vec_a.sort();
|
||||
vec_b.sort();
|
||||
|
||||
for i in 0..vec_a.len() {
|
||||
let needle = vec_a[i];
|
||||
let found = vec_b.iter().filter(|&&b| b == needle).count() as u32;
|
||||
let similarity = found * needle;
|
||||
result += similarity;
|
||||
|
||||
// println!("Line {}: {} found {} times = {}, total {}", i + 1, needle, found, similarity, result);
|
||||
}
|
||||
// println!("Total {result}");
|
||||
return Some(result);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, Some(11));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, Some(31));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user