Added Rich Lewis's Sudoku solver and problems.
This commit is contained in:
parent
4143501988
commit
f378c48daf
|
@ -0,0 +1,619 @@
|
|||
;;; Rich Lewis
|
||||
;;; Sudoku Puzzle Solver Version 2.0
|
||||
|
||||
;;; Hopefully someday, I'll work out a better user interface, but it
|
||||
;;; serves its purpose for now. Feel free to pass it around to anyone
|
||||
;;; who is interested in sudoku puzzles and wants a solver written in
|
||||
;;; scheme.
|
||||
;;;
|
||||
;;; Rich Lewis
|
||||
|
||||
;(print-vector-length #f)
|
||||
(define make-table
|
||||
(lambda (nr nc init)
|
||||
(let ([tbl (make-vector nr)])
|
||||
(let insert-rows! ([i 0])
|
||||
(unless (= i nr)
|
||||
(vector-set! tbl i (make-vector nc init))
|
||||
(insert-rows! (+ i 1))))
|
||||
tbl)))
|
||||
|
||||
(define table-set!
|
||||
(lambda (tbl ri ci x)
|
||||
(vector-set! (vector-ref tbl ri) ci x)))
|
||||
|
||||
(define table-ref
|
||||
(lambda (tbl ri ci)
|
||||
(vector-ref (vector-ref tbl ri) ci)))
|
||||
|
||||
(define make-grid
|
||||
(lambda ()
|
||||
(let ([grid (make-table 9 9 #f)])
|
||||
(let row-loop ([row 0])
|
||||
(if (= row 9)
|
||||
grid
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1))
|
||||
(begin
|
||||
(table-set! grid row col (make-vector 9 1))
|
||||
(col-loop (+ col 1))))))))))
|
||||
|
||||
(define grid (make-grid))
|
||||
|
||||
(define grid-ref
|
||||
(lambda (x y)
|
||||
(table-ref grid y x)))
|
||||
|
||||
(define grid-set!
|
||||
(lambda (x y val)
|
||||
(table-set! grid y x val)))
|
||||
|
||||
(define reset-row
|
||||
(lambda (row)
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(grid-set! row i (make-vector 9 1))
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define reset-grid
|
||||
(lambda ()
|
||||
(let row-loop ([row 0])
|
||||
(unless (= row 9)
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1))
|
||||
(begin
|
||||
(grid-set! row col (make-vector 9 1))
|
||||
(col-loop (+ col 1)))))))))
|
||||
|
||||
(define input-grid
|
||||
(lambda ()
|
||||
(printf " Sudoku Puzzle Solver~%~%")
|
||||
(printf "Starting in the top left-hand corner, input either a 0 for a~%")
|
||||
(printf "blank space or a number between 1 and 9. Then press enter and~%")
|
||||
(printf "repeat across the first row. Return to the beginning of the~%")
|
||||
(printf "next row and input each row until the end. If a number is~%")
|
||||
(printf "mis-entered, inputing \"s\" will return to the beginning. \"r\" will~%")
|
||||
(printf "restart the current row and \"q\" will quit the program altogether.~%~%")
|
||||
(printf "When the last cell (9,9) is entered, the puzzle will automatically~%")
|
||||
(printf "be solved and the result printed in a simple grid.~%~%")
|
||||
(let row-loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(let col-loop ([j 0])
|
||||
(if (= j 9)
|
||||
(begin
|
||||
(newline)
|
||||
(row-loop (+ i 1)))
|
||||
(begin
|
||||
(printf "cell ~a, ~a: " (+ i 1) (+ j 1))
|
||||
(let ([c (read)])
|
||||
(cond
|
||||
[(and (number? c) (> c 0) (< c 10)) (begin
|
||||
(grid-set! i j c)
|
||||
(col-loop (+ j 1)))]
|
||||
[(equal? c '0) (begin
|
||||
(grid-set! i j (make-vector 9 1))
|
||||
(col-loop (+ j 1)))]
|
||||
[(equal? c 'q) (row-loop 9)]
|
||||
[(equal? c 'r) (begin
|
||||
(reset-row i)
|
||||
(col-loop 0))]
|
||||
[(equal? c 's) (begin
|
||||
(reset-grid)
|
||||
(row-loop 0))]
|
||||
[else (begin
|
||||
(printf "invalid input ~a~%" c)
|
||||
(col-loop j))])))))))))
|
||||
|
||||
(define input-block
|
||||
(lambda (block)
|
||||
(unless (= (length block) 10)
|
||||
(error 'input-block "invalid block"))
|
||||
(let f ([i 0] [ls (cdr block)])
|
||||
(unless (= i 9)
|
||||
(let ([str (car ls)])
|
||||
(unless (and (string? str) (= (string-length str) 9))
|
||||
(error 'input-block "invalid string ~s" str))
|
||||
(for-each
|
||||
(lambda (c j)
|
||||
(grid-set! i j
|
||||
(case c
|
||||
[(#\0) (make-vector 9 1)]
|
||||
[(#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
|
||||
(- (char->integer c) (char->integer #\0))]
|
||||
[else (error 'input-block "invalid char ~s" c)])))
|
||||
(string->list str)
|
||||
'(0 1 2 3 4 5 6 7 8)))
|
||||
(f (add1 i) (cdr ls))))))
|
||||
|
||||
(define print-sudoku
|
||||
(lambda ()
|
||||
(printf " column ~%")
|
||||
(printf " 1 2 3 4 5 6 7 8 9~%")
|
||||
(newline)
|
||||
(let row-loop ([row 0])
|
||||
(unless (= row 9)
|
||||
(printf "~a " (+ row 1))
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(begin
|
||||
(newline)
|
||||
(row-loop (+ row 1)))
|
||||
(begin
|
||||
(if (vector? (grid-ref row col))
|
||||
(printf " ")
|
||||
(printf "~a " (grid-ref row col)))
|
||||
(col-loop (+ col 1)))))))))
|
||||
|
||||
(define sub-grid-list
|
||||
(lambda (sub)
|
||||
(case sub
|
||||
[(0) '(0 2 0 2)]
|
||||
[(1) '(0 2 3 5)]
|
||||
[(2) '(0 2 6 8)]
|
||||
[(3) '(3 5 0 2)]
|
||||
[(4) '(3 5 3 5)]
|
||||
[(5) '(3 5 6 8)]
|
||||
[(6) '(6 8 0 2)]
|
||||
[(7) '(6 8 3 5)]
|
||||
[(8) '(6 8 6 8)])))
|
||||
|
||||
(define sub-grid-list2
|
||||
(lambda (row col)
|
||||
(case row
|
||||
[(0 1 2)
|
||||
(case col
|
||||
[(0 1 2) 0]
|
||||
[(3 4 5) 1]
|
||||
[(6 7 8) 2])]
|
||||
[(3 4 5)
|
||||
(case col
|
||||
[(0 1 2) 3]
|
||||
[(3 4 5) 4]
|
||||
[(6 7 8) 5])]
|
||||
[(6 7 8)
|
||||
(case col
|
||||
[(0 1 2) 6]
|
||||
[(3 4 5) 7]
|
||||
[(6 7 8) 8])])))
|
||||
|
||||
(define row->vector
|
||||
(lambda (row)
|
||||
(let ([vec (make-vector 9 #f)])
|
||||
(let loop ([i 0])
|
||||
(if (= i 9)
|
||||
vec
|
||||
(begin
|
||||
(vector-set! vec i (grid-ref row i))
|
||||
(loop (+ i 1))))))))
|
||||
|
||||
(define row-init
|
||||
(lambda (row)
|
||||
(vector->row (vector-init (row->vector row)) row)))
|
||||
|
||||
(define row-init-all
|
||||
(lambda ()
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(row-init i)
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define col->vector
|
||||
(lambda (col)
|
||||
(let ([vec (make-vector 9 #f)])
|
||||
(let loop ([i 0])
|
||||
(if (= i 9)
|
||||
vec
|
||||
(begin
|
||||
(vector-set! vec i (grid-ref i col))
|
||||
(loop (+ i 1))))))))
|
||||
|
||||
(define col-init
|
||||
(lambda (col)
|
||||
(vector->col (vector-init (col->vector col)) col)))
|
||||
|
||||
(define col-init-all
|
||||
(lambda ()
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(col-init i)
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define sub-grid->vector
|
||||
(lambda (sub-grid)
|
||||
(let ([sub (sub-grid-list sub-grid)])
|
||||
(let ([vec (make-vector 9 #f)] [row1 (car sub)] [row2 (cadr sub)]
|
||||
[col1 (caddr sub)] [col2 (cadddr sub)])
|
||||
(let loop1 ([i row1] [k 0])
|
||||
(if (= k 9)
|
||||
vec
|
||||
(let loop2 ([j col1] [k k])
|
||||
(if (= j (+ col2 1))
|
||||
(loop1 (+ i 1) k)
|
||||
(begin
|
||||
(vector-set! vec k (grid-ref i j))
|
||||
(loop2 (+ j 1) (+ k 1)))))))))))
|
||||
|
||||
(define sub-grid-init
|
||||
(lambda (sub)
|
||||
(vector->sub-grid (vector-init (sub-grid->vector sub)) sub)))
|
||||
|
||||
(define sub-grid-init-all
|
||||
(lambda ()
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(sub-grid-init i)
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define vector->row
|
||||
(lambda (vec row)
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(grid-set! row i (vector-ref vec i))
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define vector->col
|
||||
(lambda (vec col)
|
||||
(let loop ([i 0])
|
||||
(unless (= i 9)
|
||||
(grid-set! i col (vector-ref vec i))
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define vector->sub-grid
|
||||
(lambda (vec sub-grid)
|
||||
(let ([sub (sub-grid-list sub-grid)])
|
||||
(let ([row1 (car sub)] [row2 (cadr sub)]
|
||||
[col1 (caddr sub)] [col2 (cadddr sub)])
|
||||
(let loop1 ([i row1] [k 0])
|
||||
(unless (= k 9)
|
||||
(let loop2 ([j col1] [k k])
|
||||
(if (= j (+ col2 1))
|
||||
(loop1 (+ i 1) k)
|
||||
(begin
|
||||
(grid-set! i j (vector-ref vec k))
|
||||
(loop2 (+ j 1) (+ k 1)))))))))))
|
||||
|
||||
(define vector-init
|
||||
(lambda (vec)
|
||||
(let main-loop ([i 0])
|
||||
(if (= i 9)
|
||||
vec
|
||||
(let ([x (vector-ref vec i)])
|
||||
(if (vector? x)
|
||||
(main-loop (+ i 1))
|
||||
(let sub-loop ([j 0])
|
||||
(if (= j 9)
|
||||
(main-loop (+ i 1))
|
||||
(let ([y (vector-ref vec j)])
|
||||
(if (vector? y)
|
||||
(begin
|
||||
(vector-set! y (- x 1) 0)
|
||||
(sub-loop (+ j 1)))
|
||||
(sub-loop (+ j 1))))))))))))
|
||||
|
||||
(define grid-init
|
||||
(lambda ()
|
||||
(row-init-all)
|
||||
(col-init-all)
|
||||
(sub-grid-init-all)))
|
||||
|
||||
(define one-possible?
|
||||
(lambda (vec)
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
(= count 1)
|
||||
(if (= (vector-ref vec i) 1)
|
||||
(loop (+ i 1) (+ count 1))
|
||||
(loop (+ i 1) count))))))
|
||||
|
||||
(define replace-one
|
||||
(lambda (vec)
|
||||
(let loop ([i 0])
|
||||
(if (= (vector-ref vec i) 1)
|
||||
i
|
||||
(loop (+ i 1))))))
|
||||
|
||||
(define level-one-row
|
||||
(lambda (row)
|
||||
(let loop ([i 0] [count 0] [vec (row->vector row)])
|
||||
(if (= i 9)
|
||||
count
|
||||
(let ([x (vector-ref vec i)])
|
||||
(if (vector? x)
|
||||
(if (one-possible? x)
|
||||
(begin
|
||||
(grid-set! row i (+ (replace-one x) 1))
|
||||
(row-init row)
|
||||
(col-init i)
|
||||
(sub-grid-init (sub-grid-list2 row i))
|
||||
(loop (+ i 1) (+ count 1) (row->vector row)))
|
||||
(loop (+ i 1) count vec))
|
||||
(loop (+ i 1) count vec)))))))
|
||||
|
||||
(define level-one-pass
|
||||
(lambda ()
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
count
|
||||
(loop (+ i 1) (+ count (level-one-row i)))))))
|
||||
|
||||
(define level-one
|
||||
(lambda ()
|
||||
(let loop ()
|
||||
(unless (= (level-one-pass) 0)
|
||||
(loop)))))
|
||||
|
||||
(define level-two-row
|
||||
(lambda (row)
|
||||
(let loop1 ([i 0] [count1 0] [vec (row->vector row)])
|
||||
(if (= i 9)
|
||||
count1
|
||||
(let loop2 ([j 0] [count2 0] [col 0])
|
||||
(if (= j 9)
|
||||
(if (= count2 1)
|
||||
(begin
|
||||
(grid-set! row col (+ i 1))
|
||||
(row-init row)
|
||||
(col-init col)
|
||||
(sub-grid-init (sub-grid-list2 row col))
|
||||
(loop1 (+ i 1) (+ count1 1) (row->vector row)))
|
||||
(loop1 (+ i 1) count1 vec))
|
||||
(begin
|
||||
(let ([x (vector-ref vec j)])
|
||||
(if (vector? x)
|
||||
(if (= (vector-ref x i) 1)
|
||||
(loop2 (+ j 1) (+ count2 1) j)
|
||||
(loop2 (+ j 1) count2 col))
|
||||
(loop2 (+ j 1) count2 col))))))))))
|
||||
|
||||
(define level-two-row-pass
|
||||
(lambda ()
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
count
|
||||
(loop (+ i 1) (+ count (level-two-row i)))))))
|
||||
|
||||
(define level-two-col
|
||||
(lambda (col)
|
||||
(let loop1 ([i 0] [count1 0] [vec (col->vector col)])
|
||||
(if (= i 9)
|
||||
count1
|
||||
(let loop2 ([j 0] [count2 0] [row 0])
|
||||
(if (= j 9)
|
||||
(if (= count2 1)
|
||||
(begin
|
||||
(grid-set! row col (+ i 1))
|
||||
(row-init row)
|
||||
(col-init col)
|
||||
(sub-grid-init (sub-grid-list2 row col))
|
||||
(loop1 (+ i 1) (+ count1 1) (col->vector col)))
|
||||
(loop1 (+ i 1) count1 vec))
|
||||
(begin
|
||||
(let ([x (vector-ref vec j)])
|
||||
(if (vector? x)
|
||||
(if (= (vector-ref x i) 1)
|
||||
(loop2 (+ j 1) (+ count2 1) j)
|
||||
(loop2 (+ j 1) count2 row))
|
||||
(loop2 (+ j 1) count2 row))))))))))
|
||||
|
||||
(define level-two-col-pass
|
||||
(lambda ()
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
count
|
||||
(loop (+ i 1) (+ count (level-two-col i)))))))
|
||||
|
||||
(define level-two-sub
|
||||
(lambda (sub)
|
||||
(let loop1 ([i 0] [count1 0] [vec (sub-grid->vector sub)])
|
||||
(if (= i 9)
|
||||
count1
|
||||
(let loop2 ([j 0] [count2 0] [loc 0])
|
||||
(if (= j 9)
|
||||
(if (= count2 1)
|
||||
(begin
|
||||
(let ([row (+ (car (sub-grid-list sub)) (quotient loc 3))]
|
||||
[col (+ (caddr (sub-grid-list sub)) (modulo loc 3))])
|
||||
(grid-set! row col (+ i 1))
|
||||
(row-init row)
|
||||
(col-init col)
|
||||
(sub-grid-init sub))
|
||||
(loop1 (+ i 1) (+ count1 1) (sub-grid->vector sub)))
|
||||
(loop1 (+ i 1) count1 vec))
|
||||
(begin
|
||||
(let ([x (vector-ref vec j)])
|
||||
(if (vector? x)
|
||||
(if (= (vector-ref x i) 1)
|
||||
(loop2 (+ j 1) (+ count2 1) j)
|
||||
(loop2 (+ j 1) count2 loc))
|
||||
(loop2 (+ j 1) count2 loc))))))))))
|
||||
|
||||
(define level-two-sub-pass
|
||||
(lambda ()
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
count
|
||||
(loop (+ i 1) (+ count (level-two-sub i)))))))
|
||||
|
||||
(define vector-count
|
||||
(lambda (vec)
|
||||
(let loop ([i 0] [count 0])
|
||||
(if (= i 9)
|
||||
count
|
||||
(if (= (vector-ref vec i) 1)
|
||||
(loop (+ i 1) (+ count 1))
|
||||
(loop (+ i 1) count))))))
|
||||
|
||||
(define replace-2
|
||||
(lambda (vec loc1 loc2)
|
||||
(let loop1 ([i 0] [count1 0])
|
||||
(if (= i 9)
|
||||
(list count1 vec)
|
||||
(let ([x (vector-ref vec i)])
|
||||
(if (and (vector? x) (not (= loc1 i)) (not (= loc2 i)))
|
||||
(let loop2 ([j 0] [count2 count1])
|
||||
(if (= j 9)
|
||||
(loop1 (+ i 1) count2)
|
||||
(let ([y (vector-ref vec loc1)])
|
||||
(if (and (= (vector-ref y j) 1) (= (vector-ref x j) 1))
|
||||
(begin
|
||||
(vector-set! (vector-ref vec i) j 0)
|
||||
(loop2 (+ j 1) (+ count2 1)))
|
||||
(loop2 (+ j 1) count2)))))
|
||||
(loop1 (+ i 1) count1)))))))
|
||||
|
||||
(define level-three-col
|
||||
(lambda (col)
|
||||
(let loop1 ([i 0] [count1 0] [vec (col->vector col)])
|
||||
(if (= i 9)
|
||||
count1
|
||||
(let ([x1 (vector-ref vec i)])
|
||||
(if (vector? x1)
|
||||
(let loop2 ([j 0])
|
||||
(if (= j 9)
|
||||
(loop1 (+ i 1) count1 vec)
|
||||
(let ([x2 (vector-ref vec j)])
|
||||
(if (and (vector? x2) (not (= i j)) (equal? x1 x2))
|
||||
(let ([y1 (vector-count x1)])
|
||||
(case y1
|
||||
[(2) (begin
|
||||
(let ([z (replace-2 vec i j)])
|
||||
(if (= (car z) 0)
|
||||
(loop2 (+ j 1))
|
||||
(begin
|
||||
(vector->col (cadr z) col)
|
||||
(loop1 (+ i 1) (+ count1 (car z)) (col->vector col))))))]
|
||||
[(3) (loop2 (+ j 1))]
|
||||
[(else) (loop2 (+ j 1))]))
|
||||
(loop2 (+ j 1))))))
|
||||
(loop1 (+ i 1) count1 vec)))))))
|
||||
|
||||
|
||||
(define done?
|
||||
(lambda ()
|
||||
(let row-loop ([row 0] [count 0])
|
||||
(if (= row 9)
|
||||
(= count 0)
|
||||
(let col-loop ([col 0] [count count])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1) count)
|
||||
(let ([x (grid-ref row col)])
|
||||
(if (vector? x)
|
||||
(col-loop (+ col 1) (+ count 1))
|
||||
(col-loop (+ col 1) count)))))))))
|
||||
|
||||
(define invalid?
|
||||
(lambda ()
|
||||
(let row-loop ([row 0] [count 0])
|
||||
(if (= row 9)
|
||||
(> count 0)
|
||||
(let col-loop ([col 0] [count count])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1) count)
|
||||
(let ([x (grid-ref row col)])
|
||||
(if (and (vector? x) (= (vector-count x) 0))
|
||||
(col-loop (+ col 1) (+ count 1))
|
||||
(col-loop (+ col 1) count)))))))))
|
||||
|
||||
(define copy-grid
|
||||
(lambda ()
|
||||
(let ([grid-orig (make-grid)])
|
||||
(let row-loop ([row 0])
|
||||
(if (= row 9)
|
||||
grid-orig
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1))
|
||||
(let ([x1 (table-ref grid row col)] [y1 (table-ref grid-orig col row)])
|
||||
(if (not (vector? x1))
|
||||
(begin
|
||||
(table-set! grid-orig col row x1)
|
||||
(col-loop (+ col 1)))
|
||||
(let vector-loop ([loc 0])
|
||||
(if (= loc 9)
|
||||
(col-loop (+ col 1))
|
||||
(let ([x2 (vector-ref x1 loc)] [y2 (vector-ref y1 loc)])
|
||||
(vector-set! y1 loc x2)
|
||||
(vector-loop (+ loc 1))))))))))))))
|
||||
|
||||
(define restore-grid
|
||||
(lambda (grid-orig)
|
||||
(reset-grid)
|
||||
(let row-loop ([row 0])
|
||||
(unless (= row 9)
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1))
|
||||
(let ([x1 (table-ref grid row col)] [y1 (table-ref grid-orig row col)])
|
||||
(if (not (vector? y1))
|
||||
(begin
|
||||
(grid-set! row col y1)
|
||||
(col-loop (+ col 1)))
|
||||
(let vector-loop ([loc 0])
|
||||
(if (= loc 9)
|
||||
(col-loop (+ col 1))
|
||||
(let ([y2 (vector-ref y1 loc)])
|
||||
(vector-set! (grid-ref row col) loc y2)
|
||||
(vector-loop (+ loc 1)))))))))))))
|
||||
|
||||
|
||||
(define level-four
|
||||
(lambda ()
|
||||
(let row-loop ([row 0])
|
||||
(unless (= row 9)
|
||||
(let col-loop ([col 0])
|
||||
(if (= col 9)
|
||||
(row-loop (+ row 1))
|
||||
(let ([x (grid-ref row col)])
|
||||
(if (and (vector? x) (= (vector-count x) 2))
|
||||
(let vector-loop ([loc 0])
|
||||
(if (= loc 9)
|
||||
(col-loop (+ col 1))
|
||||
(let ([x2 (grid-ref row col)])
|
||||
(if (= (vector-ref x2 loc) 1)
|
||||
(let ([grid-orig (copy-grid)])
|
||||
(vector-set! (grid-ref row col) loc 0)
|
||||
(if (sudoku-help grid)
|
||||
#t
|
||||
(begin
|
||||
(restore-grid grid-orig)
|
||||
(vector-loop (+ loc 1)))))
|
||||
(vector-loop (+ loc 1))))))
|
||||
(col-loop (+ col 1))))))))))
|
||||
|
||||
(define sudoku-help
|
||||
(lambda (grid)
|
||||
(grid-init)
|
||||
(let loop ()
|
||||
(cond
|
||||
[(and (number? (level-one-pass)) (> (level-one-pass) 0)) (loop)]
|
||||
[(invalid?) #f]
|
||||
[(and (number? (level-two-row-pass)) (> (level-two-row-pass) 0)) (loop)]
|
||||
[(and (number? (level-two-col-pass)) (> (level-two-col-pass) 0)) (loop)]
|
||||
[(and (number? (level-two-sub-pass)) (> (level-two-sub-pass) 0)) (loop)]
|
||||
[(done?) #t]
|
||||
[else (level-four)]))))
|
||||
|
||||
(define sudoku
|
||||
(lambda (block)
|
||||
(define grid (make-grid))
|
||||
(input-block block)
|
||||
(if (sudoku-help grid)
|
||||
(print-sudoku)
|
||||
(error 'sudoku "MIS-ENTERED INITIALIZATION"))))
|
||||
|
||||
(define quotient fxquotient)
|
||||
(define modulo fxmodulo)
|
||||
(define do-file
|
||||
(lambda ()
|
||||
(let f ()
|
||||
(let ([x (read)])
|
||||
(unless (eof-object? x)
|
||||
(sudoku x)
|
||||
(f))))))
|
||||
(with-input-from-file "sudoku.txt" do-file)
|
||||
(with-input-from-file "sudoku-hard.txt" do-file)
|
||||
|
||||
(exit)
|
||||
;;; vim:syntax=scheme
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,500 @@
|
|||
("Grid 01"
|
||||
"003020600"
|
||||
"900305001"
|
||||
"001806400"
|
||||
"008102900"
|
||||
"700000008"
|
||||
"006708200"
|
||||
"002609500"
|
||||
"800203009"
|
||||
"005010300")
|
||||
("Grid 02"
|
||||
"200080300"
|
||||
"060070084"
|
||||
"030500209"
|
||||
"000105408"
|
||||
"000000000"
|
||||
"402706000"
|
||||
"301007040"
|
||||
"720040060"
|
||||
"004010003")
|
||||
("Grid 03"
|
||||
"000000907"
|
||||
"000420180"
|
||||
"000705026"
|
||||
"100904000"
|
||||
"050000040"
|
||||
"000507009"
|
||||
"920108000"
|
||||
"034059000"
|
||||
"507000000")
|
||||
("Grid 04"
|
||||
"030050040"
|
||||
"008010500"
|
||||
"460000012"
|
||||
"070502080"
|
||||
"000603000"
|
||||
"040109030"
|
||||
"250000098"
|
||||
"001020600"
|
||||
"080060020")
|
||||
("Grid 05"
|
||||
"020810740"
|
||||
"700003100"
|
||||
"090002805"
|
||||
"009040087"
|
||||
"400208003"
|
||||
"160030200"
|
||||
"302700060"
|
||||
"005600008"
|
||||
"076051090")
|
||||
("Grid 06"
|
||||
"100920000"
|
||||
"524010000"
|
||||
"000000070"
|
||||
"050008102"
|
||||
"000000000"
|
||||
"402700090"
|
||||
"060000000"
|
||||
"000030945"
|
||||
"000071006")
|
||||
("Grid 07"
|
||||
"043080250"
|
||||
"600000000"
|
||||
"000001094"
|
||||
"900004070"
|
||||
"000608000"
|
||||
"010200003"
|
||||
"820500000"
|
||||
"000000005"
|
||||
"034090710")
|
||||
("Grid 08"
|
||||
"480006902"
|
||||
"002008001"
|
||||
"900370060"
|
||||
"840010200"
|
||||
"003704100"
|
||||
"001060049"
|
||||
"020085007"
|
||||
"700900600"
|
||||
"609200018")
|
||||
("Grid 09"
|
||||
"000900002"
|
||||
"050123400"
|
||||
"030000160"
|
||||
"908000000"
|
||||
"070000090"
|
||||
"000000205"
|
||||
"091000050"
|
||||
"007439020"
|
||||
"400007000")
|
||||
("Grid 10"
|
||||
"001900003"
|
||||
"900700160"
|
||||
"030005007"
|
||||
"050000009"
|
||||
"004302600"
|
||||
"200000070"
|
||||
"600100030"
|
||||
"042007006"
|
||||
"500006800")
|
||||
("Grid 11"
|
||||
"000125400"
|
||||
"008400000"
|
||||
"420800000"
|
||||
"030000095"
|
||||
"060902010"
|
||||
"510000060"
|
||||
"000003049"
|
||||
"000007200"
|
||||
"001298000")
|
||||
("Grid 12"
|
||||
"062340750"
|
||||
"100005600"
|
||||
"570000040"
|
||||
"000094800"
|
||||
"400000006"
|
||||
"005830000"
|
||||
"030000091"
|
||||
"006400007"
|
||||
"059083260")
|
||||
("Grid 13"
|
||||
"300000000"
|
||||
"005009000"
|
||||
"200504000"
|
||||
"020000700"
|
||||
"160000058"
|
||||
"704310600"
|
||||
"000890100"
|
||||
"000067080"
|
||||
"000005437")
|
||||
("Grid 14"
|
||||
"630000000"
|
||||
"000500008"
|
||||
"005674000"
|
||||
"000020000"
|
||||
"003401020"
|
||||
"000000345"
|
||||
"000007004"
|
||||
"080300902"
|
||||
"947100080")
|
||||
("Grid 15"
|
||||
"000020040"
|
||||
"008035000"
|
||||
"000070602"
|
||||
"031046970"
|
||||
"200000000"
|
||||
"000501203"
|
||||
"049000730"
|
||||
"000000010"
|
||||
"800004000")
|
||||
("Grid 16"
|
||||
"361025900"
|
||||
"080960010"
|
||||
"400000057"
|
||||
"008000471"
|
||||
"000603000"
|
||||
"259000800"
|
||||
"740000005"
|
||||
"020018060"
|
||||
"005470329")
|
||||
("Grid 17"
|
||||
"050807020"
|
||||
"600010090"
|
||||
"702540006"
|
||||
"070020301"
|
||||
"504000908"
|
||||
"103080070"
|
||||
"900076205"
|
||||
"060090003"
|
||||
"080103040")
|
||||
("Grid 18"
|
||||
"080005000"
|
||||
"000003457"
|
||||
"000070809"
|
||||
"060400903"
|
||||
"007010500"
|
||||
"408007020"
|
||||
"901020000"
|
||||
"842300000"
|
||||
"000100080")
|
||||
("Grid 19"
|
||||
"003502900"
|
||||
"000040000"
|
||||
"106000305"
|
||||
"900251008"
|
||||
"070408030"
|
||||
"800763001"
|
||||
"308000104"
|
||||
"000020000"
|
||||
"005104800")
|
||||
("Grid 20"
|
||||
"000000000"
|
||||
"009805100"
|
||||
"051907420"
|
||||
"290401065"
|
||||
"000000000"
|
||||
"140508093"
|
||||
"026709580"
|
||||
"005103600"
|
||||
"000000000")
|
||||
("Grid 21"
|
||||
"020030090"
|
||||
"000907000"
|
||||
"900208005"
|
||||
"004806500"
|
||||
"607000208"
|
||||
"003102900"
|
||||
"800605007"
|
||||
"000309000"
|
||||
"030020050")
|
||||
("Grid 22"
|
||||
"005000006"
|
||||
"070009020"
|
||||
"000500107"
|
||||
"804150000"
|
||||
"000803000"
|
||||
"000092805"
|
||||
"907006000"
|
||||
"030400010"
|
||||
"200000600")
|
||||
("Grid 23"
|
||||
"040000050"
|
||||
"001943600"
|
||||
"009000300"
|
||||
"600050002"
|
||||
"103000506"
|
||||
"800020007"
|
||||
"005000200"
|
||||
"002436700"
|
||||
"030000040")
|
||||
("Grid 24"
|
||||
"004000000"
|
||||
"000030002"
|
||||
"390700080"
|
||||
"400009001"
|
||||
"209801307"
|
||||
"600200008"
|
||||
"010008053"
|
||||
"900040000"
|
||||
"000000800")
|
||||
("Grid 25"
|
||||
"360020089"
|
||||
"000361000"
|
||||
"000000000"
|
||||
"803000602"
|
||||
"400603007"
|
||||
"607000108"
|
||||
"000000000"
|
||||
"000418000"
|
||||
"970030014")
|
||||
("Grid 26"
|
||||
"500400060"
|
||||
"009000800"
|
||||
"640020000"
|
||||
"000001008"
|
||||
"208000501"
|
||||
"700500000"
|
||||
"000090084"
|
||||
"003000600"
|
||||
"060003002")
|
||||
("Grid 27"
|
||||
"007256400"
|
||||
"400000005"
|
||||
"010030060"
|
||||
"000508000"
|
||||
"008060200"
|
||||
"000107000"
|
||||
"030070090"
|
||||
"200000004"
|
||||
"006312700")
|
||||
("Grid 28"
|
||||
"000000000"
|
||||
"079050180"
|
||||
"800000007"
|
||||
"007306800"
|
||||
"450708096"
|
||||
"003502700"
|
||||
"700000005"
|
||||
"016030420"
|
||||
"000000000")
|
||||
("Grid 29"
|
||||
"030000080"
|
||||
"009000500"
|
||||
"007509200"
|
||||
"700105008"
|
||||
"020090030"
|
||||
"900402001"
|
||||
"004207100"
|
||||
"002000800"
|
||||
"070000090")
|
||||
("Grid 30"
|
||||
"200170603"
|
||||
"050000100"
|
||||
"000006079"
|
||||
"000040700"
|
||||
"000801000"
|
||||
"009050000"
|
||||
"310400000"
|
||||
"005000060"
|
||||
"906037002")
|
||||
("Grid 31"
|
||||
"000000080"
|
||||
"800701040"
|
||||
"040020030"
|
||||
"374000900"
|
||||
"000030000"
|
||||
"005000321"
|
||||
"010060050"
|
||||
"050802006"
|
||||
"080000000")
|
||||
("Grid 32"
|
||||
"000000085"
|
||||
"000210009"
|
||||
"960080100"
|
||||
"500800016"
|
||||
"000000000"
|
||||
"890006007"
|
||||
"009070052"
|
||||
"300054000"
|
||||
"480000000")
|
||||
("Grid 33"
|
||||
"608070502"
|
||||
"050608070"
|
||||
"002000300"
|
||||
"500090006"
|
||||
"040302050"
|
||||
"800050003"
|
||||
"005000200"
|
||||
"010704090"
|
||||
"409060701")
|
||||
("Grid 34"
|
||||
"050010040"
|
||||
"107000602"
|
||||
"000905000"
|
||||
"208030501"
|
||||
"040070020"
|
||||
"901080406"
|
||||
"000401000"
|
||||
"304000709"
|
||||
"020060010")
|
||||
("Grid 35"
|
||||
"053000790"
|
||||
"009753400"
|
||||
"100000002"
|
||||
"090080010"
|
||||
"000907000"
|
||||
"080030070"
|
||||
"500000003"
|
||||
"007641200"
|
||||
"061000940")
|
||||
("Grid 36"
|
||||
"006080300"
|
||||
"049070250"
|
||||
"000405000"
|
||||
"600317004"
|
||||
"007000800"
|
||||
"100826009"
|
||||
"000702000"
|
||||
"075040190"
|
||||
"003090600")
|
||||
("Grid 37"
|
||||
"005080700"
|
||||
"700204005"
|
||||
"320000084"
|
||||
"060105040"
|
||||
"008000500"
|
||||
"070803010"
|
||||
"450000091"
|
||||
"600508007"
|
||||
"003010600")
|
||||
("Grid 38"
|
||||
"000900800"
|
||||
"128006400"
|
||||
"070800060"
|
||||
"800430007"
|
||||
"500000009"
|
||||
"600079008"
|
||||
"090004010"
|
||||
"003600284"
|
||||
"001007000")
|
||||
("Grid 39"
|
||||
"000080000"
|
||||
"270000054"
|
||||
"095000810"
|
||||
"009806400"
|
||||
"020403060"
|
||||
"006905100"
|
||||
"017000620"
|
||||
"460000038"
|
||||
"000090000")
|
||||
("Grid 40"
|
||||
"000602000"
|
||||
"400050001"
|
||||
"085010620"
|
||||
"038206710"
|
||||
"000000000"
|
||||
"019407350"
|
||||
"026040530"
|
||||
"900020007"
|
||||
"000809000")
|
||||
("Grid 41"
|
||||
"000900002"
|
||||
"050123400"
|
||||
"030000160"
|
||||
"908000000"
|
||||
"070000090"
|
||||
"000000205"
|
||||
"091000050"
|
||||
"007439020"
|
||||
"400007000")
|
||||
("Grid 42"
|
||||
"380000000"
|
||||
"000400785"
|
||||
"009020300"
|
||||
"060090000"
|
||||
"800302009"
|
||||
"000040070"
|
||||
"001070500"
|
||||
"495006000"
|
||||
"000000092")
|
||||
("Grid 43"
|
||||
"000158000"
|
||||
"002060800"
|
||||
"030000040"
|
||||
"027030510"
|
||||
"000000000"
|
||||
"046080790"
|
||||
"050000080"
|
||||
"004070100"
|
||||
"000325000")
|
||||
("Grid 44"
|
||||
"010500200"
|
||||
"900001000"
|
||||
"002008030"
|
||||
"500030007"
|
||||
"008000500"
|
||||
"600080004"
|
||||
"040100700"
|
||||
"000700006"
|
||||
"003004050")
|
||||
("Grid 45"
|
||||
"080000040"
|
||||
"000469000"
|
||||
"400000007"
|
||||
"005904600"
|
||||
"070608030"
|
||||
"008502100"
|
||||
"900000005"
|
||||
"000781000"
|
||||
"060000010")
|
||||
("Grid 46"
|
||||
"904200007"
|
||||
"010000000"
|
||||
"000706500"
|
||||
"000800090"
|
||||
"020904060"
|
||||
"040002000"
|
||||
"001607000"
|
||||
"000000030"
|
||||
"300005702")
|
||||
("Grid 47"
|
||||
"000700800"
|
||||
"006000031"
|
||||
"040002000"
|
||||
"024070000"
|
||||
"010030080"
|
||||
"000060290"
|
||||
"000800070"
|
||||
"860000500"
|
||||
"002006000")
|
||||
("Grid 48"
|
||||
"001007090"
|
||||
"590080001"
|
||||
"030000080"
|
||||
"000005800"
|
||||
"050060020"
|
||||
"004100000"
|
||||
"080000030"
|
||||
"100020079"
|
||||
"020700400")
|
||||
("Grid 49"
|
||||
"000003017"
|
||||
"015009008"
|
||||
"060000000"
|
||||
"100007000"
|
||||
"009000200"
|
||||
"000500004"
|
||||
"000000020"
|
||||
"500600340"
|
||||
"340200000")
|
||||
("Grid 50"
|
||||
"300200000"
|
||||
"000107000"
|
||||
"706030500"
|
||||
"070009080"
|
||||
"900020004"
|
||||
"010800050"
|
||||
"009040301"
|
||||
"000702000"
|
||||
"000008006")
|
Loading…
Reference in New Issue