Velocity Reviews > Brute force sudoku cracker

Brute force sudoku cracker

Bas
Guest
Posts: n/a

 09-16-2005
Hi group,

I came across some of these online sudoku games and thought after
playing a game or two that I'd better waste my time writing a solver
than play the game itself any longer. I managed to write a pretty dumb
brute force solver that can at least solve the easy cases pretty fast.

It basically works by listing all 9 possible numbers for all 81 fields
and keeps on striking out possibilities until it is done.

-any ideas how to easily incorporate advanced solving strategies?
solve(problem1) and solve(problem2) give solutions, but solve(problem3)
gets stuck...

-any improvements possible for the current code? I didn't play a lot
with python yet, so I probably missed some typical python tricks, like
converting for-loops to list expressions.

TIA,
Bas

***********

from itertools import ifilterfalse

problem1 = [' 63 7 ',
' 69 8',
'9 7 2',
' 2 1 8 ',
' 5 8 6 9 ',
' 9 7 2 ',
'6 1 3',
'7 45 ',
' 9 14 ']

problem2 = [' 3 9 7',
' 1 8 ',
' 1 9 ',
' 49 5 6',
' 2 1 ',
'5 6 74 ',
' 5 1 ',
' 4 2 ',
'7 5 3 ']

problem3 = [' 3 5 81 ',
' 76 9 ',
'4 ',
' 439 5 6',
' 1 7 ',
'6 8 193 ',
' 9',
' 9 86 ',
' 61 2 8 ']

#define horizontal lines, vertical lines and 3x3 blocks
groups = [range(9*i,9*i+9) for i in range(9)] + \
[range(i,81,9) for i in range(9)] + \
[range(0+27*i+3*j,3+27*i+3*j) + range(9+27*i+3*j,12+27*i+3*j)
+ \
range(18+27*i+3*j,21+27*i+3*j) for i in range(3) for j in
range(3)]

def display(fields):
for i in range(9):
line = ''
for j in range(9):
if len(fields[9*i+j]) == 1:
line += ' ' + str(fields[9*i+j][0])
else:
line += ' '
print line

def all(seq, pred=bool):
"Returns True if pred(x) is True for every element in the iterable"
for elem in ifilterfalse(pred, seq):
return False
return True

def product(seq):
prod = 1
for item in seq:
prod *= item
return prod

def solve(problem):
fields = [range(1,10) for i in range(81)] #fill with all
posibilities for all fields
for i,line in enumerate(problem):
for j,letter in enumerate(line):
if letter != ' ':
fields[9*i+j]=[int(letter)] #seed with numbers from
problem
oldpos = 0
while True:
pos = product(len(field) for field in fields)
if pos == oldpos: #no new possibilities eliminated, so stop
break
display(fields)
print pos,'possibilities'
oldpos = pos
for group in groups:
for index in group:
field = fields[index]
if len(field) == 1: #if only number possible for field
remove it from other fields in group
for ind in group:
if ind != index:
try:
fields[ind].remove(field[0])
except ValueError:
pass
else: #check if field contains number that does not
exist in rest of group
for f in field:
if all(f not in fields[ind] \
for ind in group if ind!=index):
fields[index] = [f]
break

my.correo.basura@gmail.com
Guest
Posts: n/a

 09-16-2005

Bas ha escrito:

> Hi group,
>
> I came across some of these online sudoku games and thought after
> playing a game or two that I'd better waste my time writing a solver
> than play the game itself any longer. I managed to write a pretty dumb
> brute force solver that can at least solve the easy cases pretty fast.
>
> It basically works by listing all 9 possible numbers for all 81 fields
> and keeps on striking out possibilities until it is done.
> [snip]

This problem is desperately begging for backtracking. The cost is still
exponential but it works nicely with small problems. Fortunately, a 9x9
grid is small enough. I programmed this about two months ago, it's not
really pretty but it works perfectly. Beware, some variable names are
in spansih...
[let's hope the tabs are kept...]
Regards,
Al

from sets import Set

class sudoku:
self.numeros=Set(range(1, 10))
self.m=[['X']*9 for i in range(9)]
for fila in range(9):
for columna in range(9):
if cadena[fila*9 + columna].isdigit():
self.posiciones=[(i,j) for i in range(9) for j in range(9) if
self.m[i][j]=='X']
def __repr__(self):
res=""
for fila in range(9):
if fila%3==0: res+= "-------------------------\n"

for columna in range(9):
if columna%3==0: res+= "| "
res+="%s "%str(self.m[fila][columna])
res+= "|\n"

res+= "-------------------------\n"
return res

def fila(self,fila, columna):
return self.numeros-Set(elem for elem in self.m[fila] if
elem!='X')
def columna(self,fila, columna):
return self.numeros-Set(self.m[i][columna] for i in range(9)
if self.m[i][columna]!='X')

s=Set()
f_ini=3*(fila/3)
c_ini=3*(columna/3)
for f in range(f_ini, f_ini+3):
for c in range(c_ini, c_ini+3):
if self.m[f][c]!='X' and self.m[f][c] not in s:

return self.numeros-s

def resuelve(self):
print "Resolviendo"
def actua(i):
if i==len(self.posiciones):
print self
return
f, c=self.posiciones[i]
posibles=self.fila(f, c).intersection(self.columna(f,
for num in posibles:
self.m[f][c]=num
actua(i+1)
self.m[f][c]='X'
actua(0)

def main():
problem3=" 3 5 81 76 9 4 439 5 6 1 7 6 8 193
9 9 86 61 2 8 "
t=sudoku(problem3)
print t
t.resuelve()

if __name__=="__main__":
main()

Sybren Stuvel
Guest
Posts: n/a

 09-17-2005
Bas enlightened us with:
> I came across some of these online sudoku games and thought after
> playing a game or two that I'd better waste my time writing a solver
> than play the game itself any longer. I managed to write a pretty
> dumb brute force solver that can at least solve the easy cases
> pretty fast.

I've got a solver too - I'm joining the Linux Format programming
contest. My program can solve and create Sudoku puzzles - and not only
9x9 ones. Check http://www.unrealtower.org/sodoku. In the LFX
programming contest they call the puzzle Sodoku, not Sudoku, so that's
why I'm sticking with the name Sodoku for now.

> -any improvements possible for the current code? I didn't play a lot
> with python yet, so I probably missed some typical python tricks,
> like converting for-loops to list expressions.

It all depends on what you want to do. My program can create & solve
puzzles from any size, load and save them to disk, check them for
validity and rank them ('easy', 'medium', 'hard', 'near impossible').
It also implements a puzzle in a class, so it can be used in an OOP
fashion.

> def all(seq, pred=bool):

What's this? What is bool?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

Tom Anderson
Guest
Posts: n/a

 09-17-2005
On Fri, 16 Sep 2005, Bas wrote:

> -any ideas how to easily incorporate advanced solving strategies?
> solve(problem1) and solve(problem2) give solutions, but solve(problem3)
> gets stuck...

the only way to solve arbitrary sudoku problems is to guess.

of course, you have to deal with guessing wrongly, and it helps if you can
make good guesses!

i wrote a solver based entirely on guessing and backtracking a few months
ago; it's fairly simple, although i wrote it in fairly fine-grained java,
so there's actually quite a lot of code. i had a very different program
structure to you, since i was only doing guesswork; i had a recursive
function that looked like this:

def solve(grid):
"""Solves a grid.

The solution is written in the grid; if no solution can be found, does
nothing to the grid. Returns True if a solution was found, False if not.
"""
x, y = pick_an_empty_cell_to_try(grid)
letters = letters_which_can_be_written_in_this_cell(grid, x, y)
if (letters == []):
return False # there are no legal moves; give up
for letter in letters:
grid.write(x, y, letter) # try a move ...
if (solve(grid)):
return True # we win!
grid.erase(x, y) # ... undo it if it didn't work
return False # none of the legal moves work; give up

the implementation of the grid class is pretty straightforward - it's just
a two-dimensional matrix with some bells and whistles.
letters_which_can_be_written_in_this_cell is also fairly simple, although
it can be made a lot faster by keeping summary information in the grid
object: i had a set for each row, column and region saying which letters
had been written already, so the set of available letters was the inverse
of the union of the sets relevant to the cell; the sets were implemented
as bitmaps, so this was all very fast.

the only tricky bit is pick_an_empty_cell_to_try; you can have fun trying
different heuristics here! the nice thing is that it's okay to use quite
computationally expensive heuristics, since a modestly better choice can
save huge numbers of recursions.

there are a number of much, much more advanced algorithms out there, doing
all sorts of clever stuff. however, my algorithm solves any sudoku i can
throw at it (barring seriously unreasonable stuff) in under a second, so
i'm happy with it.

tom

--
I content myself with the Speculative part [...], I care not for the Practick. I seldom bring any thing to use, 'tis not my way. Knowledge is my ultimate end. -- Sir Nicholas Gimcrack

Pierre Barbier de Reuille
Guest
Posts: n/a

 09-17-2005
Tom Anderson a écrit :
> On Fri, 16 Sep 2005, Bas wrote:
>
>> -any ideas how to easily incorporate advanced solving strategies?
>> solve(problem1) and solve(problem2) give solutions, but
>> solve(problem3) gets stuck...

>
>
> the only way to solve arbitrary sudoku problems is to guess.

Well, that's true, but most of the sudoku puzzles can be solved in
linear time ! And also having a linear time solving algorithm allows you
to really reduce the time used when you need backtracking.

BTW, the three given examples can be solved without backtracking.

I made one very recently (mmhh ... first complete version made
yesterday, still need a little bit of debug on the backtracking part),
and it's pretty quick (made in Ruby but well, I suppose timing are
similar), it never get stuck for long even if it fails, it fails quickly ...

Pierre

Benji York
Guest
Posts: n/a

 09-17-2005
Sybren Stuvel wrote:
>>def all(seq, pred=bool):

>
> What's this? What is bool?

See http://docs.python.org/lib/built-in-funcs.html#l2h-10
--
Benji York

David Durkee
Guest
Posts: n/a

 09-17-2005
Hi Bas,

I came across Soduko puzzles recently too and had the same reaction:
why waste my time solving the things when it would be much more fun
to write a Python program to do so?

I've enclosed my script and four puzzle files. The puzzle files are
text files containing nine lines of text, and containing nine digits
or spaces. Your puzzle 3 is the one in the file puzzle4.txt.
Interestingly, the file puzzle3.txt is the first one I ran across
that my earlier version couldn't solve. You must pass in the puzzle
file path as a single parameter to the script.

My script originally used two strategies, which I called solve-by-
needed and solve-by-possible. I found that neither strategy used by
itself completed as much of the difficult puzzles as alternating
between both strategies. However, even both together didn't
completely solve my puzzle 3 (or yours). I found I wasn't even able
to solve it myself unless I narrowed one space down to two
possibilities and took a guess. Of course, sometimes I would guess
wrong and have to keep track of where I was to be able to go back to
that state and make the other guess. Obviously a computer program can
do this more easily than I can, so I incorporated that as a third,
last resort strategy. This, so far, has always worked, and I can't
imagine it not working on a solvable puzzle. It seems like cheating,
though. I wrote it recursively, as making one guess can lead to
another situation where the puzzle is still solvable but strategies
one and two get stuck. I've never seen it recurse more than twice
with a real puzzle. With a really gross test I call "ones test", a
puzzle in which only nine ones are filled in (which obviously has
many possible solutions), it recursed very deeply and still succeeded
in producing a possible solution.

David

On Sep 16, 2005, at 3:45 PM, Bas wrote:

> I came across some of these online sudoku games and thought after
> playing a game or two that I'd better waste my time writing a solver
> than play the game itself any longer. I managed to write a pretty dumb
> brute force solver that can at least solve the easy cases pretty fast.
>

Bas
Guest
Posts: n/a

 09-17-2005
>> def all(seq, pred=bool):

>What's this? What is bool?

That came straight out of the manual for itertools:
http://docs.python.org/lib/itertools-recipes.html

Dennis Lee Bieber
Guest
Posts: n/a

 09-17-2005
On 16 Sep 2005 13:45:24 -0700, "Bas" <(E-Mail Removed)> declaimed the
following in comp.lang.python:

>
> It basically works by listing all 9 possible numbers for all 81 fields
> and keeps on striking out possibilities until it is done.
>
> -any ideas how to easily incorporate advanced solving strategies?
> solve(problem1) and solve(problem2) give solutions, but solve(problem3)
> gets stuck...
>

My version doesn't handle the really ugly ones either.

I never incorporated back-tracking, and the "level 3" puzzles in my
book are full of such ... (watch out for line wrap) {I've just converted
the input logic, it isn't fully tested, and should have a way to "erase"
prior rows for correction}

-=-=-=-=-=-=-=-=-=-

import time

class Cell(object):
def __init__(self):
self.locked = False # final value found
self.value = None
self.candidates = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def set(self, value):
self.locked = True
self.value = value
self.candidates = None

def eliminate(self, value):
if not self.locked:
if value in self.candidates:
self.candidates.remove(value)
return True
return False

class Grid(object):
def __init__(self):
self.cells = [ [Cell(), Cell(), Cell()],
[Cell(), Cell(), Cell()],
[Cell(), Cell(), Cell()] ]

def completed(self):
for r in range(3):
for c in range(3):
if not self.cells[r][c].locked:
return False
return True

class Table(object):
def __init__(self):
self.grids = [ [Grid(), Grid(), Grid()],
[Grid(), Grid(), Grid()],
[Grid(), Grid(), Grid()] ]

self.rows = [None] * 9
for r in range(9):
row = [None] * 9
for c in range(9):
row[c] = self.grids[r / 3][c / 3].cells[r % 3][c % 3]

def set(self, row, col, value):
self.grids[row / 3][col / 3].cells[row % 3][col % 3].set(value)

def get(self, row, col):
return self.grids[row / 3][col / 3].cells[row % 3][col % 3]

def eliminate(self, row, col, value):
changed = False
for c in range(9):
changed = (self.grids[row / 3][c / 3].cells[row % 3][c %
3].eliminate(value)
or changed)

for r in range(9):
changed = (self.grids[r / 3][col / 3].cells[r % 3][col %
3].eliminate(value)
or changed)

grid = self.grids[row / 3][col / 3]
for c in range(3):
for r in range(3):
changed = (grid.cells[r][c].eliminate(value) or changed)

return changed

def display(self):
print "\n\n0 1 2 3 4 5 6 7 8\n=================="
for r in range(9):
for c in range(9):
if self.get(r, c).value:
print "%s" % (self.get(r, c).value),
else:
print " ",
print "|%s" % r

def completed(self):
for r in range(3):
for c in range(3):
if not self.grids[r][c].completed():
return False
return True

if __name__ == "__main__":
myTable = Table()
## print "\n\nEnter a value of 0 to exit setup"
## print "\tRow and Column range 0..8"
##
## while True:
## cin = raw_input("Enter the cell Value Row Column (space
separated: ")
## try:
## cv, cr, cc = cin.split()
## value = int(cv)
## if value == 0: break
## row = int(cr)
## col = int(cc)
## myTable.set(row, col, value)
## myTable.display()
## print
## except:
## print "Error processing input: try again"
## pass
print "\n\nEnter row data in the form: x..y.z..."
print "\twhere . represents and empty cell, and"
print "\t x, y, z represent digits in the range 1..9\n"
for cr in range(9):
while True:
while True:
cin = raw_input("Enter Row %s: " % cr)
if len(cin) == 9: break #correct length
print "*** Enter 9 characters only; '%s' is %s
characters" % (cin, len(cin))
good_row = True
for cc in range(9):
if cin[cc] == ".": continue #empty cell
value = int(cin[cc])
if value == 0:
good_row = False #bad character
print "*** Only 1..9, or . are allowed"
for ec in range(9):
myTable.set(cr, ec, None) #clear row
break
else:
myTable.set(cr, cc, value)
if good_row: break #escape from row input loop
myTable.display()

print "\n\nBuilding table ",
for r in range(9):
for c in range(9):
if myTable.get(r, c).locked:
myTable.eliminate(r, c, myTable.get(r, c).value)

myTable.display()

while True:
print "Evaluating "
change = False
done = True
for r in range(9):
for c in range(9):
print ".",
time.sleep(0.01)
if not myTable.get(r, c).locked:
if len(myTable.get(r, c).candidates) == 1:
myTable.set(r, c, myTable.get(r,
c).candidates[0])
change = myTable.eliminate(r, c,
myTable.get(r,
c).value)
myTable.display()
if change: break
if change: break

print

# check for completion
if myTable.completed():
print "Completed"
break

if not change:
print "Resolving initial deadlock"
for gr in range(3):
for gc in range(3):
myGrid = myTable.grids[gr][gc]
count = [0] * 9
for r in range(3):
for c in range(3):
myGrid.cells[r][c].candidates:
for n in
myGrid.cells[r][c].candidates:
count[n-1] += 1

try:
n = count.index(1) + 1
for r in range(3):
for c in range(3):
if (myGrid.cells[r][c].candidates
and n in
myGrid.cells[r][c].candidates):
myGrid.cells[r][c].set(n)
myTable.eliminate(gr * 3 + r, gc
* 3 + c, n)
myTable.display()
except:
pass

print "Deadlocked; must be resolved manually"
cin = raw_input("Enter value row column: ")
cv, cr, cc = cin.split()
value = int(cv)
row = int(cr)
col = int(cc)
myTable.set(row, col, value)
myTable.eliminate(row, col, value)
myTable.display()
-=-=-=-=-=-=-=-
>
> problem3 = [' 3 5 81 ',
> ' 76 9 ',
> '4 ',
> ' 439 5 6',
> ' 1 7 ',
> '6 8 193 ',
> ' 9',
> ' 9 86 ',
> ' 61 2 8 ']
>

Yes, that is a deadlock set... (You'll need fixed width font)

0 1 2 3 4 5 6 7 8
==================
3 5 8 1 |0
1 7 6 9 |1
4 |2
8 4 3 9 7 5 1 2 6 |3
1 6 7 8 |4
6 8 1 9 3 |5
1 5 7 9 |6
9 8 6 1 |7
6 1 9 2 8 |8
Evaluating
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

0 1 2 3 4 5 6 7 8
==================
3 5 8 1 |0
1 7 6 9 |1
4 1 |2
8 4 3 9 7 5 1 2 6 |3
1 6 7 8 |4
6 8 1 9 3 |5
1 5 7 9 |6
9 8 6 1 |7
6 1 9 2 8 |8
Evaluating
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deadlocked; must be resolved manually
Enter value row column:

Given a set like that, the next approach would be to add recursion:
clone (deep copy) the tableau, find the first empty cell, pick the first
available number for it (saving a list of those tried), then recursively
try to finish the puzzle after using that number... Next deadlock,
clone, pick next empty cell, pick candidate... If you get to a deadlock
where there is an empty cell and NO viable candidates, pop back to the
previous saved tableau and try the next candidate at that location. If
all candidates fail, pop back yet again...

--
> ================================================== ============ <
> http://www.velocityreviews.com/forums/(E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

Diez B. Roggisch
Guest
Posts: n/a

 09-18-2005
As everyone posts his, I'll do the same It uses some constraint based
solving techniques - but not too complicated ones. When stuck, it
backtracks. So far it never failed me, but I haven't tested it too
thouroughly.

Diez

import copy

def condense(vals):

if len(vals) == 0:
return ""
ranges = [0]
ranges += [i+1 for i, v in enumerate(vals[:-1]) if vals[i+1] - v > 1]
ranges.append(len(vals))
ranges = zip(ranges[:-1], ranges[1:])
def f(l):
if len(l) > 1:
return "%i-%i" % (l[0], l[-1])
return "%i" % l[0]
return ", ".join([f(vals[a:b]) for a,b in ranges])

riddle1 = """
1**83***2
57***1***
***5*9*64
7*4**859*
**3*1*4**
*514**3*6
36*7*4***
***6***79
8***52**3
"""
riddle2 = """
**2*9*1*7
*386*****
4********
*****5***
**9*1*3**
***4*****
********4
*****792*
8*6*3*7**
"""
class Constraint(object):
def __init__(self, fields):
self.__fields = fields

def apply(self, sudoko, all_nums = set(xrange(1,10))):
changed = False
placed = set()
[placed.update(sudoko[x][y]) for x,y in self.__fields if len(sudoko[x][y]) == 1]
news = []
for x,y in self.__fields:
old = sudoko[x][y]
if len(old) > 1:
new = old.intersection(all_nums.difference(placed))
if len(new) == 0:
raise ValueError()
if old != new:
changed = True
sudoko[x][y] = new
news.append(((x,y), new))
# naked pair elimination
if changed:
return True
pair_found = False
#print "-" * 30
#print sudoko
#print "-" * 30
for i in xrange(2, 5):
all =[list(n) for f,n in news if len(n) == i]
[l.sort() for l in all]
all = [tuple(l) for l in all]
all.sort()
#print "all ", all
for j, l in enumerate(all[:-1]):
if len(all[j:j+i]) == i and len(set(all[j:j+i])) == 1:
np = set(l)
#print "naked pair", np
for k, (f, n) in enumerate(news):
if n != np:
#print "Adjusted ", f, n
new = n.difference(np)
if len(new) == 0:
raise ValueError()

if new != n:
pair_found =True
news[k] = (f, new)
if pair_found:
for (x,y), n in news:
sudoko[x][y] = n
return pair_found

class Sudoku(object):
def __init__(self, parent=None):
if parent is None:
self.__field = [[set(xrange(1, 10)) for i in xrange(9)] for j in xrange(9)]
self.__constraints = []
# row constraints
for y in xrange(9):
self.__constraints.append(Constraint([(x,y) for x in xrange(9)]))
# column constraints
for x in xrange(9):
self.__constraints.append(Constraint([(x,y) for y in xrange(9)]))
# field constraints
for xx in xrange(0,9,3):
for yy in xrange(0,9,3):
self.__constraints.append(Constraint([(x+xx, y+yy) for x in xrange(3) for y in xrange(3)]))
else:
self.__field = copy.deepcopy(parent.__field)
self.__constraints = parent.__constraints

def __getitem__(self, index):
class Column(object):
def __init__(self, column, field):
self.__column = column
self.__field = field

def __setitem__(self, index, value):
self.__field[self.__column][index] = value

def __getitem__(self, index):
return self.__field[self.__column][index]

return Column(index, self.__field)

def __repr__(self):
res = [[None for i in xrange(9)] for j in xrange(9)]
col_widths = [0 for i in xrange(9)]
for x in xrange(9):
for y in xrange(9):
vals = list(self[x][y])
vals.sort()
r = condense(vals)
res[x][y] = r
if col_widths[x] < len(r):
col_widths[x] = len(r)

rows = []
for y in xrange(9):
rows.append(" ".join(["%s%s" % (res[x][y], " " * (col_widths[x] - len(res[x][y]))) for x in xrange(9)]))

return "\n".join(rows)

lines = [line for line in instance.split() if line]
for x in xrange(9):
for y in xrange(9):
v = lines[y][x]
if v != "*":
self[x][y] = set([int(v)])

def solve(self):
changed = set([True])
while True in changed:
changed = set([c.apply(self) for c in self.__constraints])
if not self.solved:
#print self
def enum_squares():
sqs = [(len(sq), (pos, sq)) for pos, sq in self.squares if len(sq) > 1]
sqs.sort()
for _, (pos, square) in sqs:
for v in square:
yield pos, v
for (x, y), v in enum_squares():
child = Sudoku(self)
child[x][y] = set([v])
try:
child.solve()
if child.solved:
self.__field = child.__field
return
except ValueError:
pass

def squares(self):
for x in xrange(9):
for y in xrange(9):
yield (x,y), self[x][y]
squares = property(squares)

def solved(self):
return len([1 for _, square in self.squares if len(square) == 1]) == 81
solved = property(solved)

if __name__ == "__main__":
s = Sudoku()
s.solve()
print s