From 85ca3caa1a0847162d5874b0b5140661ad9451ea Mon Sep 17 00:00:00 2001 From: Lennard Brinkhaus Date: Sun, 3 Dec 2023 19:08:26 +0100 Subject: [PATCH] refactor: some nice code stuff --- src/day02.rs | 84 ++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/src/day02.rs b/src/day02.rs index bebac53..10e4e06 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -1,24 +1,14 @@ -use std::cmp::Ordering; -use crate::day02::CubeType::{Blue, Green, Red, UNKNOWN}; +use std::cmp::{max, Ordering}; +use CubeType::*; +#[derive(PartialEq)] enum CubeType { - Red(i32), - Green(i32), - Blue(i32), + Red(usize), + Green(usize), + Blue(usize), UNKNOWN } -impl PartialEq for CubeType { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Red(first), Red(second )) => first == second, - (Green(first), Green(second)) => first == second, - (Blue(first), Blue(second )) => first == second, - _ => false - } - } -} - impl PartialOrd for CubeType { fn partial_cmp(&self, other: &Self) -> Option { match (self, other) { @@ -35,12 +25,12 @@ struct CubePull { cubes: Vec } -impl Into for String { - fn into(self) -> CubePull { - let cubes: Vec = self +impl From for CubePull { + fn from(data: String) -> Self { + let cubes: Vec = data .split(",") .map(|cube_type| cube_type.trim().split_once(" ").unwrap()) - .map(|(size_str, color)| (color, size_str.parse::().unwrap())) + .map(|(size_str, color)| (color, size_str.parse::().unwrap())) .map(|data| tuple_to_cube(data)) .collect(); @@ -57,31 +47,32 @@ pub fn execute_task01(content: &str) { println!("Day02 - Task01 - Sum of failed Games: {}", sum_of_failed_games) } -pub fn solve_01(content: &str) -> i32{ +pub fn solve_01(content: &str) -> usize{ let red = Red(12); let green = Green(13); let blue = Blue(14); content .lines() - .map(|line| { + .filter_map(|line| { let mut data =line - .split(":"); + .split_once(":").unwrap(); - let game_id = extract_game_id(data.next().unwrap()); - let data_string = data.next().unwrap(); + let game_id = extract_game_id(data.0); + let data_string = data.1; - let num_of_not_possible_pulls = data_string + let exist_not_possible_pulls = data_string .split(";") - .map(|pull| pull.to_string().into()) - .map(|cube: CubePull| cube.cubes) - .flatten() - .filter(|cube| cube.gt(&red) || cube.gt(&green) || cube.gt(&blue)) - .count(); + .map(|pull| CubePull::from(pull.to_owned())) + .flat_map(|cube| cube.cubes) + .any(|cube| cube > red || cube > green || cube > blue); - return (game_id, num_of_not_possible_pulls > 0) + if !exist_not_possible_pulls { + return Some(game_id) + } + + return None; }) - .map(|(index, failed)| return if !failed { index } else { 0 }) .sum() } @@ -92,39 +83,34 @@ pub fn execute_task02(content: &str) { println!("Day02 - Task02 - Sum of power of min. Cubes: {}", sum) } -pub fn solve_02(content: &str) -> i32{ +pub fn solve_02(content: &str) -> usize{ content .lines() .map(|line| { let data_string = line.split(":").last().unwrap(); - let mut red = 0; - let mut green = 0; - let mut blue = 0; - - data_string + let max_cubes = data_string .split(";") .map(|pull| pull.to_string().into()) - .map(|cube: CubePull| cube.cubes) - .flatten() - .for_each(|cube_type| match cube_type { - Red(size) => if size > red { red = size } - Green(size) => if size > green { green = size } - Blue(size) => if size > blue { blue = size } - _ => {} + .flat_map(|cube: CubePull| cube.cubes) + .fold((0 ,0 ,0), |(r, g, b), cube_type: CubeType| match cube_type { + Red(size) => (max(r, size), g, b), + Green(size) => (r, max(g, size), b), + Blue(size) => (r, g, max(b, size)), + UNKNOWN => (r, g, b) }); - return red * blue * green + return max_cubes.0 * max_cubes.1 * max_cubes.2 }) .sum() } -fn extract_game_id(name: &str) -> i32 { +fn extract_game_id(name: &str) -> usize { name.split(' ').last().map(|data| data.parse().unwrap()).unwrap() } -fn tuple_to_cube((color, size): (&str, i32)) -> CubeType { +fn tuple_to_cube((color, size): (&str, usize)) -> CubeType { match color { "red" => Red(size), "green" => Green(size),