From 83d4a9b50748df7fc12ee042c04e530885f0e746 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 27 Jan 2025 14:40:36 -0500 Subject: [PATCH] improve prop range splitting: 2 --- src/repr.rs | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/repr.rs b/src/repr.rs index 326bd05..5325696 100644 --- a/src/repr.rs +++ b/src/repr.rs @@ -78,45 +78,34 @@ impl Board { return; }; - for range in split(0, BOARD_SIZE - 1, i) { + let mut chains: Vec> = split(0, BOARD_SIZE - 1, i) + .into_iter() + .map(|range| range.into_iter().map(|i| (i, j)).collect()) + .collect(); + chains.append( + &mut split(0, BOARD_SIZE - 1, j) + .into_iter() + .map(|range| range.into_iter().map(|j| (i, j)).collect()) + .collect::>>(), + ); + + for chain in chains { let mut chain_length: usize = 0; - for &new_i in &range { - let Some(piece) = self.get(new_i, j) else { + + for &(new_i, new_j) in &chain { + let Some(piece) = self.get(new_i, new_j) else { break; }; if piece == &starting_color { if chain_length > 0 { // fill all opposite colors with this color - let Some(i_o_s) = range.get(..chain_length) else { + let Some(i_o_s) = chain.get(..chain_length) else { break; }; // fill all opposite colors with this color - for i_o in i_o_s { - self.place_and_prop_unchecked(*i_o, j, starting_color); - } - } - break; - } - chain_length += 1; - } - } - - for range in split(0, BOARD_SIZE - 1, j) { - let mut chain_length: usize = 0; - for &new_j in &range { - let Some(piece) = self.get(i, new_j) else { - break; - }; - if piece == &starting_color { - if chain_length > 0 { - let Some(j_o_s) = range.get(..chain_length) else { - break; - }; - - // fill all opposite colors with this color - for j_o in j_o_s { - self.place_and_prop_unchecked(i, *j_o, starting_color); + for &(i_o, j_o) in i_o_s { + self.place_and_prop_unchecked(i_o, j_o, starting_color); } } break;