Sudoku: Difference between revisions

Content added Content deleted
(sudoku solver in common lisp)
m (minor style/formatting)
Line 104: Line 104:
(let* ((rmin (* 3 (floor row 3))) (rmax (+ rmin 3))
(let* ((rmin (* 3 (floor row 3))) (rmax (+ rmin 3))
(cmin (* 3 (floor column 3))) (cmax (+ cmin 3)))
(cmin (* 3 (floor column 3))) (cmax (+ cmin 3)))
(do ((r rmin (1+ r))) ((eql r rmax) neighbors)
(do ((r rmin (1+ r))) ((= r rmax) neighbors)
(do ((c cmin (1+ c))) ((eql c cmax))
(do ((c cmin (1+ c))) ((= c cmax))
(let ((x (aref grid r c)))
(let ((x (aref grid r c)))
(unless (or (eq x '_) (= r row) (= c column))
(unless (or (eq x '_) (= r row) (= c column))
Line 119: Line 119:
(defun solve (grid &optional (row 0) (column 0))
(defun solve (grid &optional (row 0) (column 0))
(cond
(cond
((eql row 9)
((= row 9)
grid)
grid)
((eql column 9)
((= column 9)
(solve grid (1+ row) 0))
(solve grid (1+ row) 0))
((numberp (aref grid row column))
((not (eq '_ (aref grid row column)))
(solve grid row (1+ column)))
(solve grid row (1+ column)))
(t (dolist (choice (choices row column grid) (setf (aref grid row column) '_))
(t (dolist (choice (choices row column grid) (setf (aref grid row column) '_))