Sudoku representation.
The system sdku-rep
contains useful abstractions for dealing with
Sudoku problems of order n.
A problem instance is expressed with a grid
object, which holds:
cell
s, indexed with pos
(row and columns).cell
s.Following Peter Norvig [1] (and no doubt many other authors), we structure
the problem of sudoku as follows:
n
. It contains $n^2 \times n^2$ cells.Formulated this way, the physical Sudoku grid can be seen as merely a
map indicative of how the sets of cells overlap. (It would be
interesting to see what the generalization of sudoku problems to other
overlapping schemes looks like. If you know what mathematical object
that is and can point to a paper, please let me know).
make-grid n => grid-instance
Instantiate a grid of order n
. The cells are filled with 0 by default.
Create a pos structure, indexing the grid
in rows i and columns j,
counting from 0.
Return cell value in grid at pos. Cells are also setf
able.
Return the order n of the grid.
Return the list of available distinct symbols for the grid problem.
Return the list of positions pos
corresponding to the row peers of the
cell at pos.
Idem for columns and boxes respectively.
Idem for all distinct peers across all units.
Instantiate a grid.
(defparameter *grid3* (make-grid 3))
Accessing the cell value at row 2, column 4.
(setf (cell *grid3* (pos 2 4)) 5)
(cell *grid3* (pos 2 4)) ; => 5
Reading the order of the grid instance.
(order *grid3*) ; => 3
Reading the available symbols for the problem.
(vals *grid3*) ; => (1 2 3 4 5 6 7 8 9)
Lists of peers.
(peers-row *grid3* (pos 2 4))
;; =>
;; (#S(POS :ROW 2 :COL 8) #S(POS :ROW 2 :COL 7)
;; #S(POS :ROW 2 :COL 6) #S(POS :ROW 2 :COL 5)
;; #S(POS :ROW 2 :COL 3) #S(POS :ROW 2 :COL 2)
;; #S(POS :ROW 2 :COL 1) #S(POS :ROW 2 :COL 0))
(peers-col *grid3* (pos 2 4))
;; =>
;; (#S(POS :ROW 8 :COL 4) #S(POS :ROW 7 :COL 4)
;; #S(POS :ROW 6 :COL 4) #S(POS :ROW 5 :COL 4)
;; #S(POS :ROW 4 :COL 4) #S(POS :ROW 3 :COL 4)
;; #S(POS :ROW 1 :COL 4) #S(POS :ROW 0 :COL 4))
(peers-box *grid3* (pos 2 4))
;; =>
;; (#S(POS :ROW 2 :COL 5) #S(POS :ROW 2 :COL 3)
;; #S(POS :ROW 1 :COL 5) #S(POS :ROW 1 :COL 4)
;; #S(POS :ROW 1 :COL 3) #S(POS :ROW 0 :COL 5)
;; #S(POS :ROW 0 :COL 4) #S(POS :ROW 0 :COL 3))
(peers *grid3* (pos 2 4))
;; =>
;; (#S(POS :ROW 0 :COL 3) #S(POS :ROW 0 :COL 4)
;; #S(POS :ROW 0 :COL 5) #S(POS :ROW 1 :COL 3)
;; #S(POS :ROW 1 :COL 4) #S(POS :ROW 1 :COL 5)
;; #S(POS :ROW 8 :COL 4) #S(POS :ROW 7 :COL 4)
;; #S(POS :ROW 6 :COL 4) #S(POS :ROW 5 :COL 4)
;; #S(POS :ROW 4 :COL 4) #S(POS :ROW 3 :COL 4)
;; #S(POS :ROW 2 :COL 8) #S(POS :ROW 2 :COL 7)
;; #S(POS :ROW 2 :COL 6) #S(POS :ROW 2 :COL 5)
;; #S(POS :ROW 2 :COL 3) #S(POS :ROW 2 :COL 2)
;; #S(POS :ROW 2 :COL 1) #S(POS :ROW 2 :COL 0))
Launch tests with:
(asdf:test-system "sdku-rep")
sdku-rep
: none.sdku-rep/test
: