day2part2

This commit is contained in:
Candygoblen123 2023-12-02 22:11:35 -05:00
parent 356aa5ce59
commit 29ff13fb86
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385

View File

@ -1,3 +1,37 @@
use std::{fs, cmp::max};
fn main() {
let input = fs::read_to_string("input.txt").unwrap();
let input: Vec<_> = input.split('\n').collect();
let mut sum = 0;
for line in input {
let split:Vec<_> = line.split(':').collect();
let rounds: Vec<_> = split.last().unwrap().split(';').collect();
let rounds: Vec<_> = rounds.iter().map(|x| x.split(',').collect::<Vec<_>>()).collect();
let rounds: Vec<_> = rounds.iter().map(|x| x.iter().map(|x| x.trim().split(' ').collect::<Vec<_>>()).collect::<Vec<_>>()).collect();
let (mut r_max, mut g_max, mut b_max) = (0, 0, 0);
for round in rounds {
for set in round {
let color = set.last().unwrap();
let num: i32 = set.first().unwrap().parse().unwrap();
match *color {
"red" => r_max = max(num, r_max),
"blue" => b_max = max(num, b_max),
"green" => g_max = max(num, g_max),
&_ => todo!()
}
}
}
//println!("{}", r_max);
sum += r_max * g_max * b_max;
}
println!("{}", sum);
}
// use std::fs;
// fn main() {