2023-12-03 18:08:26 +00:00
|
|
|
use std::cmp::{max, Ordering};
|
|
|
|
use CubeType::*;
|
2023-12-02 22:37:29 +00:00
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
#[derive(PartialEq)]
|
2023-12-02 22:37:29 +00:00
|
|
|
enum CubeType {
|
2023-12-03 18:08:26 +00:00
|
|
|
Red(usize),
|
|
|
|
Green(usize),
|
|
|
|
Blue(usize),
|
2023-12-02 22:37:29 +00:00
|
|
|
UNKNOWN
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<Self> for CubeType {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
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<CubeType>
|
|
|
|
}
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
impl From<String> for CubePull {
|
|
|
|
fn from(data: String) -> Self {
|
|
|
|
let cubes: Vec<CubeType> = data
|
2023-12-02 22:37:29 +00:00
|
|
|
.split(",")
|
|
|
|
.map(|cube_type| cube_type.trim().split_once(" ").unwrap())
|
2023-12-03 18:08:26 +00:00
|
|
|
.map(|(size_str, color)| (color, size_str.parse::<usize>().unwrap()))
|
2023-12-02 22:37:29 +00:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
pub fn solve_01(content: &str) -> usize{
|
2023-12-02 22:37:29 +00:00
|
|
|
let red = Red(12);
|
|
|
|
let green = Green(13);
|
|
|
|
let blue = Blue(14);
|
|
|
|
|
|
|
|
content
|
|
|
|
.lines()
|
2023-12-03 18:08:26 +00:00
|
|
|
.filter_map(|line| {
|
2023-12-04 13:26:41 +00:00
|
|
|
let data =line
|
2023-12-03 18:08:26 +00:00
|
|
|
.split_once(":").unwrap();
|
2023-12-02 22:37:29 +00:00
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
let game_id = extract_game_id(data.0);
|
|
|
|
let data_string = data.1;
|
2023-12-02 22:37:29 +00:00
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
let exist_not_possible_pulls = data_string
|
2023-12-02 22:37:29 +00:00
|
|
|
.split(";")
|
2023-12-03 18:08:26 +00:00
|
|
|
.map(|pull| CubePull::from(pull.to_owned()))
|
|
|
|
.flat_map(|cube| cube.cubes)
|
|
|
|
.any(|cube| cube > red || cube > green || cube > blue);
|
2023-12-02 22:37:29 +00:00
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
if !exist_not_possible_pulls {
|
|
|
|
return Some(game_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
2023-12-02 22:37:29 +00:00
|
|
|
})
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
pub fn solve_02(content: &str) -> usize{
|
2023-12-02 22:37:29 +00:00
|
|
|
content
|
|
|
|
.lines()
|
|
|
|
.map(|line| {
|
|
|
|
let data_string = line.split(":").last().unwrap();
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
let max_cubes = data_string
|
2023-12-02 22:37:29 +00:00
|
|
|
.split(";")
|
|
|
|
.map(|pull| pull.to_string().into())
|
2023-12-03 18:08:26 +00:00
|
|
|
.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)
|
2023-12-02 22:37:29 +00:00
|
|
|
});
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
return max_cubes.0 * max_cubes.1 * max_cubes.2
|
2023-12-02 22:37:29 +00:00
|
|
|
})
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
fn extract_game_id(name: &str) -> usize {
|
2023-12-02 22:37:29 +00:00
|
|
|
name.split(' ').last().map(|data| data.parse().unwrap()).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-12-03 18:08:26 +00:00
|
|
|
fn tuple_to_cube((color, size): (&str, usize)) -> CubeType {
|
2023-12-02 22:37:29 +00:00
|
|
|
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);
|
|
|
|
}
|