Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Display a killer sudoku puzzle
| ResourceFunction["DisplayKillerSudokuPuzzle"][pos,vals] displays a killer sudoku puzzle based on the position pos of cages and the values vals associated with the cages. | 
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]](https://www.wolframcloud.com/obj/resourcesystem/images/2fb/2fbeef37-b24c-4f4a-87b1-d18fb03112c0/54978f16d0cd538f.png) | 
| 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}]]}]]](https://www.wolframcloud.com/obj/resourcesystem/images/2fb/2fbeef37-b24c-4f4a-87b1-d18fb03112c0/3627ea175bcf7d27.png) | 
| Out[3]= |  | 
The cage positions are a List of sublists where each sublist contains the positions within that cage:
| In[4]:= |  | 
| Out[4]= |  | 
The cage values are the sums of the numbers at each position in the cage:
| In[5]:= |  | 
| Out[5]= |  | 
Create a board from the positions and values:
| In[6]:= | ![ResourceFunction["DisplayKillerSudokuPuzzle"][pos, vals]](https://www.wolframcloud.com/obj/resourcesystem/images/2fb/2fbeef37-b24c-4f4a-87b1-d18fb03112c0/426783261752c464.png) | 
| Out[6]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License