day 2 part 2
This commit is contained in:
parent
3d03dc50d3
commit
bf4a5fce0e
@ -1,28 +1,36 @@
|
|||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::cmp;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let re = Regex::new("([0-9]+) (red|green|blue)([,;])?").unwrap();
|
let re = Regex::new("([0-9]+) (red|green|blue)").unwrap();
|
||||||
let mut current_id = 1;
|
let mut current_id = 1;
|
||||||
let bag: HashMap<&str, i32> = HashMap::from([
|
let task1_max: [i32; 3] = [12, 13, 14];
|
||||||
("red", 12), ("green", 13), ("blue", 14)
|
let color_index: HashMap<&str, usize> = HashMap::from([
|
||||||
|
("red", 0), ("green", 1), ("blue", 2)
|
||||||
]);
|
]);
|
||||||
let mut sum = 0;
|
let mut sum1 = 0;
|
||||||
|
let mut sum2 = 0;
|
||||||
for line in read_to_string("input.txt").unwrap().lines() {
|
for line in read_to_string("input.txt").unwrap().lines() {
|
||||||
let mut possible = true;
|
let mut possible = true;
|
||||||
|
let mut task2_min: [i32; 3] = [0; 3];
|
||||||
for captures in re.captures_iter(line) {
|
for captures in re.captures_iter(line) {
|
||||||
let amount = captures[1].parse::<i32>().unwrap();
|
let amount = captures[1].parse::<i32>().unwrap();
|
||||||
let color = &captures[2];
|
let color = &captures[2];
|
||||||
if amount > bag[color] {
|
if amount > task1_max[color_index[color]] {
|
||||||
possible = false;
|
possible = false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
let color_min = &mut task2_min[color_index[color]];
|
||||||
|
*color_min = cmp::max(*color_min, amount);
|
||||||
}
|
}
|
||||||
if possible {
|
if possible {
|
||||||
sum += current_id;
|
sum1 += current_id;
|
||||||
}
|
}
|
||||||
|
sum2 += task2_min[0] * task2_min[1] * task2_min[2];
|
||||||
|
println!("{}: {}, {}, {}", line, task2_min[0], task2_min[1], task2_min[2]);
|
||||||
current_id += 1;
|
current_id += 1;
|
||||||
}
|
}
|
||||||
println!("Sum: {}", sum);
|
println!("Sum 1: {}", sum1);
|
||||||
|
println!("Sum 2: {}", sum2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user