Sudoku Solver!

While watching a rerun of NUMB3RS on TV, the mathmatician, Charlie, suggested to his dad that he should just right an algorithm to solve the Sudoku puzzles. Hmm, I could do that!

I started out writing a short vbscript that would attempt to solve the puzzle.

  1. Find the possible values for each square and populate all the squares with only 1 possible value.
  2. Find the squares, where only 1 square in the 3x3 grid can have a specific value, and populate those squares.
  3. Find the squares where the rows and columns do not have conflicting possible values to one in this square.

This could solve virtually all Easy and Moderate rated puzzles. When using attempting a Hard puzzle, it would fail when only squares with 2+ possible values remained.

Reading in a Sudoku puzzle book, they suggested a few additional methods and I added logic for these scenarios.

  1. When you have 2 squares in a row/column/3x3 grid containing the same 2 possible values, these are the ONLY squares that can contain these values. Eliminate them from all other's in the same row/column/3x3 grid.
  2. Make a guess (covered next).

This allowed the script to solve several HARD puzzles that the previous script crashed on. Unfortunately, not all mulitple possible value scenarios have multiples.

Alright, now time to guess. To guess, the script does a few things.

  1. Finds all squares with 2 possible values.
  2. Eliminates all squares with similar possible values.
  3. Eliminates all squares on same row/column as other squares.
  4. Once a guess is made, it marks it location and guess value.
  5. Each subsequent square is marked as Post-Guess.

If the first guess is wrong, it uses the post-guess values to revert the changes made. The script then attempts the second possible value in the guess. If this also fails, it reverts again, and finds another square.

During my practice runs, the script has completed 'Diabolical' level puzzles in a matter of seconds!! Solved scripts are saved to the root of C:\ in a CSV format.

AttachmentSize
sudoku_8-4.zip6.83 KB