solution for day 2 part 2
This commit is contained in:
parent
9bc58421c7
commit
257a9bdb91
@ -4,38 +4,34 @@ const RED_MAX: u32 = 12;
|
|||||||
const GREEN_MAX: u32 = 13;
|
const GREEN_MAX: u32 = 13;
|
||||||
const BLUE_MAX: u32 = 14;
|
const BLUE_MAX: u32 = 14;
|
||||||
|
|
||||||
static mut RED_MIN: u32 = 0;
|
|
||||||
static mut GREEN_MIN: u32 = 0;
|
|
||||||
static mut BLUE_MIN: u32 = 0;
|
|
||||||
|
|
||||||
pub fn process() -> u32 {
|
pub fn process() -> u32 {
|
||||||
let file_path = "./input_sources/day2short.txt";
|
let file_path = "./input_sources/day2.txt";
|
||||||
let mut total: u32 = 0;
|
let mut total: u32 = 0;
|
||||||
if let Ok(lines) = read_lines(file_path) {
|
if let Ok(lines) = read_lines(file_path) {
|
||||||
for game in lines.flatten() {
|
for game in lines.flatten() {
|
||||||
check_if_game_is_valid(game.clone());
|
total = total + get_color_minimums(game.clone());
|
||||||
// println!("{:?}", is_game_valid);
|
//println!("{:?}", total);
|
||||||
if is_game_valid {
|
|
||||||
let game_id: u32 = get_game_id(game.clone());
|
|
||||||
total = total + game_id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
total
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_game_id(input_line: String) -> u32 {
|
fn get_game_id(input_line: String) -> u32 {
|
||||||
let game_section: Vec<&str> = input_line.split(":").collect();
|
let game_section: Vec<&str> = input_line.split(":").collect();
|
||||||
let game_section_split: Vec<&str> = game_section[0].split(" ").collect();
|
let game_section_split: Vec<&str> = game_section[0].split(" ").collect();
|
||||||
game_section_split[1].trim().parse().unwrap()
|
game_section_split[1].trim().parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_if_game_is_valid(game: String) -> bool {
|
fn get_color_minimums(game: String) -> u32 {
|
||||||
let split_on_colon: Vec<&str> = game.split(":").collect();
|
let split_on_colon: Vec<&str> = game.split(":").collect();
|
||||||
let game_results: Vec<&str> = split_on_colon[1].split(";").collect();
|
let game_results: Vec<&str> = split_on_colon[1].split(";").collect();
|
||||||
let mut valid_game: bool = true;
|
let mut valid_game: bool = true;
|
||||||
|
|
||||||
|
let mut total: u32 = 1;
|
||||||
|
let mut green_min: u32 = 0;
|
||||||
|
let mut red_min: u32 = 0;
|
||||||
|
let mut blue_min: u32 = 0;
|
||||||
|
|
||||||
for game_result in game_results {
|
for game_result in game_results {
|
||||||
let selections: Vec<&str> = game_result.split(",").collect();
|
let selections: Vec<&str> = game_result.split(",").collect();
|
||||||
for selection in selections {
|
for selection in selections {
|
||||||
@ -45,24 +41,36 @@ fn check_if_game_is_valid(game: String) -> bool {
|
|||||||
|
|
||||||
match color_selected {
|
match color_selected {
|
||||||
"green" => {
|
"green" => {
|
||||||
if num_selected > GREEN_MIN {
|
if num_selected > green_min {
|
||||||
GREEN_MIN = num_selected;
|
green_min = num_selected;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"blue" => {
|
"blue" => {
|
||||||
if num_selected > BLUE_MIN {
|
if num_selected > blue_min {
|
||||||
BLUE_MIN = num_selected;
|
blue_min = num_selected;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"red" => {
|
"red" => {
|
||||||
if num_selected > RED_MIN {
|
if num_selected > red_min {
|
||||||
RED_MIN = num_selected;
|
red_min = num_selected;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
&_ => todo!()
|
&_ => todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
valid_game
|
if green_min > 0 {
|
||||||
|
total = green_min * total;
|
||||||
|
}
|
||||||
|
|
||||||
|
if red_min > 0 {
|
||||||
|
total = red_min * total;
|
||||||
|
}
|
||||||
|
|
||||||
|
if blue_min > 0 {
|
||||||
|
total = blue_min * total;
|
||||||
|
}
|
||||||
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user