From b8cec8a91a5e5f6e637ed9cdff26c3b37bc5cc7e Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 11 Nov 2020 14:15:03 +0100 Subject: 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. --- challenge-086/abigail/perl/ch-2.pl | 19 ++++++++++++++----- 1 file 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; -- cgit