forked from jeapostrophe/exp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl4-pr148.rkt
156 lines (139 loc) · 3.7 KB
/
l4-pr148.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#lang racket
;; I don't understand why this doesn't work. Maybe because picosat
;; isn't guaranteeing the minimal solution?
(define var 1)
(define (amb-bool)
(begin0 var
(set! var (add1 var))))
(define (list-ref* l . is)
(if (empty? is)
l
(apply list-ref* (list-ref l (first is)) (rest is))))
(define solve-box
;; Each plane (from front to back)
(list
;; Plane 1 (front)
(list
;; Row then column
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool)))
;; Plane 2 (middle)
(list
;; Row then column
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool)))
;; Plane 3 (back)
(list
;; Row then column
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool))
(list (amb-bool) (amb-bool) (amb-bool)))))
(define check-box
;; Each plane (from front to back)
(list
;; Plane 1 (front)
(list
;; Row then column
(list #f #f #f)
(list #f #t #f)
(list #f #f #f))
;; Plane 2 (middle)
(list
;; Row then column
(list #f #t #f)
(list #f #t #f)
(list #t #t #f))
;; Plane 3 (back)
(list
;; Row then column
(list #f #f #f)
(list #f #f #f)
(list #f #f #f))))
(define box solve-box)
(define clauses empty)
(define (clause! c)
(set! clauses (cons c clauses)))
(define check-equal?
(match-lambda*
[(list (list (? boolean? val) ...) (? boolean? should-be))
(unless (equal? (ormap (λ (x) x) val) should-be)
(error 'box "Not correct"))]
[(list (list (? number? var) ...) #t)
(clause! var)]
[(list (list (? number? var) ...) #f)
(clause! (map (curry cons 'not) var))]
[(list (? list? lhs) (? list? rhs))
(for-each check-equal? lhs rhs)]))
(check-equal?
;; Top view
(for/list ([plane (in-range 3)])
(for/list ([col (in-range 3)])
(for/list ([row (in-range 3)])
(list-ref* box plane row col))))
(list (list #f #t #f)
(list #t #t #f)
(list #f #f #f)))
(check-equal?
;; Side view
(for/list ([row (in-range 3)])
(for/list ([plane (in-range 3)])
(for/list ([col (in-range 3)])
(list-ref* box plane row col))))
(list (list #f #t #f)
(list #t #t #f)
(list #f #t #f)))
(check-equal?
;; Front view
(for/list ([row (in-range 3)])
(for/list ([col (in-range 3)])
(for/list ([plane (in-range 3)])
(list-ref* box plane row col))))
(list (list #f #t #f)
(list #f #t #f)
(list #t #t #f)))
(module+ main
(define-values (sp stdout stdin stderr)
(subprocess #f #f #f "/usr/bin/picosat"))
(fprintf stdin "c p148\n")
(fprintf stdin "p cnf ~a ~a\n" (sub1 var) (length clauses))
(for ([c (in-list clauses)])
(for ([v (in-list c)])
(match v
[(? number? n)
(fprintf stdin "~a " n)]
[(cons 'not (? number? n))
(fprintf stdin "-~a " n)]))
(fprintf stdin "0\n"))
(close-output-port stdin)
(subprocess-wait sp)
(define var->val (make-hasheq))
(let loop ([v (read stdout)])
(match v
['s
(match (read stdout)
['SATISFIABLE
(loop (read stdout))])]
['v
(loop (read stdout))]
[(? number? n)
(hash-set! var->val (abs n)
(not (negative? n)))
(loop (read stdout))]
[(? eof-object?)
(void)]))
(define fill-in
(match-lambda
[(? list? l)
(begin0
(for/sum ([e (in-list l)])
(fill-in e))
(newline))]
[(? number? n)
(fill-in (hash-ref var->val n))]
[(? boolean? b)
(printf "~a " b)
(if b 1 0)]))
(printf "Total: ~a\n"
(fill-in box)))