summaryrefslogtreecommitdiff
path: root/2023/day02.rs
diff options
context:
space:
mode:
Diffstat (limited to '2023/day02.rs')
-rw-r--r--2023/day02.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/2023/day02.rs b/2023/day02.rs
new file mode 100644
index 0000000..3377acd
--- /dev/null
+++ b/2023/day02.rs
@@ -0,0 +1,69 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_mut)]
+use std::fs::File;
+use std::io::Read;
+use std::cmp::max;
+
+fn main() {
+ // let filename = "in/day02.ref";
+ let filename = "in/day02.pzl";
+
+ let mut f = File::open(filename).expect("cannot open file");
+ let mut content = String::new();
+ f.read_to_string(&mut content).expect("cannot read file");
+ let lines: Vec<_> = content.trim_end().split('\n').collect();
+ // println!("{:?}", lines);
+
+
+ let mut res1 = 0;
+ let mut res2 = 0;
+
+ for line in &lines {
+ let mut game: Vec<(u32,u32,u32)> = Vec::new();
+
+ let s = line.split(":").collect::<Vec<&str>>();
+ let game = s[0];
+ let subgame = s[1];
+
+ let game_n: u32 = game.split(" ").collect::<Vec<&str>>()[1].parse().unwrap();
+
+ let mut all = 1;
+ let mut min_r = 0;
+ let mut min_g = 0;
+ let mut min_b = 0;
+ let subgames_v = subgame.split(";");
+ for sg in subgames_v {
+ let mut r = 0;
+ let mut g = 0;
+ let mut b = 0;
+ for color in sg.split(",") {
+ let pair = color.trim().split(" ").collect::<Vec<&str>>();
+ let num: u32 = pair[0].parse().unwrap();
+ match pair[1] {
+ "red" => r += num,
+ "green" => g += num,
+ "blue" => b += num,
+ _ => (),
+ }
+ }
+ if (r > 12) || (g > 13) || (b > 14) {
+ all = 0;
+ }
+ min_r = max(min_r, r);
+ min_g = max(min_g, g);
+ min_b = max(min_b, b);
+ }
+
+ let power = min_r * min_g * min_b;
+ res2 += power;
+
+ if all == 1 {
+ res1 += game_n;
+ }
+
+ }
+
+ println!("res1: {}", res1);
+ println!("res2: {}", res2);
+}