day_02 - part 2
This commit is contained in:
@@ -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::<i32>().unwrap() > blue {
|
||||
possible_game = false;
|
||||
}
|
||||
if color[2] == "red" && color[1].parse::<i32>().unwrap() > red {
|
||||
possible_game = false;
|
||||
}
|
||||
if color[2] == "green" && color[1].parse::<i32>().unwrap() > green {
|
||||
possible_game = false;
|
||||
}
|
||||
if color[2] == "blue" {
|
||||
let tmp_blue = color[1].parse::<u64>().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::<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 {
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user