day 2 part 2
This commit is contained in:
parent
06b45621a0
commit
bbd3e08709
1 changed files with 55 additions and 25 deletions
|
@ -1,9 +1,10 @@
|
|||
fn main() {
|
||||
let input = std::fs::read_to_string("input.txt").unwrap();
|
||||
|
||||
let result = valid_game_id_sum(&input);
|
||||
let game_id_sum = valid_game_id_sum(&input);
|
||||
let pow = game_pow_sum(&input);
|
||||
|
||||
println!("Result: {}", result);
|
||||
println!("Game ID sum: {}, Total game pow: {}", game_id_sum, pow);
|
||||
}
|
||||
|
||||
const MAX_RED: u32 = 12;
|
||||
|
@ -11,46 +12,70 @@ const MAX_GREEN: u32 = 13;
|
|||
const MAX_BLUE: u32 = 14;
|
||||
|
||||
fn valid_game_id_sum(game_list: &str) -> u32 {
|
||||
let res: u32 = game_list.lines().filter_map(|game| {
|
||||
let (left, right) = game.split_once(": ").unwrap();
|
||||
if game_validity_check(right.trim()) {
|
||||
let game_id: u32 = left.trim_start_matches("Game ").parse().unwrap();
|
||||
Some(game_id)
|
||||
}
|
||||
else {
|
||||
None
|
||||
}
|
||||
}).sum();
|
||||
let res: u32 = game_list
|
||||
.lines()
|
||||
.filter_map(|game| {
|
||||
let (left, right) = game.split_once(": ").unwrap();
|
||||
if let (true, _) = game_check(right.trim()) {
|
||||
let game_id: u32 = left.trim_start_matches("Game ").parse().unwrap();
|
||||
Some(game_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn game_validity_check(game: &str) -> bool {
|
||||
fn game_pow_sum(game_list: &str) -> u32 {
|
||||
game_list
|
||||
.lines()
|
||||
.map(|game| {
|
||||
let (_, right) = game.split_once(": ").unwrap();
|
||||
let (_, pow) = game_check(right.trim());
|
||||
pow
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn game_check(game: &str) -> (bool, u32) {
|
||||
let mut min_red = 0;
|
||||
let mut min_green = 0;
|
||||
let mut min_blue = 0;
|
||||
let mut valid = true;
|
||||
for set in game.split("; ") {
|
||||
let colors = set.split(", ");
|
||||
for color in colors {
|
||||
let (count, col) = color.split_once(' ').unwrap();
|
||||
let count: u32 = count.parse().unwrap();
|
||||
let invalid = match col {
|
||||
"red" => count > MAX_RED,
|
||||
"green" => count > MAX_GREEN,
|
||||
"blue" => count > MAX_BLUE,
|
||||
match col {
|
||||
"red" => {
|
||||
min_red = min_red.max(count);
|
||||
valid = valid && count <= MAX_RED;
|
||||
}
|
||||
"green" => {
|
||||
min_green = min_green.max(count);
|
||||
valid = valid && count <= MAX_GREEN;
|
||||
}
|
||||
"blue" => {
|
||||
min_blue = min_blue.max(count);
|
||||
valid = valid && count <= MAX_BLUE;
|
||||
}
|
||||
_ => panic!("invalid color"),
|
||||
};
|
||||
if invalid {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
let pow = min_red * min_green * min_blue;
|
||||
(valid, pow)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::valid_game_id_sum;
|
||||
use crate::*;
|
||||
|
||||
const PART_1_EXAMPLE: &str = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
const EXAMPLE: &str = "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
|
||||
|
@ -58,6 +83,11 @@ Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
|||
|
||||
#[test]
|
||||
fn part_1_example() {
|
||||
assert_eq!(8, valid_game_id_sum(PART_1_EXAMPLE));
|
||||
assert_eq!(8, valid_game_id_sum(EXAMPLE));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_2_example() {
|
||||
assert_eq!(2286, game_pow_sum(EXAMPLE));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue