My attempt at writing a Sudoku solver

Screenshot of the Sudoku solver

Back in November of 2008, a conversation at a family gathering inspired me to write a (Flash) program that could solve Sudoku puzzles. A relative had been trying to write a solver himself, and it seemed like a nice challenge. The resulting Flash movie was on this blog for a couple of years, but it had two issues. It was Flash, and it would fail to solve some more difficult puzzles.

During the Christmas holidays this year, I had some time on my hands, and decided to give it a second try. I used Javascript this time, and instead of trying to find the solution using a set of rules, I wanted to use some type of brute force.

The algorithm

I could have looked up existing algorithms, and implemented them. But then it wouldn’t have been a puzzle. The idea was to create my own algorithm, and see how well it did. I’m using a timer to slow down the solving, and update the screen so you can see what the script is doing. Here’s what it does for each “step”.

  1. Find all empty squares
  2. If there are none, the puzzle is solved
  3. For each empty position, determine the number of possible values (a number between 0 and 9)
  4. Find the position with the lowest number of possible values
  5. If there are zero possibilities, the current solution is wrong, return to the last known correct state
  6. If there’s just one possibility, record that value as being correct

For nearly all puzzles except very easy ones, there will soon only be positions that have multiple possible values. This is when I switch to a second “mode”, where each step is slightly different.

  1. Like before, find the position with the lowest number of possible values
  2. Randomly fill in one of the possible values
  3. Since we’re now guessing, stop recording values as correct
  4. At some point, there’s either a square with zero possibilities, or the puzzle is solved

Since “mode 2” is essentially “brute force with educated guesses”, this is where solving slows down considerably. But, it’ll never give up, and theoretically it should be able to solve all puzzles. Might take century or two, but still…

Testing

While developing the script, I’d been using a level 4 sudoku from a puzzle book I had lying around. Not very challenging. I obviously needed to see how it would do against harder to solve puzzles.

My first test was to set the Sudoku app on my phone to difficulty level “extreme”, and copy the resulting puzzle into my solver. It solved it within seconds. Promising.

I then looked for even more difficult puzzles, and found this page, which lists four extremely hard to solve sudokus. These proved to be a proper challenge. I could see the solver resorting to “mode 2” quickly, and one even took 5 minutes to complete. But eventually it did.

This convinced me that my solver can solve any “human-solvable” sudoku puzzle. It’s not optimized for speed at all. Maybe I should try to write the same algorithm in a faster language to see how fast it can be. For now, I’m quite happy with it as it is.

The source code is available on Github. Please feel free to contribute. Thanks to Alex Goller for improving usability on mobile devices.

Roy Tanck
I'm a freelance WordPress developer, designer, consultant, meetup organizer and speaker. In my spare time I love to go out and take pictures of things.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.