Sudoku Automation Solver Challenge – R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
On a recent flight I was bored waiting for the plane to land and I tried out the electronic sudoku game that they had offered. I found the game surprisingly interesting as I realized that it is far more entertaining when you cannot use paper or pencil to augment the cognitive solution seeking process. In addition, having your score based on your time presented an objective challenge and measure to test yourself against.
![]() |
Figure 1:a sample Sudoku grid |
After that I decided to design a Sudoku generator. Which I have included:
https://github.com/EconometricsBySimulation/2013-06-14-Sudoku
It has been an entertaining exercise figuring out methods to produce matrices that conform to the specifications of a Sudoku table. That is a table which distributes sets of 1 to 9 distributed both horizontally, vertically, and in a 3×3 grid without duplicates for all squares in a 9×9 grid.
![]() |
Figure 2: Ultimate Sudoku |
You can see from the R script (and the grid produced by it figure 1) that I am able to accomplish this very easily through a randomized guess and check process (with the computer finding a solution in 1 in 50-500 attempts) that is capable of drawing nearly an infinite number of grids (of course there is a finite though very large number of possible grids).
I have also made a variant that I call Ultimate Sudoku which conforms to Sudoku rules and also adds one more rule to the grid in which each position in a 3×3 set is part of a set of all other 3×3 sets in which there can be no duplicate entries from the set. That is for instance that the first square in a 3×3 grid cannot have duplicate entries with any of the other first squares in the other grid. An example table that I generated using R is figure 2.
I don’t think it is likely that Ultimate Sudoku will take off but for those hard core Sudoku players this variate would present an option that would allow for more challenging games since players must keep track of four overlapping sets of elements rather than 3. Under this set up effectively more spaces could be made blank. The R script is also capable of generating these though it is a bit more challenging to find grids that conform to all four rules (usually between 1 out of 4-10 thousand attempts).
I have also defined a function to strategically remove values from the plotted grid so that players are faced with an incomplete grid. I have been able to vary the difficulty of these grids as well by specifying the average number of missing elements (from the set of 1 through 9) for each grid value. You can see examples of problem grids I have generated in figures 3 and 4.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.