Skip to main content

Sudoku in Perl5 #1 - a brute-force solution

Having written the Perl6 Sudoku solver, I decided to go back to the Perl5 version I wrote when I was experimenting with Moose, but I couldn't find where I put it. While waiting for my senile memory to function, I implemented a brute-force solution. The basic concept of the brute-force approach is that you begin with a grid, some cells of which have been defined by the puzzle designer. I begin with the first empty cell ( call it A) and set it to '1'. I check to see if that conflicts with any already specified cells in the same row, in the same column, in the same block. If there's a conflict, I try the next element in the list of possible values : 2, 3..9. If a value does not conflict with existing cells, then on to the next cell. If all values create a conflict, then an earlier cell must have the wrong value, so back up. By recursively calling a routine over and over, backing up is easy, just return. If we run out of cells to the right, advance to the next row; when we run out of rows, the puzzle is solved. At that point, return a true value, and all the recursion unrolls in victory.

I call this version BruteForceTest, because, when testing the row, column and block, you don't want to compare a cell against itself. Thee simple way to avoid that is to iterate over the whole list, and test whether we are comparing the cell against itself.


BruteForceTest.pm





package BruteForceTest;

use warnings;
use strict;

sub new {
my ($class) = shift;
my $self = bless {}, $class;

$self->_init(@_);
return $self;
}

sub _init {
my $self = shift;
my ($args) = @_;

die( __PACKAGE__
. "constructors requires arg 'input'.\n" )
unless exists $args->{input}
and defined $args->{input};
my (@digits) = ( $args->{input} =~ m{(\d)}xmsg );
die( __PACKAGE__
. q{constructor arg 'input' must be 81 }
. ' digits, 1..9, with 0 for empty cells. '
. 'Line breaks and spaces are allowed.'
. "\n"
) unless 81 == scalar @digits;
for my $row ( 0 .. 8 ) {
for my $col ( 0 .. 8 ) {
my $digit = shift @digits;
$self->{set}{$row}{$col}++
if $digit >= 1 and $digit <= 9;
$self->{grid}[$row][$col] = $digit;
}
}
}


I've never used a copy constructor in 12 years of Perl programming, so I don't bother with that ref $class or $class stuff, but I do move all the initialization into an _init method, so subclasses can override. In the last couple of years I've begun to shift the object off @_; that way, I can x the argument list in the debugger, without having a flood of junk from $self.

My constructor expects a single argument, a hash ref, with the key input and a single string as the value. Then I extract all the digits from the string into an array. I considered a couple of options, considered allowing multi-line input, considered accepting a dot , '.', or space for a zero, but then decided sticking to digits meant I could grep the digits out of a longer string. I also considered a routine to read from a file, but my priority is to compare the different versions from a single driver program, so I didn't really need that. Any file reader would go in main() rather than in the individual solver modules.

Having verified the right number of digits, I scan through the set, assigning them one by one into an array of arrays. If the value is a zero, it's still waiting to be solved. Digits one through nine are values set by the designer, pre-conditions i must not alter during the solving process, so I also set use the row and column indices to set a hash, indicating protected cells.

sub solve {
my $self = shift;
my ( $r, $c ) = @_;

$r ||= 0;
$c ||= 0;

return 1 if $r >= 9;
return $self->solve( successor( $r, $c ) )
if $self->{set}{$r}{$c}; # skip pre-spec sell
VALUE:
for my $value ( 1 .. 9 ) {
$self->{grid}[$r][$c] = $value;
next VALUE
unless $self->cell_value_ok( $r, $c );
return 1
if $self->solve( successor( $r, $c ) )
}
$self->{grid}[$r][$c] = 0;
return;
}

I'm going to call solve() recursively, to process each next cell. The first call, from main(), shouldn't be encumbured by arguments, but the default values are trivial to assign. At one time I would have tested for the end of the puzzle when incrementing the indices ... that's the way I was taught, but that seems so C, so Pascal, now. It's simple to check the row index at the top of solve(), and more in the style of functional languages. If the index is too large, we've reached the end of the puzzle, and can return the victory signal.

If the current cell is one of the pre-specified cells, all we need do is solve the rest of the puzzle, and return the success or failure of that effort. Otherwise, try the values one through nine, in the current cell. If cell_value_ok() detects a conflict, try the next value in the list, but if it's alright for now, see if the rest of the puzzle is solvable with the current conditions. If we get a failure back, try the next value. If the last value fails, we must have gone wrong earlier, so set the current cell back to zero, and back up a space. Returning true from solve() indicates success, false indicates failure.

sub cell_value_ok {
my $self = shift;
my ( $r, $c ) = @_;

return unless $self->test_row( $r, $c );
return unless $self->test_col( $r, $c );
return unless $self->test_block( $r, $c );
return 1;
}
sub test_row {
my $self = shift;
my ( $row, $col ) = @_;

my $val = $self->{grid}[$row][$col];

CELL:
for my $c ( 0 .. 8 ) {
next CELL # don't test against self.
if $c == $col;
return # collision
if $val == $self->{grid}[$row][$c];
}
return 1; # ok
}

cell_value_ok returns success if there is no conflict between the value in cell $r, $c and the other values in the row, in the column, and in the block. test_row() demonstrates the concept: For each cell in the row, skipping over column $c, return a failure if the values match. If no conflict are present, return success. test_col() looks the same, except for swapping row and column. I considered merging the two routines into one, with some indication of which way to process, but merging test_block as well would be difficult. A little redundancy only makes the program slightly larger, but this module is so tiny, it can't take more than a single block to read it in. So there's no advantage besides clean coding, to merging, and there may be some performance to be gained.

sub test_col {
my $self = shift;
my ( $row, $col ) = @_;

my $val = $self->{grid}[$row][$col];

CELL:
for my $r ( 0 .. 8 ) {
next CELL # don't test against self.
if $r == $row;
return # collision
if $val == $self->{grid}[$r][$col];
}
return 1; # ok
}

sub test_block {
my $self = shift;
my ( $row, $col ) = @_;

my $baserow = 3 * int( $row / 3 ); # 0, 3 or 6
my $basecol = 3 * int( $col / 3 );

my $val = $self->{grid}[$row][$col];

for my $r ( $baserow .. $baserow + 2 ) {
CELL:
for my $c ( $basecol .. $basecol + 2 ) {
next CELL # don't test against self.
if $r == $row and $c == $col;
return # collision
if $val == $self->{grid}[$r][$c];
}
}
return 1; # ok
}

test_block() is a bit more complicated. Each cell is in one of 9 sub-blocks, the first row and first column of the sub-block can only be zero, three or six. So divide the cell's row or column number by 3 and keep only the integer part, multiply by three again, and you have the first row/column value. Just consider that row and the next two, that column and the next two, and you've coverd the sub-block.

sub successor {
my ( $r, $c ) = @_;

if ( $c == 8 ) {
$r++;
$c = 0;
}
else {
$c++;
}
return ( $r, $c );
}

Figuring out the next cell is fairly simple. If we've reached column 8, it's time to advance to the first column of the next row, otherwise just go to the next column.

sub print_answer {
my $self = shift;

print "\n\n";
for my $r ( 0 .. 8 ) {
for my $c ( 0 .. 8 ) {
print $self->{grid}[$r][$c];
print q{ }
if $c % 3 == 2;
}
print "\n";
print "\n"
if $r % 3 == 2;
}
}
1;

Finally, a routine to display the grid as it is at the moment. Blank spaces and empty rows division the matrix into sub-blocks, cleaner than using lines. Admittedly, lines, and dots or zeroes or underscores, rather than empty spaces, for unsolved cells, would be helpful if debugging a partially complete solution, so you can determine the location of a value more easily. But for simple display purposes, less is more.

BruteForceTest.pl




use BruteForceTest;

use warnings;
use strict;

my $puzzle = BruteForceTest->new(
{
input => (
'010056207'
. '000700005'
. '000300498'
. '000200380'
. '006000700'
. '051007000'
. '684002000'
. '100003000'
. '305460020'
),
}
);
$ouzzle->print_answer;
$puzzle->solve;
$puzzle->print_answer;

The main-line is to demonstrate that the program works ...
Create a puzzle with a particular value. Print out the initial conditions, solve the puzzle, and print the result. The display looks like this, running it with time to estimate performance:

010 056 207
000 700 005
000 300 498

000 200 380
006 000 700
051 007 000

684 002 000
100 003 000
305 460 020



418 956 237
923 784 615
567 321 498

749 215 386
236 849 751
851 637 942

684 192 573
192 573 864
375 468 129

real 0m0.139s
user 0m0.041s
sys 0m0.005s

So overall it takes 1/7 seconds of clock time, of which 1/25 seconds is real and only 5/1000 seconds is spent running the CPU. This is my desktop, not the laptop which spent 42 seconds running the Perl6 version, so direct comparisons are pointless, but obviously there are some efficiencies still to be implemented in Perl6.

Comments

Dhuree said…
Nice article Tom. I tried the same approach in Java.

I have also put a link to your post in my blog.

http://blog.dhuree.com/2015/05/howto-solve-sudoku-programmatically.html

Popular posts from this blog

Creating Perl5 Objects with Moxie

Having in the previous article prepared data types for car suits and card ranks, I can now combine them to provide a playing card class, using Stevan Little's Moxie module (version 0.04, so definitely early days.) The goal is to provide an object-oriented paradigm to the Perl 5 programming language which is more sophisticated, more powerful and less verbose than manually bless()-ing hashes. To achieve that goal it needs to be faster and light-weight compared to Moose. Currently, Moxie.pm and and MOP.pm are add-on modules, but eventually, when they are more complete, when the wrinkles have been ironed out, and when they have gained acceptance and a community of users, they might be merged into the Perl core.

One significant feature of Moxie is that it reduces boilerplate code. You don't have to specify warnigns or strict. As well, the features or the perl you are using are enabled, among them say, state, signatures, and post_deref.
A Simple Moxie Class package Card { …

Perl5, Moxie and Enumurated Data Types

Moxie - a new object system for Perl5 Stevan Little created the Moose multiverse to upgrade the Perl 5 programming language's object-oriented system more in line with the wonderfull world of Perl 6. Unfortunately, it's grown into a bloated giant, which has inspired light-weight alternatives Moos, Moo, Mo, and others. Now he's trying to create a modern, efficient OO system that can become built into the language.

I've seen a few of his presentations at YAPC (Yet Another Perl Conference, now known as TPC, The Perl Conference), among them ‎p5 mop final final v5 this is the last one i promise tar gz<. So I was delighted to recently see an announcement of the module Moxie, and decided to try implementing a card game.

While the package provides some POD documentation about the main module, Moxie, it doesn't actually explain the enum package, Moxie::Enum. But delving into the tests directory reveals its secrets.
Creating an Enum package Ranks { use Moxie::Enum; …

Adventures in Autovivification

Having recently started a new job, I was exposed to old code with multi-step tests against autovivification in multi-level hashes. You get used to the code you have seen, but in a new environment it‘s irritating and jarring.
Moose does not generally have the problem, first because class structure is pre-declared, because values are accessed using accessor functions rather than directly, and because responsibility is delegated down to attributes, avoiding long chains. On the other hand, Moose has it's own overhead, so hand-rolled objects, and bare structures still have their use.
If you don‘t protect against autovivification, then mis-spelling a key, or referencing keys which haven‘t been instantiated in this instance, causes those keys to instantly come into existence.
#!/usr/bin/perl use warnings; use strict; use Data::Dump 'dump'; use 5.024; my $var = {key1 => {key2 => {key3 => 'a'}}}; say dump $var; if ( $var->{key1}{key2}{key3b}[13]{foob…