refactor: some nice code stuff

This commit is contained in:
Lennard Brinkhaus 2023-12-03 19:08:26 +01:00
parent 3dc05c1b9d
commit 85ca3caa1a

View File

@ -1,24 +1,14 @@
use std::cmp::Ordering; use std::cmp::{max, Ordering};
use crate::day02::CubeType::{Blue, Green, Red, UNKNOWN}; use CubeType::*;
#[derive(PartialEq)]
enum CubeType { enum CubeType {
Red(i32), Red(usize),
Green(i32), Green(usize),
Blue(i32), Blue(usize),
UNKNOWN UNKNOWN
} }
impl PartialEq<Self> 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<Self> for CubeType { impl PartialOrd<Self> for CubeType {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) { match (self, other) {
@ -35,12 +25,12 @@ struct CubePull {
cubes: Vec<CubeType> cubes: Vec<CubeType>
} }
impl Into<CubePull> for String { impl From<String> for CubePull {
fn into(self) -> CubePull { fn from(data: String) -> Self {
let cubes: Vec<CubeType> = self let cubes: Vec<CubeType> = data
.split(",") .split(",")
.map(|cube_type| cube_type.trim().split_once(" ").unwrap()) .map(|cube_type| cube_type.trim().split_once(" ").unwrap())
.map(|(size_str, color)| (color, size_str.parse::<i32>().unwrap())) .map(|(size_str, color)| (color, size_str.parse::<usize>().unwrap()))
.map(|data| tuple_to_cube(data)) .map(|data| tuple_to_cube(data))
.collect(); .collect();
@ -57,31 +47,32 @@ pub fn execute_task01(content: &str) {
println!("Day02 - Task01 - Sum of failed Games: {}", sum_of_failed_games) 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 red = Red(12);
let green = Green(13); let green = Green(13);
let blue = Blue(14); let blue = Blue(14);
content content
.lines() .lines()
.map(|line| { .filter_map(|line| {
let mut data =line let mut data =line
.split(":"); .split_once(":").unwrap();
let game_id = extract_game_id(data.next().unwrap()); let game_id = extract_game_id(data.0);
let data_string = data.next().unwrap(); let data_string = data.1;
let num_of_not_possible_pulls = data_string let exist_not_possible_pulls = data_string
.split(";") .split(";")
.map(|pull| pull.to_string().into()) .map(|pull| CubePull::from(pull.to_owned()))
.map(|cube: CubePull| cube.cubes) .flat_map(|cube| cube.cubes)
.flatten() .any(|cube| cube > red || cube > green || cube > blue);
.filter(|cube| cube.gt(&red) || cube.gt(&green) || cube.gt(&blue))
.count();
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() .sum()
} }
@ -92,39 +83,34 @@ pub fn execute_task02(content: &str) {
println!("Day02 - Task02 - Sum of power of min. Cubes: {}", sum) 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 content
.lines() .lines()
.map(|line| { .map(|line| {
let data_string = line.split(":").last().unwrap(); let data_string = line.split(":").last().unwrap();
let mut red = 0; let max_cubes = data_string
let mut green = 0;
let mut blue = 0;
data_string
.split(";") .split(";")
.map(|pull| pull.to_string().into()) .map(|pull| pull.to_string().into())
.map(|cube: CubePull| cube.cubes) .flat_map(|cube: CubePull| cube.cubes)
.flatten() .fold((0 ,0 ,0), |(r, g, b), cube_type: CubeType| match cube_type {
.for_each(|cube_type| match cube_type { Red(size) => (max(r, size), g, b),
Red(size) => if size > red { red = size } Green(size) => (r, max(g, size), b),
Green(size) => if size > green { green = size } Blue(size) => (r, g, max(b, size)),
Blue(size) => if size > blue { blue = size } UNKNOWN => (r, g, b)
_ => {}
}); });
return red * blue * green return max_cubes.0 * max_cubes.1 * max_cubes.2
}) })
.sum() .sum()
} }
fn extract_game_id(name: &str) -> i32 { fn extract_game_id(name: &str) -> usize {
name.split(' ').last().map(|data| data.parse().unwrap()).unwrap() 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 { match color {
"red" => Red(size), "red" => Red(size),
"green" => Green(size), "green" => Green(size),