Function Repository Resource:

EstimateSudokuDifficultyLevel

Source Notebook

Estimate the level of difficulty for a given sudoku puzzle

Contributed by: Paritosh Mokhasi

ResourceFunction["EstimateSudokuDifficultyLevel"][mat]

estimates the difficulty level of a sudoku puzzle specified as mat.

Details and Options

The input matrix must be a SparseArray square matrix.
ResourceFunction["EstimateSudokuDifficultyLevel"] returns a value between 1 and 10, with 1 being extremely easy and 10 being extremely hard.
The function estimates the difficulty level by recursively applying the standard rules of sudoku puzzles until no unique elements are left.
The three rules of a standard n×n sudoku board are:
First, each row must contain all the numbers 1–n.
Second, each column must contain all the numbers 1–n.
Third, each 3×3 block must contain all the numbers 1–n.
The function returns $Failed if it finds that the sudoku puzzle violates any of the puzzle rules.

Examples

Basic Examples (2) 

Estimate the difficulty level of the specified sudoku puzzle:

In[1]:=
Grid[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}, _], 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[1]=
In[2]:=
ResourceFunction["EstimateSudokuDifficultyLevel"][puzzle]
Out[2]=

Estimate the difficulty level of a 1616 sudoku puzzle:

In[3]:=
Grid[puzzle = SparseArray[
  Automatic, {16, 16}, Blank[], {1, {{0, 8, 16, 27, 35, 47, 57, 66, 79, 91, 99, 109, 120, 129, 135, 144, 154}, {{1}, {4}, {5}, {7}, {8}, {11}, {14}, {
     16}, {4}, {5}, {6}, {8}, {11}, {13}, {14}, {16}, {1}, {3}, {4}, {
     5}, {6}, {8}, {9}, {10}, {13}, {15}, {16}, {1}, {2}, {6}, {8}, {
     10}, {12}, {13}, {15}, {2}, {3}, {5}, {6}, {7}, {8}, {9}, {10}, {
     12}, {14}, {15}, {16}, {1}, {3}, {4}, {5}, {7}, {11}, {12}, {
     13}, {14}, {15}, {1}, {4}, {5}, {6}, {8}, {10}, {11}, {13}, {
     16}, {1}, {2}, {3}, {4}, {6}, {7}, {8}, {10}, {11}, {12}, {13}, {
     14}, {16}, {1}, {2}, {3}, {5}, {6}, {7}, {8}, {11}, {12}, {14}, {
     15}, {16}, {1}, {4}, {9}, {10}, {11}, {14}, {15}, {16}, {2}, {
     3}, {4}, {5}, {8}, {9}, {12}, {13}, {14}, {15}, {1}, {4}, {5}, {
     6}, {7}, {8}, {9}, {10}, {12}, {14}, {15}, {2}, {5}, {7}, {8}, {
     9}, {10}, {11}, {13}, {16}, {1}, {2}, {4}, {5}, {10}, {13}, {
     1}, {4}, {5}, {6}, {7}, {12}, {14}, {15}, {16}, {1}, {2}, {3}, {
     4}, {6}, {7}, {8}, {9}, {10}, {15}}}, {3, 13, 15, 9, 1, 8, 4, 12,
     7, 3, 12, 14, 1, 16, 15, 13, 4, 12, 14, 10, 8, 11, 5, 13, 2, 7, 9, 8, 16, 7, 4, 14, 9, 5, 11, 2, 10, 8, 3, 1, 9, 12, 6, 14, 13, 16, 7, 7, 16, 12, 11, 6, 9, 1, 14, 5, 2, 14, 9, 7, 13, 5, 11, 2, 15, 1, 11, 1, 6, 8, 14, 4, 2, 15, 7, 5, 3, 12, 10, 1, 12, 2, 9, 11, 7, 3, 10, 13, 14, 4, 16, 15, 11, 6, 16, 14, 9, 3, 5, 9, 3, 4, 14, 6, 1, 12, 7, 10, 13, 10, 6, 5, 1, 13, 16, 7, 9, 4, 2, 15, 4, 1, 8, 15, 14, 12, 6, 13, 2, 13, 5, 3, 2, 1, 8, 6, 2, 4, 16, 14, 7,
     11, 12, 15, 12, 15, 11, 10, 5, 3, 7, 9, 2, 1}}], 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[3]=
In[4]:=
ResourceFunction["EstimateSudokuDifficultyLevel"][puzzle]
Out[4]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 21 May 2020

Related Resources

License Information