3  Mechanically solving Sudoku puzzles

This section gives an overview of how your program will proceed, and the rest of this document breaks down the job down into a number of programming tasks. Read this section carefully before beginning to program.

Overall, you will solve Sudoku puzzles by translating them to logical formulas and then simplifying the formulas. First, you convert the rules above into a list of formulas that must all be true and to write a program that simplifies the formulas until there are no more simplifications possible.

We begin with 93 (729) claims. Each claim consists of three numbers: a column-number (between 1 and 9), a row-number (between 1 and 9), and a digit (between 1 and 9).

;; an claim is:
;;    (make-claim number[1-9] number[1-9] number[1-9])
(define-struct claim (i j n))

Each of the claim's truth values indicates if a particular cell has a particular value. For example, if (make-claim 2 3 4) holds then the cell at (2,3) has a 4. If the claim (make-claim 2 3 5) does not hold, then the cell at (2,3) does not have a 5. The coordinates use the standard computer-science ordering: the upper-left is (0,0), increasing the first coordinate moves to the right and increasing the second coordinate moves down.

Each of the rules of Sudoku corresponds to some axioms about all Sudoku puzzles, and we can express each axiom as a list of claims where exactly one of the claims in the list must true, and the rest must be false. (Of course we do not know ahead of time which is the true claim and which are the false ones and which is which varies for each puzzle.)

;; an axiom is:
;;     (listof claim)

As an example, the first rule tells us that there can only be a single number 1 in the first row. Written as an axiom, we have this:

(list (make-claim 1 1 1) (make-claim 2 1 1) (make-claim 3 1 1)
      (make-claim 4 1 1) (make-claim 5 1 1) (make-claim 6 1 1)
      (make-claim 7 1 1) (make-claim 8 1 1) (make-claim 9 1 1))

Similarly, the second rule tells us that there can only be a single number 1 in the first column, which is represented by this axiom:

(list (make-claim 1 1 1) (make-claim 1 2 1) (make-claim 1 3 1)
      (make-claim 1 4 1) (make-claim 1 5 1) (make-claim 1 6 1)
      (make-claim 1 7 1) (make-claim 1 8 1) (make-claim 1 9 1))

The third rule tells us that there can only be a single number 1 in the top-left 3x3 grid, which corresponds to this axiom:

(list (make-claim 1 1 1) (make-claim 1 2 1) (make-claim 1 3 1)
      (make-claim 2 1 1) (make-claim 2 2 1) (make-claim 2 3 1)
      (make-claim 3 1 1) (make-claim 3 2 1) (make-claim 3 3 1))

Finally, this axiom tells us that there can be only one number in the top-left cell:

(list (make-claim 1 1 1) (make-claim 1 1 2) (make-claim 1 1 3)
      (make-claim 1 1 4) (make-claim 1 1 5) (make-claim 1 1 6)
      (make-claim 1 1 7) (make-claim 1 1 8) (make-claim 1 1 9))

Of course, there are many more axioms, but these give you the flavor of them.

There are two basic rules for learning the truth value of a claim, based on the axioms.

  1. If one of the claims in an axiom is true, then all of the rest must be false.

  2. If all of the claims in an axiom are false except for one, then that one must be true.

So, to solve a Sudoku puzzle, we can translate the initial, given numbers from the puzzle into some claims that we know are true. Then, we can use the two basic rules to find out the truth values for all of the rest of the claims. The rest of the document gives some guidance on how to do that and how to build a stepper that allows you to watch your solver chug away.