mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Improve "winning" logic
You don't need to check every possible win condition, just the one you touched.
This commit is contained in:
@@ -13,23 +13,35 @@ use nom::IResult;
|
|||||||
struct BingoCard([(bool, u8); 25]);
|
struct BingoCard([(bool, u8); 25]);
|
||||||
|
|
||||||
impl BingoCard {
|
impl BingoCard {
|
||||||
pub fn cross(&mut self, num: u8) -> bool {
|
pub fn cross(&mut self, num: u8) -> Option<usize> {
|
||||||
if let Some(card) = self.0.iter_mut().find(|&&mut (_, x)| x == num) {
|
self.0
|
||||||
card.0 = true;
|
.iter_mut()
|
||||||
true
|
.enumerate()
|
||||||
} else {
|
.find_map(|(pos, (ticked, x))| {
|
||||||
false
|
if *x == num {
|
||||||
}
|
*ticked = true;
|
||||||
|
Some(pos)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_won(&self) -> bool {
|
pub fn has_won(&self, crossed: usize) -> bool {
|
||||||
// Check horizontal lines
|
// Check horizontal lines
|
||||||
if self.0.chunks_exact(5).any(|s| s.iter().all(|&b| b.0)) {
|
if self
|
||||||
|
.0
|
||||||
|
.chunks_exact(5)
|
||||||
|
.nth(crossed / 5)
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.all(|&b| b.0)
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check vertical lines
|
// Check vertical lines
|
||||||
(0..5).any(|x| self.0.iter().skip(x).step_by(5).all(|b| b.0))
|
self.0.iter().skip(crossed % 5).step_by(5).all(|b| b.0)
|
||||||
|
|
||||||
// Diagonals do not count
|
// Diagonals do not count
|
||||||
}
|
}
|
||||||
@@ -81,7 +93,7 @@ pub fn part1(input: &mut dyn Read) -> String {
|
|||||||
|
|
||||||
for number in numbers {
|
for number in numbers {
|
||||||
for card in &mut bingo_cards {
|
for card in &mut bingo_cards {
|
||||||
if card.cross(number) && card.has_won() {
|
if matches!(card.cross(number), Some(pos) if card.has_won(pos)) {
|
||||||
return (number as u32 * card.remaining()).to_string();
|
return (number as u32 * card.remaining()).to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,7 +110,11 @@ pub fn part2(input: &mut dyn Read) -> String {
|
|||||||
|
|
||||||
for num in numbers {
|
for num in numbers {
|
||||||
for (won, card) in bingo_won.iter_mut().zip(bingo_cards.iter_mut()) {
|
for (won, card) in bingo_won.iter_mut().zip(bingo_cards.iter_mut()) {
|
||||||
if !*won && card.cross(num) && card.has_won() {
|
if *won {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches!(card.cross(num), Some(pos) if card.has_won(pos)) {
|
||||||
*won = true;
|
*won = true;
|
||||||
num_won += 1;
|
num_won += 1;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user