Wednesday, June 15, 2022
HomeMathFixing Knightdoku Challenges the Wolfram Method—Wolfram Weblog

Fixing Knightdoku Challenges the Wolfram Method—Wolfram Weblog

Every situation of the Mathematical Affiliation of America’s Math Horizons presents readers with puzzles to resolve, and the April 2021 situation included the “Knightdoku” problem created by David Nacin, a math professor at William Paterson College in Wayne, New Jersey.

On this puzzle, a easy Sudoku-like downside is described based mostly on chess knights. Every cell within the 9×9 grid could comprise a knight. The preliminary board configuration defines the location of a set of knights that has a selected variety of knights that should be current of their neighborhood within the resolution. The neighborhood of a knight is the set of cells that’s reachable from the knight within the one L-shaped chess transfer that knights are allowed to make.

Along with the preliminary placement of knights, a legitimate resolution should obey a Sudoku-like constraint. Particularly, every row, every column and every 3×3 block should have precisely three knights.

The puzzle included two board configurations to resolve: a warm-up board and a daily—that’s, tougher!—one. Right here’s the warm-up board:

Warm-up puzzle board
© Mathematical Affiliation of America, 2021. All rights reserved.

And right here’s the common board:

Regular puzzle board
© Mathematical Affiliation of America, 2021. All rights reserved.

The problem for me was not simply fixing Nacin’s Knightdoku puzzle however to take action utilizing the Wolfram Language, which affords quite a lot of methods to resolve Sudoku puzzles.

Fixing a Sudoku-like Drawback Based mostly on Chess Knights

Video games like Sudoku are comparatively easy to resolve utilizing Boolean constraint solvers. In essence, you boil the issue all the way down to relations amongst a set of logical variables that symbolize potential board configurations.

For instance, if now we have two cells the place we need to make one true and the opposite false, we are able to create 4 variables: two for the primary cell (cell1false, cell1true) and two for the second cell (cell2false, cell2true). A sound configuration would fulfill the constraint (cell1false and cell2true) or (cell1true and cell2false). This logical expression ((cell1false&&cell2true)||(cell1true&&cell2false)) will be handed to a satisfiability solver to find out if a configuration exists that satisfies the logical constraints.

Helper Features

First, we should create some helper features for forming conjunctions and disjunctions from lists that can be helpful later in constructing our logical expressions:


Board Configuration

Preliminary board configuration is an inventory of triples: {x,y,n} the place {x,y} is the place on the board (utilizing 1-based indices), and n is the variety of neighbors of the knight at {x,y} that comprise a knight within the resolution. A neighbor is outlined as a cell reachable through a authorized knight transfer.

First, we create a primary configuration for the warm-up board:


Then we make the common board configuration:


For comfort, we can even create some associations to be used later in trying up these preliminary markings after we plot the solver outcomes:


Defining Logical Variables

We have to encode the state of the board through logical variables, so we outline a set of Boolean values for the potential states of every cell (has a knight, has no knight). We use the conference that s[[i,j,1]] means {i,j} has a knight, and that s[[i,j,2]] means there is no such thing as a knight:

s = Table

We’ll additionally create an affiliation mapping coordinates to the 2 logical variables for that coordinate (that is largely helpful in debugging and constraints):

scoords =  Association

The primary logical constraint we set up is critical to ensure {that a} cell is both marked or unmarked. Having a cell that’s neither marked nor unmarked, or each marked and unmarked, is invalid, so we exclude them:

uniqueCellConstraint = AndList

A lot of the code we write for constraints seems like this. On this case, the innermost tables arrange a per-cell constraint. We then map AndList, the operate we created earlier, over the desk to type a conjunction from the columns of every row of the desk, after which apply AndList yet one more time to conjoin the rows into one massive logical expression.

Neighborhood Constraints

For our preliminary configuration, we have to contemplate the cells which can be reachable from every knight, obeying the boundaries of the board. We will write a easy operate that enumerates the coordinates of the neighbors of a cell {x,y}:


For a given place and variety of anticipated knight neighbors, generate all potential legitimate assignments. We obtain this by taking the set of neighbors and associating with every a worth of 1 or 2. The order of 1 and a pair of assignments is achieved by calculating all permutations of a sequence of 1s and 2s containing the suitable variety of 1s and 2s based mostly on the anticipated variety of neighbors. We embody the knight within the middle of the neighborhood as marked (s[[x,y,1]]):


Combining these is much like what we did above, however with the addition of Or within the expression. Particularly, we have to And the neighbors collectively for every neighborhood, and be a part of every potential neighborhood with an Or. Lastly, we conjoin all of those And/Or expressions throughout all preliminary knight markings:


Board Constraints

We additionally want so as to add the generic board constraints which can be much like Sudoku: at most three knights per row, column and three×3 block. These comply with the identical sample as above: we create all permutations of marked/unmarked for every row, column and block, and be a part of them with And and Or operators.

Add a constraint that at most three per row will be set:

rowConstraints =  AndList

Equally, set a constraint for at most three per column:

colConstraints =  AndList

Additionally add a constraint for 3×3 packing containers:

boxConstraints =  AndList

Remedy the System

Now we’re prepared to resolve each puzzle boards.

Board Configuration #1

We will remedy the system utilizing the satisfiability solver over the set of logical variables:

soln = SatisfiabilityInstances

For visualization, we reshape the end result to find out what the project was to every logical variable in the identical form because the board. We point out the unique knights as with a superscript indicating the variety of neighbors it should have. The knights that had been stuffed in by the solver are indicated as :

res = ArrayReshape
m = Table

Board Configuration #2

We will apply the identical method to the second, tougher board supplied by Nacin:

soln = SatisfiabilityInstances
res = ArrayReshape
m = Table

In case you’re enthusiastic about different examples of the Wolfram Language utilized to Sudoku puzzles, try posts comparable to “Fixing Sudoku as an Integer Programming Drawback” and “Utilizing Recursion and FindInstance to Remedy Sudoku” by Wolfram Neighborhood members. You may also discover extra fascinating puzzles created by David Nacin at Quadrata, his puzzle weblog.

Matthew Sottile is a pc scientist who works within the Middle for Utilized Scientific Computing on the Lawrence Livermore Nationwide Laboratory. His analysis is within the areas of programming languages, compilers and formal strategies. Sottile has been an energetic Wolfram Mathematica consumer since 1995 and makes use of it for generative artwork, fixing puzzles and finding out rewriting and proof programs.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments