From 0000002038fefac5918c9aa7113e4aaa3a5ff4b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 28 Nov 2022 23:28:19 +0100 Subject: [PATCH] solution: day2 --- src/bin/02.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/bin/02.rs diff --git a/src/bin/02.rs b/src/bin/02.rs new file mode 100644 index 000000000..610099e --- /dev/null +++ b/src/bin/02.rs @@ -0,0 +1,55 @@ +pub fn part_one(input: &str) -> Option { + let present_dimensions: Vec> = input + .trim() + .split('\n') + .map(|x| x.split('x').map(|y| y.parse::().unwrap()).collect()) + .collect(); + + let mut wrapping = 0; + for present in present_dimensions.iter() { + if let [x, y, z] = &present[..] { + wrapping += + 2 * (x * y + y * z + z * x) + vec![x * y, y * z, z * x].iter().min().unwrap(); + } + } + Some(wrapping) +} +pub fn part_two(input: &str) -> Option { + let present_dimensions: Vec> = input + .trim() + .split('\n') + .map(|x| x.split('x').map(|y| y.parse::().unwrap()).collect()) + .collect(); + + let mut wrapping = 0; + for present in present_dimensions.iter() { + if let [x, y, z] = &present[..] { + // ribbon + wrapping += x * y * z; + // ribbon base + wrapping += 2 * (x + y + z - present.iter().max().unwrap()); + } + } + Some(wrapping) +} +fn main() { + let input = &aoc::read_file("inputs", 2); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + // let input = aoc::read_file("test_inputs", 2); + let input = "2x3x4"; + assert_eq!(part_one(&input), Some(58)); + } + #[test] + fn test_part_two() { + // let input = aoc::read_file("test_inputs", 2); + let input = "2x3x4"; + assert_eq!(part_two(&input), Some(34)); + } +}