aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-11-11 14:15:03 +0100
committerAbigail <abigail@abigail.be>2020-11-11 14:15:03 +0100
commitb8cec8a91a5e5f6e637ed9cdff26c3b37bc5cc7e (patch)
treea8e463d8db8fc60670f3e9ac600fa7a6e91ca170
parenta0ade288f63215cde66aeab2ac43c544e9246317 (diff)
downloadperlweeklychallenge-club-b8cec8a91a5e5f6e637ed9cdff26c3b37bc5cc7e.tar.gz
perlweeklychallenge-club-b8cec8a91a5e5f6e637ed9cdff26c3b37bc5cc7e.tar.bz2
perlweeklychallenge-club-b8cec8a91a5e5f6e637ed9cdff26c3b37bc5cc7e.zip
Handle NxN sudokus where N is not a square.
Assumes a brick like pattern of blocks, where the blocks are wider than they are high.
-rw-r--r--challenge-086/abigail/perl/ch-2.pl19
1 files changed, 14 insertions, 5 deletions
diff --git a/challenge-086/abigail/perl/ch-2.pl b/challenge-086/abigail/perl/ch-2.pl
index b2fc3ef971..27f5a28b38 100644
--- a/challenge-086/abigail/perl/ch-2.pl
+++ b/challenge-086/abigail/perl/ch-2.pl
@@ -23,13 +23,22 @@ my @sudoku = map {[map {/_/ ? 0
my $SIZE = @sudoku;
my @INDICES = (0 .. $SIZE - 1);
my @ELEMENTS = (1 .. $SIZE);
-my $sqrtSIZE = sqrt $SIZE;
+
+#
+# Calculate the block size. For regular shaped sudoku's, this
+# is sqrt ($SIZE) x sqrt ($SIZE). For other sized shaped sudoku's,
+# we find the the nearest values; block will then we wider than
+# they are high.
+#
+my ($block_x, $block_y) = do {
+ my $s = int sqrt $SIZE;
+ $s -- while $SIZE % $s;
+ ($s, $SIZE / $s);
+};
#
# Sanity checks
#
-die "Sudoku width not a square\n"
- unless int (sqrt $SIZE) ** 2 == $SIZE;
die "All rows should be the same length as the columns"
if grep {@$_ != $SIZE} @sudoku;
foreach my $row (@sudoku) {
@@ -54,8 +63,8 @@ sub sees ($x, $y) {
$i == $x || # Same column
$j == $y || # Same row
# Same box
- int ($i / $sqrtSIZE) == int ($x / $sqrtSIZE) &&
- int ($j / $sqrtSIZE) == int ($y / $sqrtSIZE);
+ int ($i / $block_x) == int ($x / $block_x) &&
+ int ($j / $block_y) == int ($y / $block_y);
}
}
$out;