solution: day 2 clean

This commit is contained in:
Matej Janezic 2023-12-02 14:23:33 +01:00
parent 00000060b5
commit 000000701f
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 12 additions and 28 deletions

View File

@ -26,35 +26,19 @@ impl FromStr for Game {
pub fn part_one(input: &str) -> Option<u32> { pub fn part_one(input: &str) -> Option<u32> {
let games: Vec<Game> = to_vec(input, '\n'); let games: Vec<Game> = to_vec(input, '\n');
let rules = [(12, "red"), (13, "green"), (14, "blue")];
let mut id_sum = 0; Some(
games
'games: for g in games { .iter()
for (n, c) in g.balls { .filter(|g| {
match c.as_str() { !g.balls
"blue" => { .iter()
if n > 14 { .any(|(n, c)| rules.iter().any(|(rn, rc)| c == rc && n > rn))
continue 'games; })
} .map(|g| g.id)
} .sum(),
"red" => { )
if n > 12 {
continue 'games;
}
}
"green" => {
if n > 13 {
continue 'games;
}
}
_ => continue 'games,
}
}
id_sum += g.id;
}
Some(id_sum)
} }
pub fn part_two(input: &str) -> Option<u32> { pub fn part_two(input: &str) -> Option<u32> {