From 968d7957207a9031bc262e86a75c4e8082885428 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sat, 30 Nov 2024 22:03:19 -0800 Subject: [PATCH] day 1: cleanup and refactor * I misunderstood the problem and thought we were asked for the distance between the numbers' positions, not their values. Removed remnants of that. * Move to struct-oriented solution * Fix clippys and unnecessary code --- 1/src/main.rs | 84 +++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/1/src/main.rs b/1/src/main.rs index 612403e..9852efc 100644 --- a/1/src/main.rs +++ b/1/src/main.rs @@ -25,60 +25,58 @@ fn main() { println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); } -// PROBLEM 1 solution - -#[derive(Debug)] -struct Entry { - pos: usize, - val: u32, +struct Locations { + left: Vec, + right: Vec, } -fn problem1(input: Lines) -> u64 { - let mut left = Vec::new(); - let mut right = Vec::new(); - for (pos, line) in input.enumerate() { - if line.is_err() { - panic!("can't read line"); +impl From> for Locations { + fn from(input: Lines) -> Self { + let mut left = Vec::new(); + let mut right = Vec::new(); + for line in input.map(|i| i.unwrap()) { + let parts: Vec<&str> = line.split_ascii_whitespace().collect(); + left.push(parts[0].parse::().unwrap()); + right.push(parts[1].parse::().unwrap()); } - let line = line.unwrap(); - let parts: Vec<&str> = line.split_ascii_whitespace().collect(); - left.push(Entry { - pos, - val: parts[0].parse::().unwrap(), - }); - right.push(Entry { - pos, - val: parts[1].parse::().unwrap(), - }); + Self { left, right } } - left.sort_by_key(|entry| entry.val); - right.sort_by_key(|entry| entry.val); +} - println!("{:?}", right); +impl Locations { + fn sort(&mut self) { + self.left.sort(); + self.right.sort(); + } + fn right_count(&self) -> HashMap { + let mut right_count: HashMap = HashMap::new(); + for rval in &self.right { + right_count.insert(*rval, *right_count.get(rval).unwrap_or(&0) + 1); + } + right_count + } +} - zip(left, right) - .map(|(left, right)| { - println!("{:?} {:?}", left, right); - u64::abs_diff(left.val as u64, right.val as u64) - }) +// PROBLEM 1 solution + +fn problem1(input: Lines) -> u64 { + let mut locations = Locations::from(input); + locations.sort(); + + zip(locations.left, locations.right) + .map(|(l, r)| u64::abs_diff(l, r)) .sum() } // PROBLEM 2 solution fn problem2(input: Lines) -> u64 { - let mut left = Vec::new(); - let mut right_count = HashMap::new(); - for (pos, line) in input.enumerate() { - if line.is_err() { - panic!("can't read line"); - } - let line = line.unwrap(); - let parts: Vec<&str> = line.split_ascii_whitespace().collect(); - left.push(parts[0].parse::().unwrap()); - let right = parts[1].parse::().unwrap(); - right_count.insert(right, *right_count.get(&right).unwrap_or(&0) + 1); - } - left.iter().map(|l| l * right_count.get(l).unwrap_or(&0)).sum::() as u64 + let locations = Locations::from(input); + let right_count = locations.right_count(); + locations + .left + .iter() + .map(|l| l * right_count.get(l).unwrap_or(&0)) + .sum::() } #[cfg(test)]