aboutsummaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorMatias Linares <matias@deprecated.org>2023-12-03 20:20:11 -0300
committerMatias Linares <matias@deprecated.org>2023-12-03 20:20:11 -0300
commita4e55ef3c10e2de2fd47e2b8b0042e7db790d937 (patch)
tree03da2954ef180cf773eef1a14606d2ca6b4691be /2023
parent815a5cc136665f79ccab88e25a12242867f1d235 (diff)
downloadadvent-of-code-a4e55ef3c10e2de2fd47e2b8b0042e7db790d937.tar.gz
Add day 2 :)
Diffstat (limited to '2023')
-rw-r--r--2023/day-01/.projectile0
-rw-r--r--2023/day-02/.projectile0
-rw-r--r--2023/day-02/Cargo.lock7
-rw-r--r--2023/day-02/Cargo.toml8
-rw-r--r--2023/day-02/src/bin/input.txt100
-rw-r--r--2023/day-02/src/bin/part1.rs106
-rw-r--r--2023/day-02/src/bin/part2.rs140
7 files changed, 361 insertions, 0 deletions
diff --git a/2023/day-01/.projectile b/2023/day-01/.projectile
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/2023/day-01/.projectile
diff --git a/2023/day-02/.projectile b/2023/day-02/.projectile
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/2023/day-02/.projectile
diff --git a/2023/day-02/Cargo.lock b/2023/day-02/Cargo.lock
new file mode 100644
index 0000000..9005d7e
--- /dev/null
+++ b/2023/day-02/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "day-02"
+version = "0.1.0"
diff --git a/2023/day-02/Cargo.toml b/2023/day-02/Cargo.toml
new file mode 100644
index 0000000..6f0f217
--- /dev/null
+++ b/2023/day-02/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day-02"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/2023/day-02/src/bin/input.txt b/2023/day-02/src/bin/input.txt
new file mode 100644
index 0000000..4f7922d
--- /dev/null
+++ b/2023/day-02/src/bin/input.txt
@@ -0,0 +1,100 @@
+Game 1: 1 red, 5 blue, 1 green; 16 blue, 3 red; 6 blue, 5 red; 4 red, 7 blue, 1 green
+Game 2: 4 blue; 4 red, 3 blue, 1 green; 4 red, 9 blue, 2 green; 5 blue, 7 green, 4 red
+Game 3: 10 blue; 7 blue, 1 green; 19 blue, 1 green, 9 red
+Game 4: 2 green; 14 blue, 14 red, 4 green; 12 red, 11 green, 13 blue; 5 green, 9 red, 4 blue; 9 red, 7 green, 12 blue; 2 green, 3 blue, 8 red
+Game 5: 3 blue, 4 red; 12 red, 2 green, 15 blue; 1 red, 10 blue, 1 green
+Game 6: 1 blue, 7 red; 3 green, 5 red, 1 blue; 1 green, 7 red; 6 red, 1 blue, 4 green; 1 green, 8 red, 1 blue; 2 green, 4 red, 1 blue
+Game 7: 11 green, 10 blue, 2 red; 1 green, 12 blue, 2 red; 9 green, 14 blue; 1 red, 19 blue, 15 green
+Game 8: 4 green, 2 red, 14 blue; 9 green, 1 red, 15 blue; 2 green, 9 red, 8 blue; 11 green, 7 red, 8 blue; 9 red, 7 green, 6 blue
+Game 9: 4 blue, 1 green, 2 red; 1 blue, 3 red; 1 red, 3 blue, 3 green
+Game 10: 4 red, 3 green, 6 blue; 2 green, 15 blue, 6 red; 3 green, 2 blue; 2 red, 1 green; 11 blue, 7 red, 4 green; 2 blue, 2 red, 4 green
+Game 11: 10 red, 1 green, 7 blue; 5 red, 2 green, 7 blue; 2 green, 4 blue; 1 green, 10 red, 10 blue; 8 blue, 4 red
+Game 12: 6 green, 2 blue, 7 red; 3 green, 2 blue, 3 red; 6 red, 1 blue, 9 green; 9 green, 13 red, 5 blue; 6 green, 4 blue, 8 red
+Game 13: 10 green, 4 red, 6 blue; 19 red, 6 green, 7 blue; 6 blue, 5 red, 8 green
+Game 14: 4 blue, 2 green; 19 blue; 6 red, 17 blue; 10 blue, 7 red; 1 green, 2 blue, 7 red
+Game 15: 4 green, 12 blue, 15 red; 10 blue, 18 green, 13 red; 20 blue, 6 green, 10 red; 20 red, 12 blue, 13 green; 12 blue, 17 green, 10 red; 1 red, 3 blue, 7 green
+Game 16: 1 blue, 6 red, 5 green; 3 red, 3 green; 5 green, 1 red; 2 red, 1 blue, 6 green; 1 blue, 1 red, 6 green; 1 blue, 6 green
+Game 17: 4 red, 3 blue, 3 green; 8 blue, 8 green; 5 red, 3 green, 9 blue; 9 green, 12 blue, 13 red; 1 green, 1 blue, 5 red; 7 green, 6 red
+Game 18: 2 green, 11 blue, 6 red; 2 green, 11 red, 2 blue; 7 red, 4 blue, 9 green; 18 blue, 6 red, 1 green
+Game 19: 4 red, 7 green, 17 blue; 5 green, 6 red, 4 blue; 4 blue, 4 red, 1 green
+Game 20: 2 blue, 5 green, 9 red; 4 green, 8 red, 10 blue; 7 blue, 9 red, 1 green; 1 green, 10 blue, 9 red; 1 green, 8 red; 8 blue, 8 red, 1 green
+Game 21: 1 blue, 14 red; 1 green, 2 red; 9 red, 1 blue, 1 green
+Game 22: 7 green, 9 red, 4 blue; 9 red, 7 green, 9 blue; 8 green, 14 red; 5 blue; 10 red, 1 blue, 1 green; 8 green
+Game 23: 2 red, 12 green, 5 blue; 3 red, 5 blue, 3 green; 1 red, 9 green, 1 blue; 8 green, 6 blue; 13 green
+Game 24: 8 red, 7 green, 1 blue; 1 red, 6 green, 7 blue; 1 green, 3 red
+Game 25: 4 green, 2 red; 1 red, 2 green, 8 blue; 1 green; 8 blue
+Game 26: 1 green, 4 blue, 17 red; 15 red, 3 green, 3 blue; 2 blue, 2 red; 18 red, 2 green, 11 blue; 6 red, 7 blue; 10 blue, 1 green, 4 red
+Game 27: 2 red, 5 blue, 1 green; 14 green, 2 red, 6 blue; 1 red, 4 blue, 14 green
+Game 28: 3 red, 5 green, 2 blue; 2 red, 3 green, 4 blue; 1 red, 9 green, 3 blue; 13 green, 4 red, 4 blue
+Game 29: 18 red, 11 green; 4 blue, 18 red, 9 green; 16 red, 2 green, 4 blue; 2 red, 3 blue, 12 green; 1 green, 18 red; 2 blue, 15 green, 1 red
+Game 30: 10 red, 3 blue, 1 green; 6 red, 1 blue, 3 green; 2 green, 2 blue, 10 red; 6 green; 3 blue, 15 red
+Game 31: 1 blue, 7 green, 2 red; 12 red, 8 green, 4 blue; 2 green, 2 blue, 5 red; 2 blue, 3 green, 12 red; 7 red, 5 green, 4 blue; 7 red, 1 blue
+Game 32: 4 blue, 5 red, 11 green; 20 red, 8 green, 1 blue; 10 red, 7 green, 1 blue; 1 blue, 7 red, 2 green; 1 red, 19 green, 3 blue
+Game 33: 9 red; 9 red, 6 green, 7 blue; 5 red, 7 blue, 2 green
+Game 34: 5 green, 5 red, 3 blue; 8 green, 6 blue, 16 red; 12 blue, 8 red, 8 green; 1 blue, 10 red, 3 green; 1 green, 13 blue, 18 red; 4 blue, 5 green, 8 red
+Game 35: 15 green, 4 red, 8 blue; 7 red, 1 green, 14 blue; 12 green, 16 blue, 2 red
+Game 36: 3 blue, 3 green, 2 red; 7 red, 8 blue; 11 blue, 9 red; 4 red, 13 blue, 1 green
+Game 37: 4 red, 11 blue, 8 green; 6 green, 4 blue, 14 red; 5 blue, 7 green, 13 red; 6 red, 2 green, 5 blue; 4 red, 3 blue, 1 green; 6 red, 4 green, 6 blue
+Game 38: 10 green, 5 blue, 1 red; 3 red, 6 blue, 3 green; 9 green, 9 blue, 3 red; 9 blue, 1 red, 6 green
+Game 39: 3 blue, 16 red; 10 red, 4 green, 2 blue; 2 blue, 13 red, 1 green; 2 blue, 11 red, 2 green; 3 green, 13 red
+Game 40: 2 blue, 3 red, 2 green; 2 green, 2 blue, 6 red; 1 green, 9 red
+Game 41: 1 blue, 12 red; 8 blue, 1 red, 5 green; 1 green, 7 blue, 13 red; 8 red, 7 blue, 7 green; 4 green, 17 red, 9 blue; 2 green, 8 blue
+Game 42: 2 green, 6 red, 1 blue; 3 red, 2 green; 9 red, 1 green; 2 red, 2 green
+Game 43: 10 blue, 9 red; 14 blue, 4 green; 5 red, 3 green, 9 blue; 5 blue, 8 green, 1 red
+Game 44: 3 blue, 10 green, 1 red; 1 blue, 13 red, 3 green; 1 blue, 5 green, 16 red
+Game 45: 1 red, 1 green, 3 blue; 2 green, 1 red, 5 blue; 1 red, 2 blue, 1 green; 1 blue; 1 green, 5 blue; 1 blue
+Game 46: 8 green, 8 blue, 4 red; 10 green, 4 red, 7 blue; 2 red, 3 green, 14 blue
+Game 47: 3 green, 3 red; 5 green, 2 blue, 6 red; 3 blue, 5 red, 15 green; 2 green, 2 blue, 2 red
+Game 48: 11 blue, 12 green, 3 red; 8 blue, 3 red, 3 green; 1 green, 6 blue, 2 red
+Game 49: 3 blue, 17 green, 1 red; 4 red, 16 blue, 17 green; 1 green, 3 red, 5 blue; 14 blue, 1 red, 12 green
+Game 50: 2 blue, 5 red, 6 green; 8 blue, 11 green, 5 red; 2 green, 2 red, 6 blue
+Game 51: 1 green, 2 red; 4 green; 1 blue, 10 green
+Game 52: 8 blue, 9 red, 4 green; 2 green, 8 blue, 2 red; 1 red, 2 green, 1 blue; 2 blue, 8 green, 8 red; 4 red, 1 green, 9 blue; 11 blue, 4 green, 8 red
+Game 53: 1 green, 2 red; 3 blue, 1 green, 9 red; 5 blue, 11 red; 4 blue, 6 red, 1 green; 5 blue, 10 red; 5 blue, 5 red, 1 green
+Game 54: 1 blue, 8 green; 9 green, 1 red, 11 blue; 16 green, 8 blue; 5 green
+Game 55: 7 blue, 2 red, 1 green; 16 green, 19 blue, 5 red; 9 green, 3 blue, 7 red; 8 blue, 2 green, 4 red; 8 green, 15 blue, 5 red
+Game 56: 9 blue, 1 red, 4 green; 12 green, 12 blue; 1 green, 1 red, 5 blue
+Game 57: 1 green, 10 blue; 1 red, 9 blue; 10 blue, 1 red, 3 green
+Game 58: 6 red, 15 blue, 3 green; 13 blue, 5 red; 10 blue, 2 red; 5 red, 1 green, 14 blue
+Game 59: 7 red, 1 blue, 9 green; 4 green, 12 red, 2 blue; 6 green, 20 red, 1 blue; 4 blue, 9 red, 2 green; 8 red, 4 blue, 2 green
+Game 60: 11 red, 8 blue, 1 green; 18 green, 11 blue; 16 red, 10 blue, 7 green; 6 blue, 8 red; 7 red, 15 green, 4 blue
+Game 61: 1 blue, 1 green, 8 red; 3 red, 7 blue; 4 blue, 10 red; 1 green, 5 red, 8 blue; 10 red, 7 blue
+Game 62: 12 blue, 1 red, 1 green; 2 green, 1 red, 7 blue; 3 green, 18 blue; 11 blue, 4 green
+Game 63: 4 green, 4 red, 8 blue; 7 red, 5 blue, 5 green; 2 green, 20 blue, 4 red; 1 green, 4 blue, 3 red
+Game 64: 2 green, 2 red; 3 green, 2 blue; 12 green, 2 red, 4 blue; 5 red, 9 green, 8 blue; 7 blue, 6 green; 3 green, 5 red
+Game 65: 8 red, 2 green, 13 blue; 11 blue; 7 blue, 2 green; 12 blue, 1 green, 9 red
+Game 66: 1 blue, 3 red, 19 green; 3 red, 17 blue, 15 green; 9 green, 9 blue
+Game 67: 2 green, 7 blue, 1 red; 3 green, 1 red, 7 blue; 1 red, 6 green; 7 blue, 2 red, 10 green; 2 red, 5 green, 4 blue
+Game 68: 14 red, 10 green, 8 blue; 11 red, 1 blue, 6 green; 7 red, 7 green; 12 blue, 10 green, 3 red; 6 red, 12 blue, 10 green; 8 green, 14 red, 3 blue
+Game 69: 4 green, 8 red; 2 red, 15 green; 5 red, 1 blue, 12 green; 13 red, 6 green; 10 green, 13 red, 1 blue
+Game 70: 3 red, 10 blue, 3 green; 8 red, 11 blue, 11 green; 5 red, 13 green
+Game 71: 18 green, 3 red, 1 blue; 3 blue, 14 green, 2 red; 6 blue, 20 green, 4 red
+Game 72: 2 blue, 1 red; 2 blue, 3 green, 1 red; 4 blue, 2 red, 4 green
+Game 73: 11 red, 11 green; 5 green, 1 blue; 8 red, 7 green, 4 blue; 5 blue, 7 red, 12 green
+Game 74: 12 red, 12 green, 5 blue; 10 red, 7 blue, 15 green; 6 green, 19 red, 19 blue; 3 red, 7 blue, 16 green; 11 red, 14 green, 16 blue
+Game 75: 5 red, 17 green, 8 blue; 10 red, 8 blue, 19 green; 9 blue, 6 red, 18 green; 3 blue, 13 red, 12 green
+Game 76: 5 green, 2 red, 8 blue; 3 blue, 14 red, 2 green; 14 red, 1 blue; 3 green, 8 blue, 15 red; 11 red, 1 green; 11 red, 9 blue, 3 green
+Game 77: 3 blue, 2 red; 1 blue, 8 green, 11 red; 14 green, 14 red; 3 red, 5 green, 5 blue; 2 green, 16 blue, 3 red; 13 red, 7 green, 5 blue
+Game 78: 3 blue, 1 green, 1 red; 5 blue, 1 green, 1 red; 9 blue, 7 red, 1 green; 5 blue, 1 green, 5 red; 10 blue, 3 green, 7 red
+Game 79: 19 green, 17 blue, 4 red; 7 green, 7 red, 16 blue; 4 red, 10 green; 13 blue, 17 green, 2 red
+Game 80: 9 blue, 3 green; 15 blue, 1 red; 3 blue, 12 green, 2 red; 1 red, 14 green, 13 blue; 1 red, 10 blue, 16 green; 8 blue, 6 green, 2 red
+Game 81: 1 green, 3 red, 19 blue; 2 red, 1 green, 9 blue; 1 green, 2 red, 8 blue; 1 red, 1 green, 11 blue; 1 green, 3 red, 11 blue
+Game 82: 8 red, 1 blue, 4 green; 9 green, 3 blue, 4 red; 3 green, 3 blue, 18 red
+Game 83: 3 red, 13 blue, 16 green; 16 green, 2 blue; 14 green, 12 blue; 8 green, 14 blue, 4 red; 12 green, 4 blue; 20 green, 1 red
+Game 84: 4 green, 4 blue, 5 red; 6 red, 6 blue, 8 green; 5 blue, 12 green, 3 red; 5 red, 13 green; 6 blue, 1 green, 5 red
+Game 85: 10 green; 7 green, 1 blue; 5 red, 5 blue, 1 green; 2 green, 2 red, 3 blue; 3 red, 10 green, 3 blue; 1 blue, 1 red
+Game 86: 3 green, 1 red, 3 blue; 2 red, 2 green; 9 green, 2 blue, 3 red; 3 red, 3 blue, 4 green
+Game 87: 6 red, 4 green; 1 red, 3 green, 5 blue; 1 green, 7 blue, 4 red
+Game 88: 2 green, 4 red, 3 blue; 5 green, 1 blue; 3 red, 5 green, 2 blue; 1 green, 6 red, 1 blue; 7 red, 2 blue; 17 red, 13 green
+Game 89: 4 green, 2 blue, 6 red; 15 red, 7 green, 10 blue; 7 red, 9 blue, 4 green
+Game 90: 9 red, 17 blue; 1 green, 9 blue; 5 red, 8 blue; 3 blue, 9 red, 1 green; 17 blue, 1 red
+Game 91: 7 green, 3 red, 5 blue; 4 blue, 3 red, 9 green; 9 red, 7 blue, 7 green; 5 red, 6 blue, 3 green; 10 red, 2 green, 6 blue
+Game 92: 13 blue, 8 red; 7 green, 1 red, 8 blue; 5 blue, 4 red, 2 green; 9 red, 10 blue
+Game 93: 6 green; 1 blue, 16 green, 6 red; 5 green, 1 blue, 5 red; 5 red, 6 green; 16 green, 2 red, 1 blue; 11 green, 2 red
+Game 94: 9 blue, 4 green; 12 green, 17 blue; 4 green, 5 blue, 6 red; 2 red, 2 blue, 12 green
+Game 95: 5 red, 4 blue, 5 green; 2 blue, 4 green, 4 red; 4 blue, 2 red, 7 green; 1 green, 7 blue, 8 red
+Game 96: 7 blue, 6 green, 2 red; 3 green, 1 blue; 7 blue, 3 red, 5 green; 1 green, 5 blue; 6 blue, 2 red; 2 green, 1 red
+Game 97: 10 red, 1 green, 1 blue; 4 green, 11 red, 2 blue; 4 red, 1 blue, 4 green
+Game 98: 3 green, 4 blue, 7 red; 7 red, 8 green; 7 green, 16 red, 1 blue; 8 green, 2 blue, 4 red; 5 green, 3 blue, 18 red
+Game 99: 6 green, 12 red, 1 blue; 5 blue, 1 red, 7 green; 5 green, 7 red, 10 blue; 8 blue, 1 red, 7 green; 17 red, 4 blue, 9 green
+Game 100: 6 blue, 10 green; 3 green, 4 blue, 1 red; 7 blue, 1 red, 12 green
diff --git a/2023/day-02/src/bin/part1.rs b/2023/day-02/src/bin/part1.rs
new file mode 100644
index 0000000..f49f409
--- /dev/null
+++ b/2023/day-02/src/bin/part1.rs
@@ -0,0 +1,106 @@
+
+struct GameSet {
+ red: u32,
+ green: u32,
+ blue: u32
+}
+
+impl GameSet {
+ fn is_valid(&self) -> bool {
+ self.red <= 12 && self.green <= 13 && self.blue <= 14
+ }
+}
+
+struct Game {
+ id: u32,
+ sets: Vec<GameSet>
+}
+
+fn main() {
+ let input = include_str!("./input.txt");
+ let output: u32 = input
+ .lines()
+ .map(|line| {
+ process_line(line)
+ })
+ .filter(|game| {
+ game.sets.iter().all(|set: &GameSet| set.is_valid())
+ })
+ .map(|game| {
+ game.id
+ }).sum();
+ println!("Result {output}")
+}
+
+fn gameset_from_pairs(pairs: Vec<(&str, u32)>) -> GameSet {
+ let mut red: u32 = 0;
+ let mut green: u32 = 0;
+ let mut blue: u32 = 0;
+
+ for pair in pairs {
+ match pair {
+ ("red", x) => { red = x },
+ ("green", x) => { green = x },
+ ("blue", x) => { blue = x },
+ what => panic!("Unknown pair {:?}", what)
+ }
+ }
+
+ GameSet { red, green, blue }
+}
+
+/// Progrss a line
+fn process_line(string: &str) -> Game {
+ let game_split: Vec<&str> = string.splitn(2, ": ").collect();
+ // Get game Id from string "Game 1"
+ let game_id = game_split[0].split(" ").last().expect("Expected id").parse::<u32>().expect("Cant parse game id");
+ let sets: Vec<_> = game_split[1]
+ .split("; ")
+ .map(|set_line| {
+ set_line.split(", ").collect()
+ })
+ .map(|gameset: Vec<&str>| {
+ let pairs: Vec<(&str, u32)> = gameset.into_iter().map(|s| {
+ let split: Vec<&str> = s.split_whitespace().collect();
+ (split[1], split[0].parse::<u32>().expect("Cant parse"))
+ }).collect();
+ gameset_from_pairs(pairs)
+ })
+ .collect();
+ Game {
+ id: game_id,
+ sets
+ }
+}
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_process_simple() {
+ let game = process_line("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green");
+ assert_eq!(game.id, 1);
+ }
+
+ #[test]
+ fn test_input() {
+ let input = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
+Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
+Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
+Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
+Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
+ let output: u32 = input.lines().map(|line| {
+ process_line(line)
+ })
+ .filter(|game| {
+ game.sets.iter().all(|set: &GameSet| set.is_valid())
+ })
+ .map(|game| {
+ game.id
+ }).sum();
+
+ assert_eq!(output, 8);
+ }
+}
diff --git a/2023/day-02/src/bin/part2.rs b/2023/day-02/src/bin/part2.rs
new file mode 100644
index 0000000..b931ba1
--- /dev/null
+++ b/2023/day-02/src/bin/part2.rs
@@ -0,0 +1,140 @@
+
+#[derive(Debug)]
+struct GameSet {
+ red: u32,
+ green: u32,
+ blue: u32
+}
+
+fn normalize(x: u32) -> u32 {
+ if x == 0 {
+ 1
+ } else {
+ x
+ }
+}
+
+impl GameSet {
+ fn power(&self) -> u32 {
+ normalize(self.red) * normalize(self.green) * normalize(self.blue)
+ }
+}
+
+struct Game {
+ id: u32,
+ sets: Vec<GameSet>
+}
+
+impl Game {
+ fn min_gameset(&self) -> GameSet {
+ let mut red = 0;
+ let mut green = 0;
+ let mut blue = 0;
+
+ for gameset in self.sets.iter() {
+ if gameset.red > 0 {
+ red = u32::max(red, gameset.red);
+ }
+ if gameset.green > 0 {
+ green = u32::max(green, gameset.green);
+ }
+ if gameset.blue > 0 {
+ blue = u32::max(blue, gameset.blue);
+ }
+ }
+
+ GameSet {
+ red, green, blue
+ }
+ }
+}
+
+fn main() {
+ let input = include_str!("./input.txt");
+ let output: u32 = input
+ .lines()
+ .map(|line| {
+ process_line(line)
+ })
+ .map(|game| {
+ game.min_gameset().power()
+ })
+ .sum();
+ println!("Result {output}")
+}
+
+fn gameset_from_pairs(pairs: Vec<(&str, u32)>) -> GameSet {
+ let mut red: u32 = 0;
+ let mut green: u32 = 0;
+ let mut blue: u32 = 0;
+
+ for pair in pairs {
+ match pair {
+ ("red", x) => { red = x },
+ ("green", x) => { green = x },
+ ("blue", x) => { blue = x },
+ what => panic!("Unknown pair {:?}", what)
+ }
+ }
+
+ GameSet { red, green, blue }
+}
+
+/// Progrss a line
+fn process_line(string: &str) -> Game {
+ let game_split: Vec<&str> = string.splitn(2, ": ").collect();
+ // Get game Id from string "Game 1"
+ let game_id = game_split[0].split(" ").last().expect("Expected id").parse::<u32>().expect("Cant parse game id");
+ let sets: Vec<_> = game_split[1]
+ .split("; ")
+ .map(|set_line| {
+ set_line.split(", ").collect()
+ })
+ .map(|gameset: Vec<&str>| {
+ let pairs: Vec<(&str, u32)> = gameset.into_iter().map(|s| {
+ let split: Vec<&str> = s.split_whitespace().collect();
+ (split[1], split[0].parse::<u32>().expect("Cant parse"))
+ }).collect();
+ gameset_from_pairs(pairs)
+ })
+ .collect();
+ Game {
+ id: game_id,
+ sets
+ }
+}
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_process_simple() {
+ let game = process_line("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green");
+ assert_eq!(game.id, 1);
+ }
+
+ #[test]
+ fn test_input() {
+ let input = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
+Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
+Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
+Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
+Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
+ let output: u32 = input
+ .lines()
+ .map(|line| {
+ process_line(line)
+ })
+ .map(|game| {
+ game.min_gameset()
+ })
+ .inspect(|power| { dbg!(power); })
+ .map(|gs| { gs.power() })
+
+ .sum();
+
+ assert_eq!(output, 2286);
+ }
+}