From 1feb4fae9fa1030d5f71668e2244cf14c8d39e23 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Sat, 2 Dec 2023 13:49:24 +0100 Subject: [PATCH] Rust clean up day 2 --- rust/src/day02/main.rs | 66 +++++++++++++++++++++++++++++++++++++ rust/src/day02/part_1.rs | 69 +++++++++----------------------------- rust/src/day02/part_2.rs | 71 +++++++++------------------------------- 3 files changed, 97 insertions(+), 109 deletions(-) diff --git a/rust/src/day02/main.rs b/rust/src/day02/main.rs index 333bf98..d255bd1 100644 --- a/rust/src/day02/main.rs +++ b/rust/src/day02/main.rs @@ -5,6 +5,72 @@ use part_1::part_1; mod part_2; use part_2::part_2; +pub(crate) struct CubeCounts { + red: u16, + green: u16, + blue: u16, +} +impl CubeCounts { + #[must_use] + const fn power(&self) -> u16 { + self.red * self.green * self.blue + } + + #[must_use] + const fn zero() -> Self { + Self { + red: 0, + green: 0, + blue: 0, + } + } + + #[must_use] + const fn red(red: u16) -> Self { + Self { + red, + green: 0, + blue: 0, + } + } + + #[must_use] + const fn green(green: u16) -> Self { + Self { + red: 0, + green, + blue: 0, + } + } + + #[must_use] + const fn blue(blue: u16) -> Self { + Self { + red: 0, + green: 0, + blue, + } + } +} + +impl std::ops::Add for CubeCounts { + type Output = Self; + + fn add(self, rhs: CubeCounts) -> Self::Output { + Self { + red: self.red + rhs.red, + green: self.green + rhs.green, + blue: self.blue + rhs.blue, + } + } +} + +impl std::iter::Sum for CubeCounts { + fn sum>(iter: I) -> Self { + iter.fold(CubeCounts::zero(), |acc, x| acc + x) + } +} + fn main() { println!("{}", part_1(INPUT)); println!("{}", part_2(INPUT)); diff --git a/rust/src/day02/part_1.rs b/rust/src/day02/part_1.rs index bd65caf..1e4c392 100644 --- a/rust/src/day02/part_1.rs +++ b/rust/src/day02/part_1.rs @@ -1,32 +1,4 @@ -#[derive(Default, Debug)] -struct CubeCounts { - red: u16, - green: u16, - blue: u16, -} -impl std::fmt::Display for CubeCounts { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "({},{},{})", self.red, self.green, self.blue) - } -} - -impl std::ops::Add for CubeCounts { - type Output = Self; - - fn add(self, rhs: CubeCounts) -> Self::Output { - Self { - red: self.red + rhs.red, - green: self.green + rhs.green, - blue: self.blue + rhs.blue, - } - } -} - -impl std::iter::Sum for CubeCounts { - fn sum>(iter: I) -> Self { - iter.fold(CubeCounts::default(), |acc, x| acc + x) - } -} +use crate::CubeCounts; pub(crate) fn part_1(input: &'static str) -> usize { let id_sum = input @@ -43,33 +15,22 @@ pub(crate) fn part_1(input: &'static str) -> usize { .map(|group| { let color_counts = group.split(", "); - let group_counts = - color_counts.map(|color_count| { - let (count, color) = color_count - .split_once(' ') - .expect("Every color count should have one space"); + let group_counts = color_counts.map(|color_count| { + let (count, color) = color_count + .split_once(' ') + .expect("Every color count should have one space"); - let count = count.parse().expect( - format!("Every color count should be a valid number - `{count}` `{color}`") - .as_str(), - ); + let count = count + .parse() + .expect("Every color count should be a valid number"); - match color { - "red" => CubeCounts { - red: count, - ..Default::default() - }, - "green" => CubeCounts { - green: count, - ..Default::default() - }, - "blue" => CubeCounts { - blue: count, - ..Default::default() - }, - _ => unreachable!(), - } - }); + match color { + "red" => CubeCounts::red(count), + "green" => CubeCounts::green(count), + "blue" => CubeCounts::blue(count), + _ => unreachable!(), + } + }); group_counts.sum::() }) .reduce(|acc, cube_count| CubeCounts { diff --git a/rust/src/day02/part_2.rs b/rust/src/day02/part_2.rs index 3953677..f61d15a 100644 --- a/rust/src/day02/part_2.rs +++ b/rust/src/day02/part_2.rs @@ -1,38 +1,10 @@ -#[derive(Default)] -struct CubeCounts { - red: u16, - green: u16, - blue: u16, -} -impl CubeCounts { - const fn power(&self) -> u16 { - self.red * self.green * self.blue - } -} - -impl std::ops::Add for CubeCounts { - type Output = Self; - - fn add(self, rhs: CubeCounts) -> Self::Output { - Self { - red: self.red + rhs.red, - green: self.green + rhs.green, - blue: self.blue + rhs.blue, - } - } -} - -impl std::iter::Sum for CubeCounts { - fn sum>(iter: I) -> Self { - iter.fold(CubeCounts::default(), |acc, x| acc + x) - } -} +use crate::CubeCounts; pub(crate) fn part_2(input: &'static str) -> u16 { let id_sum = input .lines() .map(|line| { - let game_start_index = line.find(":").expect("Every line should have a ': '") + 2; + let game_start_index = line.find(':').expect("Every line should have a ': '") + 2; let game = &line[game_start_index..]; @@ -42,33 +14,22 @@ pub(crate) fn part_2(input: &'static str) -> u16 { .map(|group| { let color_counts = group.split(", "); - let group_counts = - color_counts.map(|color_count| { - let (count, color) = color_count - .split_once(' ') - .expect("Every color count should have one space"); + let group_counts = color_counts.map(|color_count| { + let (count, color) = color_count + .split_once(' ') + .expect("Every color count should have one space"); - let count = count.parse().expect( - format!("Every color count should be a valid number - `{count}` `{color}`") - .as_str(), - ); + let count = count + .parse() + .expect("Every color count should be a valid number"); - match color { - "red" => CubeCounts { - red: count, - ..Default::default() - }, - "green" => CubeCounts { - green: count, - ..Default::default() - }, - "blue" => CubeCounts { - blue: count, - ..Default::default() - }, - _ => unreachable!(), - } - }); + match color { + "red" => CubeCounts::red(count), + "green" => CubeCounts::green(count), + "blue" => CubeCounts::blue(count), + _ => unreachable!(), + } + }); group_counts.sum::() }) .reduce(|acc, cube_count| CubeCounts {