use std::cmp::{max, Ordering}; use CubeType::*; #[derive(PartialEq)] enum CubeType { Red(usize), Green(usize), Blue(usize), UNKNOWN } impl PartialOrd for CubeType { fn partial_cmp(&self, other: &Self) -> Option { match (self, other) { (Red(first), Red(second )) => Some(first.cmp(second)), (Green(first), Green(second)) => Some(first.cmp(second)), (Blue(first), Blue(second )) => Some(first.cmp(second)), _ => None } } } struct CubePull { cubes: Vec } 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(|data| tuple_to_cube(data)) .collect(); return CubePull{ cubes } } } pub fn execute_task01(content: &str) { let sum_of_failed_games = solve_01(content); assert_eq!(sum_of_failed_games, 2528); println!("Day02 - Task01 - Sum of failed Games: {}", sum_of_failed_games) } pub fn solve_01(content: &str) -> usize{ let red = Red(12); let green = Green(13); let blue = Blue(14); content .lines() .filter_map(|line| { let data =line .split_once(":").unwrap(); let game_id = extract_game_id(data.0); let data_string = data.1; let exist_not_possible_pulls = data_string .split(";") .map(|pull| CubePull::from(pull.to_owned())) .flat_map(|cube| cube.cubes) .any(|cube| cube > red || cube > green || cube > blue); if !exist_not_possible_pulls { return Some(game_id) } return None; }) .sum() } pub fn execute_task02(content: &str) { let sum = solve_02(content); assert_eq!(sum, 67363); println!("Day02 - Task02 - Sum of power of min. Cubes: {}", sum) } pub fn solve_02(content: &str) -> usize{ content .lines() .map(|line| { let data_string = line.split(":").last().unwrap(); let max_cubes = data_string .split(";") .map(|pull| pull.to_string().into()) .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 max_cubes.0 * max_cubes.1 * max_cubes.2 }) .sum() } fn extract_game_id(name: &str) -> usize { name.split(' ').last().map(|data| data.parse().unwrap()).unwrap() } fn tuple_to_cube((color, size): (&str, usize)) -> CubeType { match color { "red" => Red(size), "green" => Green(size), "blue" => Blue(size), _ => UNKNOWN } } #[test] fn test_solve_01() { let test_input = r#"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"#; let solution = solve_01(test_input); assert_eq!(8, solution); } #[test] fn test_solve_02() { let test_input = r#"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"#; let solution = solve_02(test_input); assert_eq!(2286, solution); }