This page describes a Groovy solution for ShortestSudokuSolver. Check the link for all the details, but basically the puzzle state is fed in as a String. Each line of the script must be no more than 80 characters in length.

The solution (184 characters plus 2 newlines):

def r(a){def i=a.indexOf(48);if(i<0)print a else(('1'..'9')-(0..80).collect{j->


Add the following line to the script to solve a partially complete puzzle (should take just a few seconds):

r '200375169639218457571964382152496873348752916796831245900100500800007600400089001'

Alternatively, add the following line to the script to solve a puzzle from scratch (may take 30-60 minutes or more):

r '200370009009200007001004002050000800008000900006000040900100500800007600400089001'

The expected output is:


Here is the more usual representation of the puzzle:

You can run it from the command-line by adding ;r args[0] to the end of the script (saved in a file called sudoku.groovy) and then invoking:

> groovy sudoku.groovy 200370009009200007001004002050000800008000900006000040900100500800007600400089001

If you use '.groovy' as your script file extension, you can even leave off the extension as follows:

> groovy sudoku 200370009009200007001004002050000800008000900006000040900100500800007600400089001

A slightly longer version using a matcher and no inner closure (209 characters plus 2 newlines):

def r(a){def m=a=~'0';if(m.find()){int i=m.start();(('1'..'9')-(0..80).collect{
j->int q=j/9,r=i/9,u=q/3,v=r/3,w=j%9/3,x=i%9/3;q==r||j%9==i%9||u==v&&w==x?
a[j]:'0'}).each{r(a[0..<i]+it+a[i+1..-1])}}else print a}

Or without the matcher (193 characters plus two newlines):

def r(a){int i=a.indexOf(48);if(i<0)print a else(('1'..'9')-(0..80).collect{j->
int q=j/9,r=i/9,u=q/3,v=r/3,w=j%9/3,x=i%9/3;q==r||i%9==j%9||u==v&&w==x?a[j]:'0'

Also see another version for a more understandable (though much longer) algorithm - but it also does a lot more.