-
Notifications
You must be signed in to change notification settings - Fork 8
/
sudoku.rkt
115 lines (100 loc) · 3.23 KB
/
sudoku.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#lang racket
(require racket/system)
(define rows
(for/list ([i (in-range 1 10)])
(for/list ([j (in-range 1 10)])
(cons i j))))
(define cols
(for/list ([i (in-range 1 10)])
(for/list ([j (in-range 1 10)])
(cons j i))))
(define squares
(for*/list ([m (in-range 3)] [n (in-range 3)])
(for*/list ([i (in-range 3)] [j (in-range 3)])
(cons (+ i (* n 3) 1) (+ j (* m 3) 1)))))
(define (name ij)
(let ((i (car ij)) (j (cdr ij)))
(string->symbol (string-append "x" (string-append (number->string i) (number->string j))))))
(define (model-assoc m)
(map (lambda (x) (match x [(list _ lhs _ _ rhs) (cons lhs rhs)])) (cdr m)))
(define (model-get m ij) (cdr (assoc (name ij) m)))
(define (model->puzzle m)
(for/vector ([i (in-range 1 10)])
(for/vector ([j (in-range 1 10)])
(model-get m (cons i j)))))
(define (print-puzzle puzzle)
(printf "~a\n" "'#(")
(for ([line puzzle])
(printf " ~a\n" line))
(printf " ~a\n" ")"))
(define (puzzle-index puzzle i j)
(vector-ref (vector-ref puzzle (- i 1)) (- j 1)))
(define puzzle
'#(
#(4 0 0 0 0 0 8 0 5)
#(0 3 0 0 0 0 0 0 0)
#(0 0 0 7 0 0 0 0 0)
#(0 2 0 0 0 0 0 6 0)
#(0 0 0 0 8 0 4 0 0)
#(0 0 0 0 1 0 0 0 0)
#(0 0 0 6 0 3 0 7 0)
#(5 0 0 2 0 0 0 0 0)
#(1 0 4 0 0 0 0 0 0)
))
(define (say msg)
(printf "~a\n" msg))
(define (board-constraints say)
(for ([i (in-range 1 10)])
(for ([j (in-range 1 10)])
(let ((x (name (cons i j))))
(say `(declare-const ,x Int))
(say `(assert (<= 1 ,x)))
(say `(assert (<= ,x 9))))))
(for ([c (list rows cols squares)])
(for ([r c])
(say `(assert (distinct . ,(map name r)))))))
(define (puzzle-constraints puzzle say)
(for ([i (in-range 1 10)])
(for ([j (in-range 1 10)])
(let ((r (puzzle-index puzzle i j)))
(when (not (= r 0))
(let ((x (name (cons i j))))
(say `(assert (= ,x ,r)))))))))
(define (uniqueness-constraints puzzle solution say)
(say
`(assert (or .
,(for*/list ([i (in-range 1 10)] [j (in-range 1 10)]
#:when (= 0 (puzzle-index puzzle i j)))
(let ((x (name (cons i j))))
`(not (= ,x ,(puzzle-index solution i j)))))))))
(define (solve-sudoku puzzle)
(match (process "z3 -in")
[(list p-in p-out _ p-err p-fun)
(define (say msg)
;(printf "~a\n" msg)
(fprintf p-out "~a\n" msg))
(define (check-sat)
(say '(check-sat))
(flush-output p-out)
(let ((r (read p-in)))
(println r)
(eq? r 'sat)))
(define (get-model)
(say '(get-model))
(flush-output p-out)
(model-assoc (read p-in)))
(board-constraints say)
(puzzle-constraints puzzle say)
(if (check-sat)
(let ((solution (model->puzzle (get-model))))
(print-puzzle solution)
(uniqueness-constraints puzzle solution say)
(if (check-sat)
(printf "~a\n" ";; solution is not unique!")
(printf "~a\n" ";; solution is unique :)"))
)
(printf "~a\n" ";; no solution :("))
(close-input-port p-in)
(close-output-port p-out)
(close-input-port p-err)]))
(solve-sudoku puzzle)