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
#[derive(Debug)]
@@ -23,26 +23,27 @@ fn main() {
// Consumes the iterator, returns an (Optional) String
for line in lines.flatten() {
let game = parse_line(&line);
println!("{game:?}");
println!("{line}");
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{
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 => (),
}
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 => (),
}
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 => (),
}
}
if !rule_break{
result+=game.game_num
}
println!("R = {red_max}\nG = {green_max}\nB = {blue_max}");
result += (red_max * blue_max * green_max);
}
println!("{result}");
}