| 1 | package Games::Sudoku::DLX; | ||||||||||||||||||||||||||||
| 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 | 1 | our $VERSION = 0.02; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Algorithm::DLX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw( solve_sudoku ); # symbols to export on request | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub sudoku_to_dlx { | ||||||||||||||||||||||||||
|
Subroutine
sudoku_to_dlx: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 4 | my %params = @_; | |||||||||||||||||||||||||||
| 15 | 4 | my $puzzle = $params{puzzle}; | |||||||||||||||||||||||||||
| 16 | 4 | my $regions = $params{regions}; | |||||||||||||||||||||||||||
| 17 | 4 | my $dlx = Algorithm::DLX->new(); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | my $order = @$puzzle; | |||||||||||||||||||||||||||
| 20 | 4 | my $number_of_regions = @$regions; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | my @cols; | |||||||||||||||||||||||||||
| 23 | # Each cell can only have one symbol | ||||||||||||||||||||||||||||
| 24 | 4 | for my $r (0..$order - 1) { | |||||||||||||||||||||||||||
| 25 | 30 | for my $c (0..$order - 1) { | |||||||||||||||||||||||||||
| 26 | 234 | push @cols, $dlx->add_column("cell_$r$c"); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Each symbol can only appear once in each row, column, and region | ||||||||||||||||||||||||||||
| 31 | 4 | for my $r (1..$order) { | |||||||||||||||||||||||||||
| 32 | 30 | for my $c (0..$order - 1) { | |||||||||||||||||||||||||||
| 33 | 234 | for my $region (@$regions) { | |||||||||||||||||||||||||||
| 34 | 738 | my ($a, $b) = @$region; | |||||||||||||||||||||||||||
| 35 | 738 | my $block = (int($r/$a) * $a) + int($c/$b); | |||||||||||||||||||||||||||
| 36 | 738 | push @cols, $dlx->add_column("r#$r c#$c R#$a,$b B#$block"); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | for my $r (0..$order - 1) { | |||||||||||||||||||||||||||
| 42 | 30 | for my $c (0..$order - 1) { | |||||||||||||||||||||||||||
| 43 | 234 | ▶ | if ($puzzle->[$r][$c]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 122 | my $n = $puzzle->[$r][$c]; | |||||||||||||||||||||||||||
| 45 | 122 | my @columns; | |||||||||||||||||||||||||||
| 46 | 122 | for my $region (0..@$regions - 1) { | |||||||||||||||||||||||||||
| 47 | 375 | my ($a, $b) = @{$regions->[$region]}; | |||||||||||||||||||||||||||
| 48 | 375 | my $block = (int($r/$a) * $a) + int($c/$b); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 375 | push @columns, $cols[(($region+1) * $order**2) + ($block*$order)+($n-1)]; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Add the cell column | ||||||||||||||||||||||||||||
| 54 | 122 | push @columns, $cols[$r*$order+$c]; | |||||||||||||||||||||||||||
| 55 | 122 | $dlx->add_row("$r $c $n", @columns); | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 112 | for my $n (1..$order) { | |||||||||||||||||||||||||||
| 58 | 846 | my @columns; | |||||||||||||||||||||||||||
| 59 | 846 | for my $region (0..scalar @$regions - 1) { | |||||||||||||||||||||||||||
| 60 | 2700 | my ($a, $b) = @{$regions->[$region]}; | |||||||||||||||||||||||||||
| 61 | 2700 | my $block = (int($r/$a) * $a) + int($c/$b); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2700 | push @columns, $cols[(($region+1) * $order**2) + ($block*$order)+($n-1)]; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Add the cell column | ||||||||||||||||||||||||||||
| 67 | 846 | push @columns, $cols[$r*$order+$c]; | |||||||||||||||||||||||||||
| 68 | 846 | $dlx->add_row("$r $c $n", @columns); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | return $dlx; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # When we me this a module, this is what we will export | ||||||||||||||||||||||||||||
| 78 | 6 | ▶ | sub solve_sudoku { | ||||||||||||||||||||||||||
|
Subroutine
solve_sudoku: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 6 | my %params = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 6 | my $puzzle = $params{puzzle}; | |||||||||||||||||||||||||||
| 82 | 6 | my $regions = $params{regions}; | |||||||||||||||||||||||||||
| 83 | 6 | ▶ | my $number_of_solutions = $params{number_of_solutions} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{'number_of_solutions'} || 0
MC/DC0%0/1
$params{'number_of_solutions'} || 0
$params{'number_of_solutions'}
Truth table50%1/2
A
A= $params{'number_of_solutions'}
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # validate the regions | ||||||||||||||||||||||||||||
| 86 | 6 | my $puzzle_size = scalar @$puzzle; | |||||||||||||||||||||||||||
| 87 | 6 | for my $region (@$regions) { | |||||||||||||||||||||||||||
| 88 | 17 | my ($a, $b) = @$region; | |||||||||||||||||||||||||||
| 89 | 17 | ▶ | die "Invalid region size: $a x $b for puzzle of size $puzzle_size\n" if $a * $b != @$puzzle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # validate the puzzle size | ||||||||||||||||||||||||||||
| 93 | 5 | for my $row (@$puzzle) { | |||||||||||||||||||||||||||
| 94 | 36 | ▶ | die "Invalid row size: @$row should have size $puzzle_size\n" if @$row != $puzzle_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # validate the cell values | ||||||||||||||||||||||||||||
| 97 | 36 | for my $cell (@$row) { | |||||||||||||||||||||||||||
| 98 | 288 | ▶ | die "Invalid cell value: $cell should be between 0 and $puzzle_size\n" if $cell < 0 || $cell > $puzzle_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cell < 0 or $cell > $puzzle_size
MC/DC50%1/2
$cell < 0 or $cell > $puzzle_size
$cell < 0
$cell > $puzzle_size
Truth table66%2/3
A or B
A= $cell < 0B= $cell > $puzzle_size
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | my $dlx = sudoku_to_dlx( | |||||||||||||||||||||||||||
| 103 | regions => $regions, | ||||||||||||||||||||||||||||
| 104 | puzzle => $puzzle, | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | 4 | my $solutions = $dlx->solve( | |||||||||||||||||||||||||||
| 107 | number_of_solutions => $number_of_solutions | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | return $solutions; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | 1 | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||