diff options
| author | Abigail <abigail@abigail.be> | 2020-11-11 17:01:45 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2020-11-11 17:01:45 +0100 |
| commit | 5363e88f3d502d0a3e647b151f8a6259156f2665 (patch) | |
| tree | 9e1363f3f9460b037d3c5ef8d53a2386514e7268 | |
| parent | 6b9654fd02247b134e732abbf57615ebe34e7428 (diff) | |
| download | perlweeklychallenge-club-5363e88f3d502d0a3e647b151f8a6259156f2665.tar.gz perlweeklychallenge-club-5363e88f3d502d0a3e647b151f8a6259156f2665.tar.bz2 perlweeklychallenge-club-5363e88f3d502d0a3e647b151f8a6259156f2665.zip | |
Make an educated guess what the missing clue is.
It's possible to have NxN Sudoku's, with only different N-1 clues.
If that is the case, we make an educated guess, based on whether
the clues are numbers only, letters only, a mixture, and whether
there are any "holes".
| -rw-r--r-- | challenge-086/abigail/perl/ch-2.pl | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/challenge-086/abigail/perl/ch-2.pl b/challenge-086/abigail/perl/ch-2.pl index 1faefc7b59..2fb2e79a50 100644 --- a/challenge-086/abigail/perl/ch-2.pl +++ b/challenge-086/abigail/perl/ch-2.pl @@ -71,22 +71,58 @@ die "Not enough different clues" if $clue_count < $SIZE - 1; # # We may have to come with a missing clue name. This could be -# a number, or a letter. If there are letters, we need the next -# letter after the last one. Else, we need the first unused number. +# a number, or a letter. +# - If there are numbers, but 1 is missing, use 1. +# - Else, if there are numbers, with a hole, fill in that hole. +# - Else, if there are numbers 1 .. $SIZE - 1, but not +# $SIZE or higher, use $SIZE. +# - Else, if there are letters, but 'A' is missing, use 'A' +# - Else, if there are letters, with a hole, fill in that hole. +# - Else, if there are letters, use the next letter after the last, +# or '*' if the last is 'Z' +# - Else, use one more than the highest number. # if ($clue_count < $SIZE) { my $clue; - if (grep {/[A-Z]/} keys %clues) { - my ($max) = sort {$b cmp $a} grep {/[A-Z]/} keys %clues; - # - # Special case 'Z' - # - $clue = $max eq 'Z' ? "*" : chr (1 + ord $max); + my @numbers = sort {$a <=> $b} grep {/[0-9]/} keys %clues; + my @letters = sort {$a cmp $b} grep {/[A-Z]/} keys %clues; + + if (@numbers) { + if ($numbers [0] != 1) { + $clue = 1; + } + else { + for (my $i = 0; $i < @numbers - 2; $i ++) { + if ($numbers [$i] + 1 != $numbers [$i + 1]) { + $clue = $numbers [$i] + 1; + last; + } + } + if (!$clue && @numbers == $SIZE - 1 && + $numbers [-1] == $SIZE - 1) { + $clue = $SIZE; + } + } } - else { - my ($max) = sort {$b <=> $a} keys %clues; - $clue = $max + 1; + if (!$clue && @letters) { + if ($letters [0] ne 'A') { + $clue = 'A'; + } + else { + for (my $i = 0; $i < @letters - 2; $i ++) { + if (ord ($letters [$i]) + 1 != ord ($letters [$i + 1])) { + $clue = chr (ord ($letters [$i]) + 1); + last; + } + } + if (!$clue) { + $clue = $letters [-1] eq 'Z' ? "*" + : chr (ord ($letters [-1]) + 1); + } + } } + $clue //= $numbers [-1] + 1; + $clues {$clue} = ++ $clue_count; } |
