Function Repository Resource:

SolveKillerSudokuPuzzle

Source Notebook

Solve a killer sudoku puzzle

Contributed by: Paritosh Mokhasi

ResourceFunction["SolveKillerSudokuPuzzle"][pos,vals]

solves a killer sudoku puzzle based on the position pos of cages and the values vals associated with the cages.

Details and Options

The lengths of the first and second arguments must be the same.
Each element of the first argument must be a list containing the matrix positions of the cage. Each position in a cage represents an unknown number.
Each element of the second argument must be a positive integer and represents the sum of all the numbers in that cage.
The objective is to use the cage information to find all the elements of the board such that the final solved puzzle must obey the three rules of a standard sudoku puzzle.
The three rules of a standard sudoku board are:
First, each row must contain all the numbers 1–9.
Second, each column must contain all the numbers 1–9.
Third, each 3×3 block must contain all the numbers 1–9.

Examples

Basic Examples (2) 

Solve the following killer sudoku puzzle:

In[1]:=
{cagePos, cageVals} = { {{{1, 1}, {1, 2}}, {{1, 4}, {2, 4}}, {{1, 7}, {2, 7}, {2, 8}, {3, 7}}, {{4, 4}, {4, 5}}, {{6, 1}, {6, 2}, {7, 1}, {
    7, 2}}, {{6, 7}, {6, 8}}, {{7, 4}, {8, 4}, {8, 5}, {9, 4}}, {{9, 7}, {9, 8}}, {{1, 5}, {1, 6}, {2, 5}}, {{2, 1}, {2, 2}, {3, 1}, {
    4, 1}, {5, 1}, {5, 2}}, {{2, 9}, {3, 8}, {3, 9}}, {{4, 7}, {5, 7}}, {{5, 4}, {5, 5}, {6, 4}}, {{8, 1}, {9, 1}}, {{7, 7}, {8, 6}, {8, 7}, {9, 5}, {9, 6}}, {{1, 8}, {1, 9}}, {{2, 6}, {3, 6}}, {{3, 2}, {3, 3}, {4, 2}, {4, 3}}, {{4, 8}, {4, 9}}, {{6, 5}, {6, 6}, {7, 5}, {7, 6}}, {{7, 8}, {7, 9}}, {{9, 2}, {9, 3}}, {{1, 3}, {2, 3}}, {{3, 4}, {3, 5}}, {{4, 6}, {5, 6}}, {{5, 8}, {5, 9}, {6, 9}}, {{5, 3}, {6, 3}, {7, 3}, {8, 2}, {8, 3}}, {{
    8, 8}, {8, 9}, {9, 9}}}, {8, 14, 14, 5, 17, 11, 10, 10, 12, 26, 21, 16, 18, 16, 26, 10, 9, 25, 5, 26, 10, 9, 15, 10, 12, 13, 19, 18}};
solvedPuzzle = ResourceFunction["SolveKillerSudokuPuzzle"][cagePos, cageVals]
Out[2]=

Display the solved puzzle:

In[3]:=
Grid[solvedPuzzle, 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[3]=

Scope (4) 

The cage positions are a List of sublists where each sublist contains the positions within that cage:

In[4]:=
pos = {{{1, 1}, {1, 2}}, {{1, 4}, {2, 4}}, {{1, 7}, {2, 7}, {2, 8}, {3, 7}}, {{4, 4}, {4, 5}}, {{6, 1}, {6, 2}, {7, 1}, {7, 2}}, {{6, 7}, {6, 8}}, {{7, 4}, {8, 4}, {8, 5}, {9, 4}}, {{9, 7}, {9, 8}}, {{1, 5}, {1, 6}, {2, 5}}, {{2, 1}, {2, 2}, {3, 1}, {4, 1}, {5, 1}, {5, 2}}, {{2, 9}, {3, 8}, {3, 9}}, {{4, 7}, {5, 7}}, {{5, 4}, {5, 5}, {6, 4}}, {{8, 1}, {9, 1}}, {{7, 7}, {8, 6}, {8, 7}, {9, 5}, {9, 6}}, {{1, 8}, {1, 9}}, {{2, 6}, {3, 6}}, {{3, 2}, {3, 3}, {4, 2}, {4, 3}}, {{4, 8}, {4, 9}}, {{6, 5}, {6, 6}, {7, 5}, {7, 6}}, {{7, 8}, {7, 9}}, {{9, 2}, {9, 3}}, {{1, 3}, {2, 3}}, {{3, 4}, {3, 5}}, {{4, 6}, {5, 6}}, {{5, 8}, {5, 9}, {6, 9}}, {{5, 3}, {6, 3}, {7, 3}, {8, 2}, {8, 3}}, {{8, 8}, {8, 9}, {9, 9}}}
Out[4]=

The cage values are the sums of the numbers at each position in the cage:

In[5]:=
vals = {8, 14, 14, 5, 17, 11, 10, 10, 12, 26, 21, 16, 18, 16, 26, 10, 9, 25, 5, 26, 10, 9, 15, 10, 12, 13, 19, 18}
Out[5]=

Solve the puzzle:

In[6]:=
ResourceFunction["SolveKillerSudokuPuzzle"][pos, vals]
Out[6]=

See the solution:

In[7]:=
Normal[%]
Out[7]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 19 May 2020

Related Resources

License Information