| 1 | package Games::Sudoku::PatternSolver::CPLogic; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | require 5.06.0; | ||
| 7 | |||
| 8 | 0 | ▶ | use Bit::Vector; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Algorithm::Combinatorics qw(combinations); | ||
| 10 | |||
| 11 | # array with 81 hashes representing the fields; field->{covers} returns Bit::Vector with constraint coverage field indexes set (row, column and block in one) | ||
| 12 | my $Fields = Games::Sudoku::PatternSolver::Patterns::init_fields(); | ||
| 13 | # an array of 27 arrays, holding the 9 indexes sharing any row, column or box | ||
| 14 | my $Groups = Games::Sudoku::PatternSolver::Patterns::build_groups(); | ||
| 15 | my $Group_Vectors = [map {my $v = Bit::Vector->new(81); $v->Index_List_Store(@$_); $v} @$Groups]; | ||
| 16 | |||
| 17 | # an array of 54 (9x6) arrays with 3 Bit::Vector(s) each: [intersection of box with row or column], [remainder of box], [remainder of row or column] | ||
| 18 | my $Intersections = Games::Sudoku::PatternSolver::Patterns::get_intersections(); | ||
| 19 | # to be initialized with each symbol's candidates map as a Bit::Vector | ||
| 20 | |||
| 21 | # ugly: a 2nd data structure with the candidates, that has to be created and maintained | ||
| 22 | # only for use by advanced logic (locked_sets, locked_candidates) | ||
| 23 | my %Candidate_Vectors = (); | ||
| 24 | my $steps_taken; | ||
| 25 | my $RECORD_STEPS = 0; | ||
| 26 | |||
| 27 | sub apply_logic { | ||
| 28 | my ($puzzle) = @_; | ||
| 29 | |||
| 30 | my $puzzle_string = $puzzle->{strPuzzle}; | ||
| 31 | $puzzle_string =~ s/[^1-9]/0/g; | ||
| 32 | |||
| 33 | $_->{candidates} = {}, $_->{value} = undef for @$Fields; # clear Field objects | ||
| 34 | |||
| 35 | my $index_arrays_by_symbol = {}; # symbol -> array with all fields (offset) where the symbol is the given value | ||
| 36 | my $coverage_vectors_by_symbol = {}; # do we really need this? currently only for populating and deleting the candidates | ||
| 37 | |||
| 38 | my $missing_count = 81; | ||
| 39 | my $field_index = -1; | ||
| 40 | foreach my $symbol (split //, $puzzle_string) { | ||
| 41 | $field_index++; | ||
| 42 | $symbol =~ /[1-9]/ or next; | ||
| 43 | $missing_count--; | ||
| 44 | push @{$index_arrays_by_symbol->{$symbol}}, $field_index; | ||
| 45 | $Fields->[$field_index]{value} = $symbol; | ||
| 46 | } | ||
| 47 | |||
| 48 | foreach my $symbol (keys %$index_arrays_by_symbol) { | ||
| 49 | my $covers = Bit::Vector->new(81); | ||
| 50 | $covers->Or($covers, $Fields->[$_]{covers}) for $puzzle->{symbolVectors}{$symbol}->Index_List_Read(); | ||
| 51 | $coverage_vectors_by_symbol->{$symbol} = $covers; | ||
| 52 | } | ||
| 53 | |||
| 54 | foreach my $symbol (1..9) { | ||
| 55 | # rem: $coverage_vectors_by_symbol used only here and in put_value(), would be nice to drop | ||
| 56 | my $area_vector = $coverage_vectors_by_symbol->{$symbol} ||= Bit::Vector->new(81); | ||
| 57 | my $candidate_vector = Bit::Vector->new(81); | ||
| 58 | |||
| 59 | foreach my $field (@$Fields) { | ||
| 60 | next if $field->{value}; | ||
| 61 | unless ($area_vector->bit_test($field->{offset})) { | ||
| 62 | $field->{candidates}{$symbol} = 1; | ||
| 63 | $candidate_vector->Bit_On($field->{offset}); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | $Candidate_Vectors{$symbol} = $candidate_vector; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $filled_total = 0; | ||
| 70 | my $dropped_total = 0; | ||
| 71 | my $dropping_candidates_did_help = 0; | ||
| 72 | $steps_taken = []; | ||
| 73 | |||
| 74 | while ($missing_count) { | ||
| 75 | my $last_filled = 0; | ||
| 76 | |||
| 77 | $last_filled += hidden_singles($puzzle); | ||
| 78 | $last_filled += naked_singles($puzzle); | ||
| 79 | |||
| 80 | unless ($last_filled) { | ||
| 81 | #### last; # skip further efforts to reduce the candidates (advanced methods) | ||
| 82 | |||
| 83 | my $last_dropped = 0; | ||
| 84 | |||
| 85 | $last_dropped += locked_sets($puzzle); | ||
| 86 | $last_dropped += locked_candidates($puzzle); | ||
| 87 | |||
| 88 | $last_dropped or last; | ||
| 89 | |||
| 90 | $dropped_total += $last_dropped; | ||
| 91 | |||
| 92 | } elsif ($dropped_total) { | ||
| 93 | $dropping_candidates_did_help = 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | $filled_total += $last_filled; | ||
| 97 | $missing_count -= $last_filled; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $puzzle_string_2; | ||
| 101 | if ($filled_total) { | ||
| 102 | $puzzle_string_2 = join '', map {$Fields->[$_]{value} || '.'} (0..80); | ||
| 103 | |||
| 104 | # are 8 symbols completely filled in and 1 symbol entirely missing? | ||
| 105 | if ($missing_count == 9 && $puzzle->{uniqueGivens} == 8) { | ||
| 106 | for (1..9) { | ||
| 107 | next if $puzzle_string_2 =~ /$_/; | ||
| 108 | |||
| 109 | my $added = $puzzle_string_2 =~ s/\./$_/g; | ||
| 110 | $filled_total += $added; | ||
| 111 | $missing_count -= $added; | ||
| 112 | # did not yet find a sudoku where that happend | ||
| 113 | warn "'$puzzle_string': Logic mode finished by adding '$_' as the last symbol.\n"; | ||
| 114 | |||
| 115 | last; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | $puzzle->{strPuzzleAfterLogic} = $puzzle_string_2; | ||
| 120 | } | ||
| 121 | |||
| 122 | $puzzle->{logicFilled} = $filled_total; | ||
| 123 | $puzzle->{candidatesDropped} = $dropped_total; | ||
| 124 | $puzzle->{candidateVectors} = \%Candidate_Vectors; | ||
| 125 | $puzzle->{droppingCandidatesDidHelp} = $dropping_candidates_did_help; | ||
| 126 | $puzzle->{logicSteps} = $steps_taken; | ||
| 127 | |||
| 128 | if ($missing_count) { | ||
| 129 | $puzzle->{logicSolved} = 0; | ||
| 130 | |||
| 131 | } elsif ($filled_total) { | ||
| 132 | |||
| 133 | $puzzle->{logicSolved} = 1; | ||
| 134 | push @{$puzzle->{solutions}}, $puzzle_string_2; | ||
| 135 | $puzzle->{solutionCount} = 1; | ||
| 136 | |||
| 137 | } else { | ||
| 138 | print "Puzzle was no puzzle!\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 139 | } | ||
| 140 | |||
| 141 | return $puzzle | ||
| 142 | } | ||
| 143 | |||
| 144 | sub hidden_singles { | ||
| 145 | my ($puzzle) = @_; | ||
| 146 | |||
| 147 | print "hidden_singles()\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 148 | |||
| 149 | my $symbol_counts = $puzzle->{symbolCounts}; | ||
| 150 | my $filled = 0; | ||
| 151 | # moving symbols with most givens upfront probably wouldn't yield | ||
| 152 | foreach my $symbol (1..9) { | ||
| 153 | next if (exists($symbol_counts->{$symbol}) && ($symbol_counts->{$symbol} == 9)); | ||
| 154 | |||
| 155 | GROUP: | ||
| 156 | foreach my $g (@$Groups) { | ||
| 157 | my $only_position = undef; | ||
| 158 | foreach my $field_index (@$g) { | ||
| 159 | next unless exists $Fields->[$field_index]{candidates}{$symbol}; | ||
| 160 | next GROUP if defined $only_position; | ||
| 161 | $only_position = $field_index; | ||
| 162 | } | ||
| 163 | |||
| 164 | if (defined $only_position) { | ||
| 165 | put_value($only_position, $symbol, $puzzle); | ||
| 166 | $filled++; | ||
| 167 | push @$steps_taken, 'HS_V_' . $symbol . '_' . Games::Sudoku::PatternSolver::Patterns::field_coordinates($only_position) | ||
| 168 | if $RECORD_STEPS; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | return $filled; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub naked_singles { | ||
| 177 | my ($puzzle) = @_; | ||
| 178 | |||
| 179 | print "naked_singles()\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 180 | |||
| 181 | my $filled = 0; | ||
| 182 | foreach my $field (@$Fields) { | ||
| 183 | $field->{value} and next; | ||
| 184 | my @candidates = keys %{$field->{candidates}}; | ||
| 185 | if (@candidates == 1) { | ||
| 186 | my $field_index = $field->{offset}; | ||
| 187 | my $symbol = shift @candidates; | ||
| 188 | put_value($field_index, $symbol, $puzzle); | ||
| 189 | $filled++; | ||
| 190 | push @$steps_taken, 'NS_V_' . $symbol . '_' . Games::Sudoku::PatternSolver::Patterns::field_coordinates($field_index) | ||
| 191 | if $RECORD_STEPS; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | return $filled; | ||
| 196 | } | ||
| 197 | |||
| 198 | # Because the naked and hidden sets of candidates occuring within a group always are mutually complementary | ||
| 199 | # we have to implement search for only one of the two types. | ||
| 200 | # Going for the hidden pairs, triples and quads seems like it can be done more easily with our candidates and groups vectors. | ||
| 201 | # example with a hidden quad set: 816573294392......4572.9..6941...5687854961236238...4.279.....1138....7.564....82 | ||
| 202 | sub locked_sets { | ||
| 203 | my ($puzzle) = @_; | ||
| 204 | |||
| 205 | print "locked_sets()\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 206 | |||
| 207 | my $symbol_counts = $puzzle->{symbolCounts}; | ||
| 208 | my $dropped = 0; | ||
| 209 | my $test_vector = Bit::Vector->new(81); | ||
| 210 | |||
| 211 | for (my $group_index = 0; $group_index < 27; $group_index++) { | ||
| 212 | my $group_array = $Groups->[$group_index]; | ||
| 213 | my $group_vector = $Group_Vectors->[$group_index]; | ||
| 214 | |||
| 215 | my %candidate_counts = (); | ||
| 216 | my %candidate_vectors = (); | ||
| 217 | foreach my $field_index (@$group_array) { | ||
| 218 | $Fields->[$field_index]{value} and next; | ||
| 219 | foreach my $candidate_symbol (keys %{$Fields->[$field_index]{candidates}}) { | ||
| 220 | $candidate_counts{$candidate_symbol}++; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | # stay with candidates which occupy 2-4 cells in the group | ||
| 225 | foreach my $candidate_symbol (keys %candidate_counts) { | ||
| 226 | my $candidate_count = $candidate_counts{$candidate_symbol}; | ||
| 227 | if ($candidate_count > 4 || $candidate_count == 1) { | ||
| 228 | delete $candidate_counts{$candidate_symbol}; | ||
| 229 | |||
| 230 | } else { | ||
| 231 | my $candidate_in_group_vector = Bit::Vector->new(81); | ||
| 232 | $candidate_in_group_vector->And($group_vector, $Candidate_Vectors{$candidate_symbol}); | ||
| 233 | $candidate_vectors{$candidate_symbol} = $candidate_in_group_vector; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | my @candidates_in_group = keys %candidate_counts; | ||
| 237 | next if @candidates_in_group < 4; | ||
| 238 | |||
| 239 | # iterate over all possible combinations of the 2-4 different candidates found above | ||
| 240 | foreach my $k_size (4, 3, 2) { | ||
| 241 | next if @candidates_in_group <= $k_size; | ||
| 242 | my $combination_iterator = combinations(\@candidates_in_group, $k_size); | ||
| 243 | while (my $candidates_combination = $combination_iterator->next) { | ||
| 244 | $test_vector->Empty(); | ||
| 245 | foreach my $candidate (@$candidates_combination) { | ||
| 246 | $test_vector->Or($test_vector, $candidate_vectors{$candidate}); | ||
| 247 | } | ||
| 248 | |||
| 249 | # do the k different candidates occupy exactly k cells? | ||
| 250 | my @combined_fields = $test_vector->Index_List_Read(); | ||
| 251 | if (@combined_fields == $k_size) { | ||
| 252 | print "group $group_index, hidden_set (@$candidates_combination) in cells: [@combined_fields]\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 253 | # got a hidden set | ||
| 254 | # now we must look for any other candidates in the same cells, which we can eliminate | ||
| 255 | my %hidden_candidates = (); | ||
| 256 | @hidden_candidates{@$candidates_combination} = undef; | ||
| 257 | |||
| 258 | foreach my $field_index (@combined_fields) { | ||
| 259 | foreach my $candidate_symbol (keys %{$Fields->[$field_index]{candidates}}) { | ||
| 260 | unless (exists $hidden_candidates{$candidate_symbol}) { | ||
| 261 | drop_candidate($field_index, $candidate_symbol); | ||
| 262 | $dropped++; | ||
| 263 | push @$steps_taken, 'LS_D_' . $candidate_symbol . '_' . Games::Sudoku::PatternSolver::Patterns::field_coordinates($field_index) | ||
| 264 | if $RECORD_STEPS; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | return $dropped; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub locked_candidates { | ||
| 277 | my ($puzzle) = @_; | ||
| 278 | |||
| 279 | print "locked_candidates()\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 280 | |||
| 281 | my $symbol_counts = $puzzle->{symbolCounts}; | ||
| 282 | my $dropped = 0; | ||
| 283 | my $in_box_vector = Bit::Vector->new(81); | ||
| 284 | my $in_line_vector = Bit::Vector->new(81); | ||
| 285 | |||
| 286 | foreach my $set (@$Intersections) { | ||
| 287 | my ($intersection_vector, $boxremainder_vector, $lineremainder_vector) = @$set; | ||
| 288 | |||
| 289 | my %candidates_in_intersection = (); | ||
| 290 | foreach my $field_index ($intersection_vector->Index_List_Read()) { | ||
| 291 | my $intersection_field = $Fields->[$field_index]; | ||
| 292 | $candidates_in_intersection{$_}++ for keys %{$intersection_field->{candidates}}; | ||
| 293 | } | ||
| 294 | |||
| 295 | foreach my $symbol (keys %candidates_in_intersection) { | ||
| 296 | my $candidate_vector = $Candidate_Vectors{$symbol}; | ||
| 297 | |||
| 298 | $in_box_vector->And($candidate_vector, $boxremainder_vector); | ||
| 299 | my @further_box_matches = $in_box_vector->Index_List_Read(); | ||
| 300 | |||
| 301 | $in_line_vector->And($candidate_vector, $lineremainder_vector); | ||
| 302 | my @further_line_matches = $in_line_vector->Index_List_Read(); | ||
| 303 | |||
| 304 | if (@further_box_matches && ! @further_line_matches) { | ||
| 305 | # case of 'Claiming' | ||
| 306 | foreach my $field_index (@further_box_matches) { | ||
| 307 | drop_candidate($field_index, $symbol); | ||
| 308 | $dropped++; | ||
| 309 | push @$steps_taken, 'LC_D_' . $symbol . '_' . Games::Sudoku::PatternSolver::Patterns::field_coordinates($field_index) | ||
| 310 | if $RECORD_STEPS; | ||
| 311 | } | ||
| 312 | |||
| 313 | } elsif (@further_line_matches && ! @further_box_matches) { | ||
| 314 | # case of 'Pointing' aka 'Box / Line reduction' | ||
| 315 | foreach my $field_index (@further_line_matches) { | ||
| 316 | drop_candidate($field_index, $symbol); | ||
| 317 | $dropped++; | ||
| 318 | push @$steps_taken, 'LC_D_' . $symbol . '_' . Games::Sudoku::PatternSolver::Patterns::field_coordinates($field_index) | ||
| 319 | if $RECORD_STEPS; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | return $dropped; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub drop_candidate { | ||
| 329 | my ($index, $value) = @_; | ||
| 330 | |||
| 331 | print "Dropping candidate '$value' from $index\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 332 | |||
| 333 | my $field = $Fields->[$index]; | ||
| 334 | delete $field->{candidates}{$value}; | ||
| 335 | $Candidate_Vectors{$value}->Bit_Off($index); | ||
| 336 | } | ||
| 337 | |||
| 338 | sub put_value { | ||
| 339 | my ($index, $value, $puzzle) = @_; | ||
| 340 | |||
| 341 | print "Putting '$value' in $index\n" if $Games::Sudoku::PatternSolver::VERBOSE; | ||
| 342 | |||
| 343 | my $field = $Fields->[$index]; | ||
| 344 | $field->{value} = $value; | ||
| 345 | $field->{candidates} = {}; | ||
| 346 | |||
| 347 | # drop candidates | ||
| 348 | my $area_vector = $field->{covers}; | ||
| 349 | # same value candidates from covered fields | ||
| 350 | delete $Fields->[$_]{candidates}{$value} for $area_vector->Index_List_Read(); | ||
| 351 | # and also in the symbol's map of remaining candidates | ||
| 352 | $Candidate_Vectors{$value}->Bit_Off($_) for $area_vector->Index_List_Read(); | ||
| 353 | # and in all candidate maps the one occupied place | ||
| 354 | $_->Bit_Off($index) for values %Candidate_Vectors; | ||
| 355 | |||
| 356 | $puzzle->{symbolVectors}{$value}->Bit_On($index); | ||
| 357 | $puzzle->{symbolCounts}{$value}++; | ||
| 358 | } | ||
| 359 | |||
| 360 | 1 | ||