day_02 - part 2

This commit is contained in:
2023-12-06 08:13:08 +01:00
parent 316c86a86a
commit 7987d3359f

View File

@@ -9,52 +9,75 @@ fn main() {
let mut total_part_2: u64 = 0; let mut total_part_2: u64 = 0;
// Winning condition // Winning condition
let red = 12; let red: u64 = 12;
let green = 13; let green: u64 = 13;
let blue = 14; let blue: u64 = 14;
// Consumes the iterator, returns an (Optional) String // Consumes the iterator, returns an (Optional) String
for line in lines { for line in lines {
if let Ok(mut l) = line { if let Ok(mut l) = line {
// Part 1
l = l.replace("Game ", ""); l = l.replace("Game ", "");
// Split game_number and game details // Split game_number and game details
if let Some((game_number, game)) = l.split_once(":") { if let Some((game_number, game)) = l.split_once(":") {
let mut possible_game = true; 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 // Split game into subsets
let subsets: Vec<_> = game.split(";").collect(); let subsets: Vec<_> = game.split(";").collect();
subsets.iter().for_each(|subset| { subsets.iter().for_each(|subset| {
// Split subsets into colors // Split subsets into colors
let set: Vec<_> = subset.split(",").collect(); let set: Vec<_> = subset.split(",").collect();
set.iter().for_each(|colors| { set.iter().for_each(|colors| {
if possible_game { // Check colors for possible games
// Check colors for possible games let color: Vec<_> = colors.split(" ").collect();
let color: Vec<_> = colors.split(" ").collect();
if color[2] == "blue" && color[1].parse::<i32>().unwrap() > blue { if color[2] == "blue" {
possible_game = false; let tmp_blue = color[1].parse::<u64>().unwrap();
} // Part 1
if color[2] == "red" && color[1].parse::<i32>().unwrap() > red { if tmp_blue > blue {
possible_game = false; possible_game = false;
}
if color[2] == "green" && color[1].parse::<i32>().unwrap() > green {
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::<u64>().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::<u64>().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 { if possible_game {
total_part_1 = total_part_1 + game_number.parse::<u64>().unwrap(); total_part_1 = total_part_1 + game_number.parse::<u64>().unwrap();
} }
// Part 2
total_part_2 = total_part_2 + (small_green * small_red * small_blue)
} }
// Part 2
} }
} }