/
meteor.racket
221 lines (196 loc) · 7.33 KB
/
meteor.racket
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#lang racket/base
;; The Computer Language Benchmarks Game
;; http://shootout.alioth.debian.org/
;;
;; Based on a Python version:
;; contributed by Olof Kraigher
;; modified by Tupteq
;; contributed by Matthew Flatt
;; optimized by Eli Barzilay
(require racket/cmdline)
(define width 5)
(define height 10)
(define size (* width height))
(define (valid-xy? x y)
(and (0 . <= . x)
(x . < . width)
(0 . <= . y)
(y . < . height)))
(define (mover fun)
(let ([t (make-vector size)])
(for ([p (in-range size)])
(vector-set! t p (let*-values ([(y x) (quotient/remainder p width)]
[(x y) (fun x y)])
(if (valid-xy? x y) (+ x (* y width)) -1))))
t))
(define E
(mover (lambda (x y) (values (add1 x) y))))
(define W
(mover (lambda (x y) (values (sub1 x) y))))
(define NE
(mover (lambda (x y) (values (+ x (bitwise-and y 1)) (sub1 y)))))
(define NW
(mover (lambda (x y) (values (sub1 (+ x (bitwise-and y 1))) (sub1 y)))))
(define SE
(mover (lambda (x y) (values (+ x (bitwise-and y 1)) (add1 y)))))
(define SW
(mover (lambda (x y) (values (sub1 (+ x (bitwise-and y 1))) (add1 y)))))
(define rotate-list (list E NE NW W SW SE E))
(define (rotate dir)
(cadr (memq dir rotate-list)))
(define flip-alist (list (cons E W) (cons NE NW) (cons NW NE)
(cons W E) (cons SW SE) (cons SE SW)))
(define (flip dir) (cdr (assq dir flip-alist)))
(define movers (list E W NE NW SE SW))
(define (valid? p)
(p . >= . 0))
(define (clear? board pos)
(not (bitwise-bit-set? board pos)))
(define (set board pos)
(bitwise-ior board (arithmetic-shift 1 pos)))
(define (zero-count board)
(for/fold ([count 0]) ([i (in-range size)])
(if (clear? board i) (add1 count) count)))
(define (find-free-cell board)
(for/or ([p (in-range 0 size)])
(and (clear? board p) p)))
(define (flood-fill board p)
(for/fold ([board (set board p)]) ([mover (in-list movers)])
(let ([p (vector-ref mover p)])
(if (and (valid? p) (clear? board p))
(flood-fill board p)
board))))
(define (no-islands? mask)
(let ([zeros (zero-count mask)])
(and (zeros . >= . 5)
(let loop ([mask mask] [zeros zeros])
(if (= mask #x3FFFFFFFFFFFF)
#t
(let* ([p (find-free-cell mask)]
[mask (flood-fill mask p)]
[new-zeros (zero-count mask)])
(and ((- zeros new-zeros) . >= . 5)
(loop mask new-zeros))))))))
(define (get-bitmask p piece)
(let ([mask (arithmetic-shift 1 p)])
(let loop ([p p] [cells piece] [mask mask])
(if (null? cells)
mask
(let ([p (vector-ref (car cells) p)])
(and (valid? p) (loop p (cdr cells) (set mask p))))))))
(define (all-bitmasks piece color)
(let ([pieces
(let-values ([(accum piece)
(for/fold ([accum null] [piece piece])
([orientations (in-range 2)])
(let-values ([(accum piece)
(for/fold ([accum accum] [piece piece])
([orientations (in-range (- 6 (* 3 (if (= color 4) 1 0))))])
(values (cons piece accum)
(map rotate piece)))])
(values accum (map flip piece))))])
accum)])
(reverse
(for*/fold ([accum null])
([piece (in-list pieces)]
[p (in-range 0 size)])
(let ([mask (get-bitmask p piece)])
(if (and mask (no-islands? mask)) (cons mask accum) accum))))))
(define generate-bitmasks-pieces
(list (list E E E SE)
(list SE SW W SW)
(list W W SW SE)
(list E E SW SE)
(list NW W NW SE SW)
(list E E NE W)
(list NW NE NE W)
(list NE SE E NE)
(list SE SE E SE)
(list E NW NW NW)))
(define (generate-bitmasks)
(let ([masks-at-cell
(list->vector
(for/list ([i (in-range size)])
(list->vector (for/list ([j (in-range 10)]) null))))])
(for ([piece (in-list generate-bitmasks-pieces)]
[color (in-naturals)])
(let loop ([masks (sort (all-bitmasks piece color) >)]
[cell-bit (sub1 size)]
[cell-counter (sub1 size)])
(if (null? masks)
masks-at-cell
(if (bitwise-bit-set? (car masks) cell-bit)
(let ([vec (vector-ref masks-at-cell cell-counter)])
(vector-set! vec color (cons (car masks) (vector-ref vec color)))
(loop (cdr masks) cell-bit cell-counter))
(loop masks (sub1 cell-bit) (sub1 cell-counter))))))
(for ([v (in-vector masks-at-cell)])
(for ([j (in-naturals)]
[val (in-vector v)])
(vector-set! v j (reverse val))))
masks-at-cell))
(define masks-at-cell (generate-bitmasks))
(define masks (make-vector 10 0))
(define to-go 0)
(define solutions (mcons #f #f)) ; keeps (min max) solutions
(define (solve-cell! cell board)
(when (and (positive? to-go) (not (negative? cell)))
;; Need solutions and not off board
(cond [(= board #x3FFFFFFFFFFFF)
;; Solved
(add-solutions!)]
[(not (clear? board cell))
;; Cell full, so try next
(solve-cell! (sub1 cell) board)]
[else
;; Recur
(for* ([color (in-range 10)]
#:when (zero? (vector-ref masks color))
[mask (in-list (vector-ref (vector-ref masks-at-cell cell)
color))]
#:when (zero? (bitwise-and mask board)))
(vector-set! masks color mask)
(solve-cell! (sub1 cell) (bitwise-ior board mask))
(vector-set! masks color 0))])))
(define (add-solutions!)
(define (add! solution)
(cond [(not (mcar solutions))
(set-mcar! solutions solution)
(set-mcdr! solutions solution)]
[(bytes<? solution (mcar solutions))
(set-mcar! solutions solution)]
[(bytes>? solution (mcdr solutions))
(set-mcdr! solutions solution)]))
(let* ([s (list->bytes
(for/list ([pos (in-range size)])
(for/or ([color (in-range 10)])
(and (not (clear? (vector-ref masks color) pos))
(+ color (char->integer #\0))))))]
[ns (make-bytes size)])
;; Inverse
(for* ([y (in-range height)]
[x (in-range width)])
(bytes-set! ns (+ x (* y width))
(bytes-ref s (+ (- width (+ x 1))
(* width (- height (+ y 1)))))))
;; Keep first and last only
(add! s)
(add! ns)
(set! to-go (- to-go 2))))
(define (print-solution solution)
(let ([solution (bytes->string/utf-8 solution)])
(for ([y (in-range height)])
(when (odd? y) (display " "))
(for ([x (in-range width)])
(printf "~a " (string-ref solution (+ x (* y width)))))
(printf "\n"))
(newline)))
(define (solve! n)
(set! to-go n)
(solve-cell! (sub1 size) 0))
(command-line #:args (n)
(let ([n (string->number n)])
(solve! n)
(printf "~a solutions found\n\n" (- n to-go))
(print-solution (mcar solutions))
(print-solution (mcdr solutions))))