Instantly share code, notes, and snippets.

Last active May 23, 2024 13:46
Show Gist options
• Save p01/1230481 to your computer and use it in GitHub Desktop.
Sudoku Solver in 140bytes

Sudoku Solver in 140 bytes

Solve a Sudoku grid only using magic, recursion, and 140bytes of brute force.

This entry was inspired and built on top of the itsy bitsy shoulders of the smallest sudoku solvers in Perl (120b), Ruby (122b), Python (178b), ...

Credits go this way:

``````166b @p01 .......... initial implementation
147b @p01 .......... initial golf
146b @qfox ......... loop optimization
145b @qfox ......... output with closured callback
140b @p01 .......... output with hijacked Array.prototype.toString()
141b @maksverver ... fixed the glitchy j^i==j test
140b @fgnass ....... ReferenceError exit trick + cross browser fix
``````

Thanks to everyone who helped golf and fix this puppy.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // /!\ WARNING this function assumes that there is no variable 'A' in the global scope function R ( a, // the array representing the sudoku 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 throw a ReferenceError exception 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*= // j==i // keep 'g' as is if we are on the cell 'i' || // otherwise, turn 'g' falsy if a[j]^m // the cell 'j' is set to 'm' || // and 'i' and 'j' are in the same row|col|node i%9^j%9&&i/9^j/9&&i/27^j/27|i%9/3^j%9/3 }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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*=j==i||a[j]^m||i%9^j%9&&i/9^j/9&&i/27^j/27|i%9/3^j%9/3}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2011 Mathieu 'p01' Henri - http://www.p01.org/releases/ Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 { "name": "sudokuSolver", "description": "Brute force sudoku solver.", "keywords": [ "sudoku", "solver", "recursive", "brute force" ] }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 Sudoku Solver in 140bytes

Sudoku Solver in 140bytes

Input value:

Expected value: 4,2,8,1,5,9,6,7,3,1,9,6,3,7,4,8,2,5,3,7,5,8,6,2,9,4,1,9,8,1,4,2,3,5,6,7,5,6,4,7,1,8,3,9,2,7,3,2,5,9,6,1,8,4,2,4,3,6,8,1,7,5,9,6,1,7,9,4,5,2,3,8,8,5,9,2,3,7,4,1,6

Actual value:

atk commented Sep 30, 2011

I agree, though I'd rather find a way to shorten up the calculation for 1 bit...