Rust clean up day 2

This commit is contained in:
Tobias Berger 2023-12-02 13:49:24 +01:00
parent dd2b1d362c
commit 1feb4fae9f
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
3 changed files with 97 additions and 109 deletions

View file

@ -5,6 +5,72 @@ use part_1::part_1;
mod part_2; mod part_2;
use part_2::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<CubeCounts> 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<CubeCounts> for CubeCounts {
fn sum<I: Iterator<Item = CubeCounts>>(iter: I) -> Self {
iter.fold(CubeCounts::zero(), |acc, x| acc + x)
}
}
fn main() { fn main() {
println!("{}", part_1(INPUT)); println!("{}", part_1(INPUT));
println!("{}", part_2(INPUT)); println!("{}", part_2(INPUT));

View file

@ -1,32 +1,4 @@
#[derive(Default, Debug)] use crate::CubeCounts;
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<CubeCounts> 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<CubeCounts> for CubeCounts {
fn sum<I: Iterator<Item = CubeCounts>>(iter: I) -> Self {
iter.fold(CubeCounts::default(), |acc, x| acc + x)
}
}
pub(crate) fn part_1(input: &'static str) -> usize { pub(crate) fn part_1(input: &'static str) -> usize {
let id_sum = input let id_sum = input
@ -43,33 +15,22 @@ pub(crate) fn part_1(input: &'static str) -> usize {
.map(|group| { .map(|group| {
let color_counts = group.split(", "); let color_counts = group.split(", ");
let group_counts = let group_counts = color_counts.map(|color_count| {
color_counts.map(|color_count| { let (count, color) = color_count
let (count, color) = color_count .split_once(' ')
.split_once(' ') .expect("Every color count should have one space");
.expect("Every color count should have one space");
let count = count.parse().expect( let count = count
format!("Every color count should be a valid number - `{count}` `{color}`") .parse()
.as_str(), .expect("Every color count should be a valid number");
);
match color { match color {
"red" => CubeCounts { "red" => CubeCounts::red(count),
red: count, "green" => CubeCounts::green(count),
..Default::default() "blue" => CubeCounts::blue(count),
}, _ => unreachable!(),
"green" => CubeCounts { }
green: count, });
..Default::default()
},
"blue" => CubeCounts {
blue: count,
..Default::default()
},
_ => unreachable!(),
}
});
group_counts.sum::<CubeCounts>() group_counts.sum::<CubeCounts>()
}) })
.reduce(|acc, cube_count| CubeCounts { .reduce(|acc, cube_count| CubeCounts {

View file

@ -1,38 +1,10 @@
#[derive(Default)] use crate::CubeCounts;
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<CubeCounts> 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<CubeCounts> for CubeCounts {
fn sum<I: Iterator<Item = CubeCounts>>(iter: I) -> Self {
iter.fold(CubeCounts::default(), |acc, x| acc + x)
}
}
pub(crate) fn part_2(input: &'static str) -> u16 { pub(crate) fn part_2(input: &'static str) -> u16 {
let id_sum = input let id_sum = input
.lines() .lines()
.map(|line| { .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..]; let game = &line[game_start_index..];
@ -42,33 +14,22 @@ pub(crate) fn part_2(input: &'static str) -> u16 {
.map(|group| { .map(|group| {
let color_counts = group.split(", "); let color_counts = group.split(", ");
let group_counts = let group_counts = color_counts.map(|color_count| {
color_counts.map(|color_count| { let (count, color) = color_count
let (count, color) = color_count .split_once(' ')
.split_once(' ') .expect("Every color count should have one space");
.expect("Every color count should have one space");
let count = count.parse().expect( let count = count
format!("Every color count should be a valid number - `{count}` `{color}`") .parse()
.as_str(), .expect("Every color count should be a valid number");
);
match color { match color {
"red" => CubeCounts { "red" => CubeCounts::red(count),
red: count, "green" => CubeCounts::green(count),
..Default::default() "blue" => CubeCounts::blue(count),
}, _ => unreachable!(),
"green" => CubeCounts { }
green: count, });
..Default::default()
},
"blue" => CubeCounts {
blue: count,
..Default::default()
},
_ => unreachable!(),
}
});
group_counts.sum::<CubeCounts>() group_counts.sum::<CubeCounts>()
}) })
.reduce(|acc, cube_count| CubeCounts { .reduce(|acc, cube_count| CubeCounts {