From 4b95bddb6d33c23c79bee8f41fb0e9ee4d0c23f7 Mon Sep 17 00:00:00 2001 From: Maurice Wangleng Tan Date: Sun, 1 Dec 2024 13:35:21 +0800 Subject: [PATCH] Clean up day 01 some more --- src/bin/day01/main.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/bin/day01/main.rs b/src/bin/day01/main.rs index 0ec0893..47b6050 100644 --- a/src/bin/day01/main.rs +++ b/src/bin/day01/main.rs @@ -6,16 +6,19 @@ fn read_input(input: &str) -> (Vec, Vec) { input .trim() .lines() + .map(|line| { + let (left, right) = line.trim().split_once(" ").unwrap_or_else(|| { + panic!("Expect two values per line with three spaces in between, but {line} is not") + }); + + ( + left.parse().expect("not a number"), + right.parse().expect("not a number"), + ) + }) .fold((vec![], vec![]), |mut acc, line| { - let values = line.split_whitespace().collect::>(); - - if values.len() != 2 { - panic!("Expect two values per line, but {line} is not"); - } - - acc.0.push(values[0].parse().expect("not a number")); - acc.1.push(values[1].parse().expect("not a number")); - + acc.0.push(line.0); + acc.1.push(line.1); acc }) } @@ -29,7 +32,7 @@ fn p1(input: &str) -> String { left_list .into_iter() .zip(right_list) - .map(|(left, right)| left.max(right) - left.min(right)) + .map(|(left, right)| (left - right).abs()) .sum::() .to_string() } @@ -45,7 +48,7 @@ fn p2(input: &str) -> String { left_list .into_iter() - .map(|left_number| left_number * (right_map.get(&left_number).copied().unwrap_or_default())) + .map(|left_number| left_number * (*right_map.get(&left_number).unwrap_or(&0))) .sum::() .to_string() }