From ce80022674889da5ad3ab6568dac1e9a08f1af77 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Tue, 3 Dec 2024 02:01:50 -0500 Subject: [PATCH] Complete day 1 --- src/day01.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +++ 2 files changed, 79 insertions(+) create mode 100644 src/day01.rs diff --git a/src/day01.rs b/src/day01.rs new file mode 100644 index 0000000..b022310 --- /dev/null +++ b/src/day01.rs @@ -0,0 +1,75 @@ +use std::collections::HashMap; + +fn input() -> &'static str { + include_str!("../input/day01.txt") +} + +fn parse(input: &str) -> (Vec, Vec) { + let mut left = vec![]; + let mut right = vec![]; + for (i, n) in input.split_whitespace().enumerate() { + if i % 2 == 0 { + left.push(n.parse::().unwrap()) + } else { + right.push(n.parse::().unwrap()) + } + } + (left, right) +} + +fn distance(left: &mut [u32], right: &mut [u32]) -> u32 { + left.sort(); + right.sort(); + left.iter() + .zip(right.iter()) + .map(|(l, r)| l.abs_diff(*r)) + .sum() +} + +fn similarity(left: &mut [u32], right: &mut [u32]) -> u32 { + let mut counts: HashMap = HashMap::new(); + for n in right { + let count = counts.entry(*n).or_default(); + *count += 1; + } + left.iter().map(|n| n * counts.get(n).unwrap_or(&0)).sum() +} + +pub fn part1() { + let (mut left, mut right) = parse(input()); + let d = distance(&mut left, &mut right); + println!("Day 1 Part 1: {}", d); +} + +pub fn part2() { + let (mut left, mut right) = parse(input()); + let d = similarity(&mut left, &mut right); + println!("Day 1 Part 2: {}", d); +} + +#[cfg(test)] +mod test { + use super::*; + + const INPUT_STR: &str = concat!("3 4\n", "4 3\n", "2 5\n", "1 3\n", "3 9\n", "3 3"); + + #[test] + fn test_parse() { + assert_eq!( + parse(INPUT_STR), + (vec![3, 4, 2, 1, 3, 3], vec![4, 3, 5, 3, 9, 3]) + ) + } + + #[test] + fn test_distance() { + let (mut left, mut right) = parse(INPUT_STR); + assert_eq!(distance(&mut left, &mut right), 11) + } + + #[test] + fn test_similarity() { + let (mut left, mut right) = parse(INPUT_STR); + assert_eq!(similarity(&mut left, &mut right), 31) + } +} diff --git a/src/main.rs b/src/main.rs index f79c691..d54a6f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,2 +1,6 @@ +pub mod day01; + fn main() { + day01::part1(); + day01::part2(); } -- 2.38.5