Function Repository Resource:

DisplayKillerSudokuPuzzle

Source Notebook

Display a killer sudoku puzzle

Contributed by: Paritosh Mokhasi

ResourceFunction["DisplayKillerSudokuPuzzle"][pos,vals]

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

Details and Options

The length of the first and second arguments must be the same.
Each element of the pos must be a list containing the matrix positions of the cage. Each position in a cage represents an unknown number.
Each element of the vals 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 board.
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) 

Display a killer sudoku board for specified cage positions and values:

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}};
ResourceFunction["DisplayKillerSudokuPuzzle"][cagePos, cageVals]
Out[2]=

The sudoku matrix associated with the above puzzle can be used to check the validity of the puzzle by summing the elements of each cage:

In[3]:=
Grid[SparseArray[
 Automatic, {9, 9}, 0, {1, {{0, 9, 18, 27, 36, 45, 54, 63, 72, 81}, {{1}, {2}, {3}, {
    4}, {5}, {6}, {7}, {8}, {9}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {
    8}, {9}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {1}, {2}, {
    3}, {4}, {5}, {6}, {7}, {8}, {9}, {1}, {2}, {3}, {4}, {5}, {6}, {
    7}, {8}, {9}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {1}, {
    2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {1}, {2}, {3}, {4}, {5}, {
    6}, {7}, {8}, {9}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {
    9}}}, {1, 7, 9, 6, 5, 3, 4, 2, 8, 5, 2, 6, 8, 4, 7, 3, 1, 9, 4, 3,
    8, 9, 1, 2, 6, 7, 5, 6, 9, 5, 3, 2, 8, 7, 4, 1, 8, 1, 3, 7, 6, 4, 9, 5, 2, 2, 4, 7, 5, 9, 1, 8, 3, 6, 3, 8, 2, 1, 7, 9, 5, 6, 4, 9, 6, 1, 4, 3, 5, 2, 8, 7, 7, 5, 4, 2, 8, 6, 1, 9, 3}}], 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 (3) 

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]=

Create a board from the positions and values:

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

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 19 May 2020

Related Resources

License Information