use std::str::FromStr;
use round::Round;
+use std::cmp;
#[derive(Debug, PartialEq, Eq)]
pub struct Game {
#[derive(Debug, PartialEq, Eq)]
pub struct ParseGameError;
+impl Game {
+ pub fn power(&self) -> u32 {
+ let mut res = Round {red: 0, green: 0, blue: 0 };
+ for round in self.rounds.iter() {
+ res.red = cmp::max(res.red, round.red);
+ res.green = cmp::max(res.green, round.green);
+ res.blue = cmp::max(res.blue, round.blue);
+ }
+ return res.red * res.green * res.blue;
+ }
+}
+
impl FromStr for Game {
type Err = ParseGameError;
]});
assert_eq!(Game::from_str("Game 1: 20 red, 1 green, 2 blue; 2 green\n"), expected);
}
+
+ #[test]
+ fn test_game_power() {
+ let game = Game::from_str("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green").unwrap();
+ assert_eq!(game.power(), 48);
+ }
}
.collect::<Vec<_>>();
let limits = Round { red: 12, green: 13, blue: 14 };
- let sum_ids: u32= games.into_iter()
+ let sum_ids: u32 = games.iter()
.filter(|g| valid_game(g, &limits))
// .inspect(|g| print_game(g))
.map(|g| g.id)
.sum();
println!("part 1: {}", sum_ids);
+
+ let sum_power: u32 = games.into_iter()
+ .map(|g| g.power())
+ .sum();
+ println!("part 2: {}", sum_power);
}
Ok(())
}