day2 part2

This commit is contained in:
2024-09-16 20:08:15 +02:00
parent 00e06586e8
commit 91cd3287b3

View File

@@ -1,4 +1,4 @@
use std::{collections::HashMap, fs::File, io::{self, BufRead}, path::Path}; use std::{cmp, collections::HashMap, fs::File, io::{self, BufRead}, path::Path};
//struct to hold the game's information //struct to hold the game's information
#[derive(Debug)] #[derive(Debug)]
@@ -23,26 +23,27 @@ fn main() {
// Consumes the iterator, returns an (Optional) String // Consumes the iterator, returns an (Optional) String
for line in lines.flatten() { for line in lines.flatten() {
let game = parse_line(&line); let game = parse_line(&line);
println!("{game:?}");
println!("{line}"); println!("{line}");
let mut rule_break:bool = false; let mut rule_break:bool = false;
let mut red_max = 1;
let mut blue_max = 1;
let mut green_max = 1;
for draw in game.all_draws.draws{ for draw in game.all_draws.draws{
match draw.get("red"){ match draw.get("red"){
Some(amount) => if amount.gt(&12) {println!("{amount} > 12");rule_break = true; continue;}, Some(amount) => red_max = cmp::max(amount.clone(), red_max),
None => (), None => (),
} }
match draw.get("green"){ match draw.get("green"){
Some(amount) => if amount.gt(&13) {println!("{amount} > 13");rule_break = true; continue;}, Some(amount) => green_max = cmp::max(amount.clone(), green_max),
None => (), None => (),
} }
match draw.get("blue"){ match draw.get("blue"){
Some(amount) => if amount.gt(&14) {println!("{amount} > 14");rule_break = true; continue;}, Some(amount) => blue_max = cmp::max(amount.clone(), blue_max),
None => (), None => (),
} }
} }
if !rule_break{ println!("R = {red_max}\nG = {green_max}\nB = {blue_max}");
result+=game.game_num result += (red_max * blue_max * green_max);
}
} }
println!("{result}"); println!("{result}");
} }