From 7987d3359f5bb036e299ac78881d8dde95ad14a2 Mon Sep 17 00:00:00 2001 From: Christian Nachtigall Date: Wed, 6 Dec 2023 08:13:08 +0100 Subject: [PATCH] day_02 - part 2 --- day_02/src/main.rs | 79 ++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/day_02/src/main.rs b/day_02/src/main.rs index 3dfed4e..dae38db 100644 --- a/day_02/src/main.rs +++ b/day_02/src/main.rs @@ -9,52 +9,75 @@ fn main() { let mut total_part_2: u64 = 0; // Winning condition - let red = 12; - let green = 13; - let blue = 14; + let red: u64 = 12; + let green: u64 = 13; + let blue: u64 = 14; // Consumes the iterator, returns an (Optional) String for line in lines { if let Ok(mut l) = line { - // Part 1 l = l.replace("Game ", ""); // Split game_number and game details if let Some((game_number, game)) = l.split_once(":") { let mut possible_game = true; + let mut small_red: u64 = 1; + let mut small_green: u64 = 1; + let mut small_blue: u64 = 1; - // Split game into subsets - let subsets: Vec<_> = game.split(";").collect(); + // Split game into subsets + let subsets: Vec<_> = game.split(";").collect(); - subsets.iter().for_each(|subset| { - // Split subsets into colors - let set: Vec<_> = subset.split(",").collect(); + subsets.iter().for_each(|subset| { + // Split subsets into colors + let set: Vec<_> = subset.split(",").collect(); - set.iter().for_each(|colors| { - if possible_game { - // Check colors for possible games - let color: Vec<_> = colors.split(" ").collect(); + set.iter().for_each(|colors| { + // Check colors for possible games + let color: Vec<_> = colors.split(" ").collect(); - if color[2] == "blue" && color[1].parse::().unwrap() > blue { - possible_game = false; - } - if color[2] == "red" && color[1].parse::().unwrap() > red { - possible_game = false; - } - if color[2] == "green" && color[1].parse::().unwrap() > green { - possible_game = false; - } + if color[2] == "blue" { + let tmp_blue = color[1].parse::().unwrap(); + // Part 1 + if tmp_blue > blue { + possible_game = false; } - }) - }); - + // Part 2 + if small_blue == 1 || tmp_blue > small_blue { + small_blue = tmp_blue; + } + } + if color[2] == "red" { + let tmp_red = color[1].parse::().unwrap(); + // Part 1 + if tmp_red > red { + possible_game = false; + } + // Part 2 + if small_red == 1 || tmp_red > small_red { + small_red = tmp_red; + } + } + if color[2] == "green" { + let tmp_green = color[1].parse::().unwrap(); + // Part 1 + if tmp_green > green { + possible_game = false; + } + // Part 2 + if small_green == 1 || tmp_green > small_green{ + small_green = tmp_green; + } + } + }) + }); + // Part 1 if possible_game { total_part_1 = total_part_1 + game_number.parse::().unwrap(); } + // Part 2 + total_part_2 = total_part_2 + (small_green * small_red * small_blue) } - - // Part 2 - } }