This commit is contained in:
Acvaxoort
2023-12-02 23:52:46 +01:00
commit 3d03dc50d3
6 changed files with 1198 additions and 0 deletions

28
day2/src/main.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::fs::read_to_string;
use std::collections::HashMap;
use regex::Regex;
fn main() {
let re = Regex::new("([0-9]+) (red|green|blue)([,;])?").unwrap();
let mut current_id = 1;
let bag: HashMap<&str, i32> = HashMap::from([
("red", 12), ("green", 13), ("blue", 14)
]);
let mut sum = 0;
for line in read_to_string("input.txt").unwrap().lines() {
let mut possible = true;
for captures in re.captures_iter(line) {
let amount = captures[1].parse::<i32>().unwrap();
let color = &captures[2];
if amount > bag[color] {
possible = false;
break;
}
}
if possible {
sum += current_id;
}
current_id += 1;
}
println!("Sum: {}", sum);
}