Search results

Uparrow
2
Downarrow
Daniel Ge

function R
(
a, // the array representing the sudoko grid

// placeholder arguments
i, // index of the last empty cell
j, // index to check the candidates for the cell 'i'
m, // candidate number for the the cell 'i'
g // flag whether 'm' is a already used in the same row|col|node as 'i'
){
// phase 1: look for an empty cell
for
(
i=80;
a[i]; // keep going if the cell isn't empty
i--||+a // decrease the index and call 'a.toString()' if we went through the whole grid
);
// phase 2: check all candidate numbers for the cell 'i'
for
(
m=10;
g=a[i]=--m; // put the candidate in the cell 'i' already and set 'g' to something truthy
// at the end of phase 2, the cell 'i' is reset to 0 for "higher" branches of the recursion
g&&R(a) // recurse if 'm' isn't already used in the same row|col|node as 'i'
)
// phase 3: check if the candidate number is used already
for(j in a) // loop through the whole grid again
g*= // turn 'g' falsy if
a[j^i==j] // we are not on the cell 'i'
^m // and the cell 'j' is set to 'm'
|| // and we are in the same row|col|node as 'i'
i/9^j/9&&i%9^j%9&&i/27^j/27|i%9/3^j%9/3
}

A tiny sudoku solver written in JS (https://gist.github.com/1230481/95f6facb7...
created about 6 years ago

Tags: javascript sudoku

Uparrow
0
Downarrow
Daniel Ge

function R(a,i,j,m,g){for(i=80;a[i];i--||+a);for(m=10;g=a[i]=--m;g&&R(a))for(j in a)g*=a[j^i==j]^m||i/9^j/9&&i%9^j%9&&i/27^j/27|i%9/3^j%9/3}

Even tinier js sudoku solver
created about 6 years ago

Tags: javascript sudoku