Function Repository Resource:

SolveSudokuPuzzle

Source Notebook

Solve a sudoku puzzle

Contributed by: Paritosh Mokhasi

ResourceFunction["SolveSudokuPuzzle"][mat]

solves a sudoku puzzle with the input mat specified by a SparseArray matrix.

Details and Options

The input matrix must be a SparseArray square matrix with the row length a square integer. All input values must be integers with a magnitude between 1 and the puzzle dimension.
If an element in the matrix is negative, then ResourceFunction["SolveSudokuPuzzle"] solves the puzzle assuming that the specified number cannot exist at that position.
The conditions that define a solution for dimension dim are that all integers in a row or column be distinct (a permutation of 1dim) and that subsquares of size also be a permutation of that set.
If the input has no solution, then it is returned unevaluated.
If there are multiple solutions, ResourceFunction["SolveSudokuPuzzle"] returns one of them.

Examples

Basic Examples (2) 

Solve a standard Sudoku puzzle:

In[1]:=
solvedPuzzle = ResourceFunction["SolveSudokuPuzzle"][
   puzzle = SparseArray[{{1, 3} -> 5, {1, 4} -> 3, {2, 1} -> 8, {2, 8} -> 2, {
       3, 2} -> 7, {3, 5} -> 1, {3, 7} -> 5, {4, 1} -> 4, {4, 6} -> 5, {4, 7} -> 3, {5, 2} -> 1, {5, 5} -> 7, {5, 9} -> 6, {
       6, 3} -> 3, {6, 4} -> 2, {6, 8} -> 8, {7, 2} -> 6, {7, 4} -> 5, {7, 9} -> 9, {8, 3} -> 4, {8, 8} -> 3, {9, 6} -> 9, {
       9, 7} -> 7}, {9, 9}, _]];
Grid[#, Sequence[Dividers -> Table[{
Prepend[
Table[True, 2], 
Thickness[2]]}, 2], Background -> {Automatic, Automatic, 
Flatten[
Table[{i, j} -> If[
EvenQ[
Apply[Plus, 
Floor[{i - 1, j - 1}/3]]], 
Darker[White, 0.2], White], {i, 9}, {j, 9}]]}]] & /@ {puzzle, solvedPuzzle}
Out[2]=

Solve a 1616 Sudoku puzzle:

In[3]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/6dfce4ba-4a15-4410-8804-8e727b9c5fa3"]
Out[4]=

Scope (3) 

Generate a full standard Sudoku board by specifying the diagonal elements:

In[5]:=
Grid[ResourceFunction["SolveSudokuPuzzle"][
  DiagonalMatrix[SparseArray[Range[9]]]], Sequence[
 Dividers -> Table[{
Prepend[
Table[True, 2], 
Thickness[2]]}, 2], Background -> {Automatic, Automatic, 
Flatten[
Table[{i, j} -> If[
EvenQ[
Apply[Plus, 
Floor[{i - 1, j - 1}/3]]], 
Darker[White, 0.2], White], {i, 9}, {j, 9}]]}]]
Out[5]=

Generate a full 1616 Sudoku board by randomly specifying the first column:

In[6]:=
Grid[ResourceFunction["SolveSudokuPuzzle"][
  SparseArray[
   Thread[Thread[{Range[16], 1}] -> RandomSample[Range[16]]], {16, 16}]], Sequence[Dividers -> Table[{
Prepend[
Table[True, 3], 
Thickness[2]]}, 2], Background -> {Automatic, Automatic, 
Flatten[
Table[{i, j} -> If[
EvenQ[
Apply[Plus, 
Floor[(1/4) {i - 1, j - 1}]]], 
Darker[White, 0.2], White], {i, 16}, {j, 16}]]}]]
Out[6]=

Generate a Sudoku board by specifying elements that cannot exist at certain positions:

In[7]:=
Grid[ResourceFunction["SolveSudokuPuzzle"][
  DiagonalMatrix[SparseArray[-Range[9]]]], Sequence[Dividers -> Table[{
Prepend[
Table[True, 2], 
Thickness[2]]}, 2], Background -> {Automatic, Automatic, 
Flatten[
Table[{i, j} -> If[
EvenQ[
Apply[Plus, 
Floor[{i - 1, j - 1}/3]]], 
Darker[White, 0.2], White], {i, 9}, {j, 9}]]}]]
Out[7]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 18 May 2020

Related Resources

License Information