Debian Codegraph @

benchmarks.larceny/src/maze.scm

raw
1 ;;; MAZE -- Constructs a maze on a hexagonal grid, written by Olin Shivers.
2
3 ;------------------------------------------------------------------------------
4 ; Was file "rand.scm".
5
6 ; Minimal Standard Random Number Generator
7 ; Park & Miller, CACM 31(10), Oct 1988, 32 bit integer version.
8 ; better constants, as proposed by Park.
9 ; By Ozan Yigit
10
11 ;;; Rehacked by Olin 4/1995.
12
13 (define (random-state n)
14 (cons n #f))
15
16 (define (rand state)
17 (let ((seed (car state))
18 (A 2813) ; 48271
19 (M 8388607) ; 2147483647
20 (Q 2787) ; 44488
21 (R 2699)) ; 3399
22 (let* ((hi (quotient seed Q))
23 (lo (modulo seed Q))
24 (test (- (* A lo) (* R hi)))
25 (val (if (> test 0) test (+ test M))))
26 (set-car! state val)
27 val)))
28
29 (define (random-int n state)
30 (modulo (rand state) n))
31
32 ; poker test
33 ; seed 1
34 ; cards 0-9 inclusive (random 10)
35 ; five cards per hand
36 ; 10000 hands
37 ;
38 ; Poker Hand Example Probability Calculated
39 ; 5 of a kind (aaaaa) 0.0001 0
40 ; 4 of a kind (aaaab) 0.0045 0.0053
41 ; Full house (aaabb) 0.009 0.0093
42 ; 3 of a kind (aaabc) 0.072 0.0682
43 ; two pairs (aabbc) 0.108 0.1104
44 ; Pair (aabcd) 0.504 0.501
45 ; Bust (abcde) 0.3024 0.3058
46
47 ; (define (random n)
48 ; (let* ((M 2147483647)
49 ; (slop (modulo M n)))
50 ; (let loop ((r (rand)))
51 ; (if (> r slop)
52 ; (modulo r n)
53 ; (loop (rand))))))
54 ;
55 ; (define (rngtest)
56 ; (display "implementation ")
57 ; (srand 1)
58 ; (let loop ((n 0))
59 ; (if (< n 10000)
60 ; (begin
61 ; (rand)
62 ; (loop (1+ n)))))
63 ; (if (= *seed* 399268537)
64 ; (display "looks correct.")
65 ; (begin
66 ; (display "failed.")
67 ; (newline)
68 ; (display " current seed ") (display *seed*)
69 ; (newline)
70 ; (display " correct seed 399268537")))
71 ; (newline))
72
73 ;------------------------------------------------------------------------------
74 ; Was file "uf.scm".
75
76 ;;; Tarjan's amortised union-find data structure.
77 ;;; Copyright (c) 1995 by Olin Shivers.
78
79 ;;; This data structure implements disjoint sets of elements.
80 ;;; Four operations are supported. The implementation is extremely
81 ;;; fast -- any sequence of N operations can be performed in time
82 ;;; so close to linear it's laughable how close it is. See your
83 ;;; intro data structures book for more. The operations are:
84 ;;;
85 ;;; - (base-set nelts) -> set
86 ;;; Returns a new set, of size NELTS.
87 ;;;
88 ;;; - (set-size s) -> integer
89 ;;; Returns the number of elements in set S.
90 ;;;
91 ;;; - (union! set1 set2)
92 ;;; Unions the two sets -- SET1 and SET2 are now considered the same set
93 ;;; by SET-EQUAL?.
94 ;;;
95 ;;; - (set-equal? set1 set2)
96 ;;; Returns true <==> the two sets are the same.
97
98 ;;; Representation: a set is a cons cell. Every set has a "representative"
99 ;;; cons cell, reached by chasing cdr links until we find the cons with
100 ;;; cdr = (). Set equality is determined by comparing representatives using
101 ;;; EQ?. A representative's car contains the number of elements in the set.
102
103 ;;; The speed of the algorithm comes because when we chase links to find
104 ;;; representatives, we collapse links by changing all the cells in the path
105 ;;; we followed to point directly to the representative, so that next time
106 ;;; we walk the cdr-chain, we'll go directly to the representative in one hop.
107
108
109 (define (base-set nelts) (cons nelts '()))
110
111 ;;; Sets are chained together through cdr links. Last guy in the chain
112 ;;; is the root of the set.
113
114 (define (get-set-root s)
115 (let lp ((r s)) ; Find the last pair
116 (let ((next (cdr r))) ; in the list. That's
117 (cond ((pair? next) (lp next)) ; the root r.
118
119 (else
120 (if (not (eq? r s)) ; Now zip down the list again,
121 (let lp ((x s)) ; changing everyone's cdr to r.
122 (let ((next (cdr x)))
123 (cond ((not (eq? r next))
124 (set-cdr! x r)
125 (lp next))))))
126 r))))) ; Then return r.
127
128 (define (set-equal? s1 s2) (eq? (get-set-root s1) (get-set-root s2)))
129
130 (define (set-size s) (car (get-set-root s)))
131
132 (define (union! s1 s2)
133 (let* ((r1 (get-set-root s1))
134 (r2 (get-set-root s2))
135 (n1 (set-size r1))
136 (n2 (set-size r2))
137 (n (+ n1 n2)))
138
139 (cond ((> n1 n2)
140 (set-cdr! r2 r1)
141 (set-car! r1 n))
142 (else
143 (set-cdr! r1 r2)
144 (set-car! r2 n)))))
145
146 ;------------------------------------------------------------------------------
147 ; Was file "maze.scm".
148
149 ;;; Building mazes with union/find disjoint sets.
150 ;;; Copyright (c) 1995 by Olin Shivers.
151
152 ;;; This is the algorithmic core of the maze constructor.
153 ;;; External dependencies:
154 ;;; - RANDOM-INT
155 ;;; - Union/find code
156 ;;; - bitwise logical functions
157
158 ; (define-record wall
159 ; owner ; Cell that owns this wall.
160 ; neighbor ; The other cell bordering this wall.
161 ; bit) ; Integer -- a bit identifying this wall in OWNER's cell.
162
163 ; (define-record cell
164 ; reachable ; Union/find set -- all reachable cells.
165 ; id ; Identifying info (e.g., the coords of the cell).
166 ; (walls -1) ; A bitset telling which walls are still standing.
167 ; (parent #f) ; For DFS spanning tree construction.
168 ; (mark #f)) ; For marking the solution path.
169
170 (define (make-wall owner neighbor bit)
171 (vector 'wall owner neighbor bit))
172
173 (define (wall:owner o) (vector-ref o 1))
174 (define (set-wall:owner o v) (vector-set! o 1 v))
175 (define (wall:neighbor o) (vector-ref o 2))
176 (define (set-wall:neighbor o v) (vector-set! o 2 v))
177 (define (wall:bit o) (vector-ref o 3))
178 (define (set-wall:bit o v) (vector-set! o 3 v))
179
180 (define (make-cell reachable id)
181 (vector 'cell reachable id -1 #f #f))
182
183 (define (cell:reachable o) (vector-ref o 1))
184 (define (set-cell:reachable o v) (vector-set! o 1 v))
185 (define (cell:id o) (vector-ref o 2))
186 (define (set-cell:id o v) (vector-set! o 2 v))
187 (define (cell:walls o) (vector-ref o 3))
188 (define (set-cell:walls o v) (vector-set! o 3 v))
189 (define (cell:parent o) (vector-ref o 4))
190 (define (set-cell:parent o v) (vector-set! o 4 v))
191 (define (cell:mark o) (vector-ref o 5))
192 (define (set-cell:mark o v) (vector-set! o 5 v))
193
194 ;;; Iterates in reverse order.
195
196 (define (vector-for-each proc v)
197 (let lp ((i (- (vector-length v) 1)))
198 (cond ((>= i 0)
199 (proc (vector-ref v i))
200 (lp (- i 1))))))
201
202
203 ;;; Randomly permute a vector.
204
205 (define (permute-vec! v random-state)
206 (let lp ((i (- (vector-length v) 1)))
207 (cond ((> i 1)
208 (let ((elt-i (vector-ref v i))
209 (j (random-int i random-state))) ; j in [0,i)
210 (vector-set! v i (vector-ref v j))
211 (vector-set! v j elt-i))
212 (lp (- i 1)))))
213 v)
214
215
216 ;;; This is the core of the algorithm.
217
218 (define (dig-maze walls ncells)
219 (call-with-current-continuation
220 (lambda (quit)
221 (vector-for-each
222 (lambda (wall) ; For each wall,
223 (let* ((c1 (wall:owner wall)) ; find the cells on
224 (set1 (cell:reachable c1))
225
226 (c2 (wall:neighbor wall)) ; each side of the wall
227 (set2 (cell:reachable c2)))
228
229 ;; If there is no path from c1 to c2, knock down the
230 ;; wall and union the two sets of reachable cells.
231 ;; If the new set of reachable cells is the whole set
232 ;; of cells, quit.
233 (if (not (set-equal? set1 set2))
234 (let ((walls (cell:walls c1))
235 (wall-mask (bitwise-not (wall:bit wall))))
236 (union! set1 set2)
237 (set-cell:walls c1 (bitwise-and walls wall-mask))
238 (if (= (set-size set1) ncells) (quit #f))))))
239 walls))))
240
241
242 ;;; Some simple DFS routines useful for determining path length
243 ;;; through the maze.
244
245 ;;; Build a DFS tree from ROOT.
246 ;;; (DO-CHILDREN proc maze node) applies PROC to each of NODE's children.
247 ;;; We assume there are no loops in the maze; if this is incorrect, the
248 ;;; algorithm will diverge.
249
250 (define (dfs-maze maze root do-children)
251 (let search ((node root) (parent #f))
252 (set-cell:parent node parent)
253 (do-children (lambda (child)
254 (if (not (eq? child parent))
255 (search child node)))
256 maze node)))
257
258 ;;; Move the root to NEW-ROOT.
259
260 (define (reroot-maze new-root)
261 (let lp ((node new-root) (new-parent #f))
262 (let ((old-parent (cell:parent node)))
263 (set-cell:parent node new-parent)
264 (if old-parent (lp old-parent node)))))
265
266 ;;; How far from CELL to the root?
267
268 (define (path-length cell)
269 (do ((len 0 (+ len 1))
270 (node (cell:parent cell) (cell:parent node)))
271 ((not node) len)))
272
273 ;;; Mark the nodes from NODE back to root. Used to mark the winning path.
274
275 (define (mark-path node)
276 (let lp ((node node))
277 (set-cell:mark node #t)
278 (cond ((cell:parent node) => lp))))
279
280 ;------------------------------------------------------------------------------
281 ; Was file "harr.scm".
282
283 ;;; Hex arrays
284 ;;; Copyright (c) 1995 by Olin Shivers.
285
286 ;;; External dependencies:
287 ;;; - define-record
288
289 ;;; ___ ___ ___
290 ;;; / \ / \ / \
291 ;;; ___/ A \___/ A \___/ A \___
292 ;;; / \ / \ / \ / \
293 ;;; / A \___/ A \___/ A \___/ A \
294 ;;; \ / \ / \ / \ /
295 ;;; \___/ \___/ \___/ \___/
296 ;;; / \ / \ / \ / \
297 ;;; / \___/ \___/ \___/ \
298 ;;; \ / \ / \ / \ /
299 ;;; \___/ \___/ \___/ \___/
300 ;;; / \ / \ / \ / \
301 ;;; / \___/ \___/ \___/ \
302 ;;; \ / \ / \ / \ /
303 ;;; \___/ \___/ \___/ \___/
304
305 ;;; Hex arrays are indexed by the (x,y) coord of the center of the hexagonal
306 ;;; element. Hexes are three wide and two high; e.g., to get from the center
307 ;;; of an elt to its {NW, N, NE} neighbors, add {(-3,1), (0,2), (3,1)}
308 ;;; respectively.
309 ;;;
310 ;;; Hex arrays are represented with a matrix, essentially made by shoving the
311 ;;; odd columns down a half-cell so things line up. The mapping is as follows:
312 ;;; Center coord row/column
313 ;;; ------------ ----------
314 ;;; (x, y) -> (y/2, x/3)
315 ;;; (3c, 2r + c&1) <- (r, c)
316
317
318 ; (define-record harr
319 ; nrows
320 ; ncols
321 ; elts)
322
323 (define (make-harr nrows ncols elts)
324 (vector 'harr nrows ncols elts))
325
326 (define (harr:nrows o) (vector-ref o 1))
327 (define (set-harr:nrows o v) (vector-set! o 1 v))
328 (define (harr:ncols o) (vector-ref o 2))
329 (define (set-harr:ncols o v) (vector-set! o 2 v))
330 (define (harr:elts o) (vector-ref o 3))
331 (define (set-harr:elts o v) (vector-set! o 3 v))
332
333 (define (harr r c)
334 (make-harr r c (make-vector (* r c))))
335
336
337
338 (define (href ha x y)
339 (let ((r (quotient y 2))
340 (c (quotient x 3)))
341 (vector-ref (harr:elts ha)
342 (+ (* (harr:ncols ha) r) c))))
343
344 (define (hset! ha x y val)
345 (let ((r (quotient y 2))
346 (c (quotient x 3)))
347 (vector-set! (harr:elts ha)
348 (+ (* (harr:ncols ha) r) c)
349 val)))
350
351 (define (href/rc ha r c)
352 (vector-ref (harr:elts ha)
353 (+ (* (harr:ncols ha) r) c)))
354
355 ;;; Create a nrows x ncols hex array. The elt centered on coord (x, y)
356 ;;; is the value returned by (PROC x y).
357
358 (define (harr-tabulate nrows ncols proc)
359 (let ((v (make-vector (* nrows ncols))))
360
361 (do ((r (- nrows 1) (- r 1)))
362 ((< r 0))
363 (do ((c 0 (+ c 1))
364 (i (* r ncols) (+ i 1)))
365 ((= c ncols))
366 (vector-set! v i (proc (* 3 c) (+ (* 2 r) (bitwise-and c 1))))))
367
368 (make-harr nrows ncols v)))
369
370
371 (define (harr-for-each proc harr)
372 (vector-for-each proc (harr:elts harr)))
373
374 ;------------------------------------------------------------------------------
375 ; Was file "hex.scm".
376
377 ;;; Hexagonal hackery for maze generation.
378 ;;; Copyright (c) 1995 by Olin Shivers.
379
380 ;;; External dependencies:
381 ;;; - cell and wall records
382 ;;; - Functional Postscript for HEXES->PATH
383 ;;; - logical functions for bit hacking
384 ;;; - hex array code.
385
386 ;;; To have the maze span (0,0) to (1,1):
387 ;;; (scale (/ (+ 1 (* 3 ncols))) (/ (+ 1 (* 2 nrows)))
388 ;;; (translate (point 2 1) maze))
389
390 ;;; Every elt of the hex array manages his SW, S, and SE wall.
391 ;;; Terminology: - An even column is one whose column index is even. That
392 ;;; means the first, third, ... columns (indices 0, 2, ...).
393 ;;; - An odd column is one whose column index is odd. That
394 ;;; means the second, fourth... columns (indices 1, 3, ...).
395 ;;; The even/odd flip-flop is confusing; be careful to keep it
396 ;;; straight. The *even* columns are the low ones. The *odd*
397 ;;; columns are the high ones.
398 ;;; _ _
399 ;;; _/ \_/ \
400 ;;; / \_/ \_/
401 ;;; \_/ \_/ \
402 ;;; / \_/ \_/
403 ;;; \_/ \_/ \
404 ;;; / \_/ \_/
405 ;;; \_/ \_/ \
406 ;;; / \_/ \_/
407 ;;; \_/ \_/
408 ;;; 0 1 2 3
409
410 (define south-west 1)
411 (define south 2)
412 (define south-east 4)
413
414 (define (gen-maze-array r c)
415 (harr-tabulate r c (lambda (x y) (make-cell (base-set 1) (cons x y)))))
416
417 ;;; This could be made more efficient.
418 (define (make-wall-vec harr)
419 (let* ((nrows (harr:nrows harr))
420 (ncols (harr:ncols harr))
421 (xmax (* 3 (- ncols 1)))
422
423 ;; Accumulate walls.
424 (walls '())
425 (add-wall (lambda (o n b) ; owner neighbor bit
426 (set! walls (cons (make-wall o n b) walls)))))
427
428 ;; Do everything but the bottom row.
429 (do ((x (* (- ncols 1) 3) (- x 3)))
430 ((< x 0))
431 (do ((y (+ (* (- nrows 1) 2) (bitwise-and x 1))
432 (- y 2)))
433 ((<= y 1)) ; Don't do bottom row.
434 (let ((hex (href harr x y)))
435 (if (not (zero? x))
436 (add-wall hex (href harr (- x 3) (- y 1)) south-west))
437 (add-wall hex (href harr x (- y 2)) south)
438 (if (< x xmax)
439 (add-wall hex (href harr (+ x 3) (- y 1)) south-east)))))
440
441 ;; Do the SE and SW walls of the odd columns on the bottom row.
442 ;; If the rightmost bottom hex lies in an odd column, however,
443 ;; don't add it's SE wall -- it's a corner hex, and has no SE neighbor.
444 (if (> ncols 1)
445 (let ((rmoc-x (+ 3 (* 6 (quotient (- ncols 2) 2)))))
446 ;; Do rightmost odd col.
447 (let ((rmoc-hex (href harr rmoc-x 1)))
448 (if (< rmoc-x xmax) ; Not a corner -- do E wall.
449 (add-wall rmoc-hex (href harr xmax 0) south-east))
450 (add-wall rmoc-hex (href harr (- rmoc-x 3) 0) south-west))
451
452 (do ((x (- rmoc-x 6) ; Do the rest of the bottom row's odd cols.
453 (- x 6)))
454 ((< x 3)) ; 3 is X coord of leftmost odd column.
455 (add-wall (href harr x 1) (href harr (- x 3) 0) south-west)
456 (add-wall (href harr x 1) (href harr (+ x 3) 0) south-east))))
457
458 (list->vector walls)))
459
460
461 ;;; Find the cell ctop from the top row, and the cell cbot from the bottom
462 ;;; row such that cbot is furthest from ctop.
463 ;;; Return [ctop-x, ctop-y, cbot-x, cbot-y].
464
465 (define (pick-entrances harr)
466 (dfs-maze harr (href/rc harr 0 0) for-each-hex-child)
467 (let ((nrows (harr:nrows harr))
468 (ncols (harr:ncols harr)))
469 (let tp-lp ((max-len -1)
470 (entrance #f)
471 (exit #f)
472 (tcol (- ncols 1)))
473 (if (< tcol 0) (vector entrance exit)
474 (let ((top-cell (href/rc harr (- nrows 1) tcol)))
475 (reroot-maze top-cell)
476 (let ((result
477 (let bt-lp ((max-len max-len)
478 (entrance entrance)
479 (exit exit)
480 (bcol (- ncols 1)))
481 ; (format #t "~a ~a ~a ~a~%" max-len entrance exit bcol)
482 (if (< bcol 0) (vector max-len entrance exit)
483 (let ((this-len (path-length (href/rc harr 0 bcol))))
484 (if (> this-len max-len)
485 (bt-lp this-len tcol bcol (- bcol 1))
486 (bt-lp max-len entrance exit (- bcol 1))))))))
487 (let ((max-len (vector-ref result 0))
488 (entrance (vector-ref result 1))
489 (exit (vector-ref result 2)))
490 (tp-lp max-len entrance exit (- tcol 1)))))))))
491
492
493
494 ;;; Apply PROC to each node reachable from CELL.
495 (define (for-each-hex-child proc harr cell)
496 (let* ((walls (cell:walls cell))
497 (id (cell:id cell))
498 (x (car id))
499 (y (cdr id))
500 (nr (harr:nrows harr))
501 (nc (harr:ncols harr))
502 (maxy (* 2 (- nr 1)))
503 (maxx (* 3 (- nc 1))))
504 (if (not (bit-test walls south-west)) (proc (href harr (- x 3) (- y 1))))
505 (if (not (bit-test walls south)) (proc (href harr x (- y 2))))
506 (if (not (bit-test walls south-east)) (proc (href harr (+ x 3) (- y 1))))
507
508 ;; NW neighbor, if there is one (we may be in col 1, or top row/odd col)
509 (if (and (> x 0) ; Not in first column.
510 (or (<= y maxy) ; Not on top row or
511 (zero? (modulo x 6)))) ; not in an odd column.
512 (let ((nw (href harr (- x 3) (+ y 1))))
513 (if (not (bit-test (cell:walls nw) south-east)) (proc nw))))
514
515 ;; N neighbor, if there is one (we may be on top row).
516 (if (< y maxy) ; Not on top row
517 (let ((n (href harr x (+ y 2))))
518 (if (not (bit-test (cell:walls n) south)) (proc n))))
519
520 ;; NE neighbor, if there is one (we may be in last col, or top row/odd col)
521 (if (and (< x maxx) ; Not in last column.
522 (or (<= y maxy) ; Not on top row or
523 (zero? (modulo x 6)))) ; not in an odd column.
524 (let ((ne (href harr (+ x 3) (+ y 1))))
525 (if (not (bit-test (cell:walls ne) south-west)) (proc ne))))))
526
527
528
529 ;;; The top-level
530 (define (make-maze nrows ncols)
531 (let* ((cells (gen-maze-array nrows ncols))
532 (walls (permute-vec! (make-wall-vec cells) (random-state 20))))
533 (dig-maze walls (* nrows ncols))
534 (let ((result (pick-entrances cells)))
535 (let ((entrance (vector-ref result 0))
536 (exit (vector-ref result 1)))
537 (let* ((exit-cell (href/rc cells 0 exit))
538 (walls (cell:walls exit-cell)))
539 (reroot-maze (href/rc cells (- nrows 1) entrance))
540 (mark-path exit-cell)
541 (set-cell:walls exit-cell (bitwise-and walls (bitwise-not south)))
542 (vector cells entrance exit))))))
543
544
545 (define (pmaze nrows ncols)
546 (let ((result (make-maze nrows ncols)))
547 (let ((cells (vector-ref result 0))
548 (entrance (vector-ref result 1))
549 (exit (vector-ref result 2)))
550 (print-hexmaze cells entrance))))
551
552 ;------------------------------------------------------------------------------
553 ; Was file "hexprint.scm".
554
555 ;;; Print out a hex array with characters.
556 ;;; Copyright (c) 1995 by Olin Shivers.
557
558 ;;; External dependencies:
559 ;;; - hex array code
560 ;;; - hex cell code
561
562 ;;; _ _
563 ;;; _/ \_/ \
564 ;;; / \_/ \_/
565 ;;; \_/ \_/ \
566 ;;; / \_/ \_/
567 ;;; \_/ \_/ \
568 ;;; / \_/ \_/
569 ;;; \_/ \_/ \
570 ;;; / \_/ \_/
571 ;;; \_/ \_/
572
573 ;;; Top part of top row looks like this:
574 ;;; _ _ _ _
575 ;;; _/ \_/ \/ \_/ \
576 ;;; /
577
578 (define output #f) ; the list of all characters written out, in reverse order.
579
580 (define (write-ch c)
581 (set! output (cons c output)))
582
583 (define (print-hexmaze harr entrance)
584 (let* ((nrows (harr:nrows harr))
585 (ncols (harr:ncols harr))
586 (ncols2 (* 2 (quotient ncols 2))))
587
588 ;; Print out the flat tops for the top row's odd cols.
589 (do ((c 1 (+ c 2)))
590 ((>= c ncols))
591 ; (display " ")
592 (write-ch #\space)
593 (write-ch #\space)
594 (write-ch #\space)
595 (write-ch (if (= c entrance) #\space #\_)))
596 ; (newline)
597 (write-ch #\newline)
598
599 ;; Print out the slanted tops for the top row's odd cols
600 ;; and the flat tops for the top row's even cols.
601 (write-ch #\space)
602 (do ((c 0 (+ c 2)))
603 ((>= c ncols2))
604 ; (format #t "~a/~a\\"
605 ; (if (= c entrance) #\space #\_)
606 ; (dot/space harr (- nrows 1) (+ c 1)))
607 (write-ch (if (= c entrance) #\space #\_))
608 (write-ch #\/)
609 (write-ch (dot/space harr (- nrows 1) (+ c 1)))
610 (write-ch #\\))
611 (if (odd? ncols)
612 (write-ch (if (= entrance (- ncols 1)) #\space #\_)))
613 ; (newline)
614 (write-ch #\newline)
615
616 (do ((r (- nrows 1) (- r 1)))
617 ((< r 0))
618
619 ;; Do the bottoms for row r's odd cols.
620 (write-ch #\/)
621 (do ((c 1 (+ c 2)))
622 ((>= c ncols2))
623 ;; The dot/space for the even col just behind c.
624 (write-ch (dot/space harr r (- c 1)))
625 (display-hexbottom (cell:walls (href/rc harr r c))))
626
627 (cond ((odd? ncols)
628 (write-ch (dot/space harr r (- ncols 1)))
629 (write-ch #\\)))
630 ; (newline)
631 (write-ch #\newline)
632
633 ;; Do the bottoms for row r's even cols.
634 (do ((c 0 (+ c 2)))
635 ((>= c ncols2))
636 (display-hexbottom (cell:walls (href/rc harr r c)))
637 ;; The dot/space is for the odd col just after c, on row below.
638 (write-ch (dot/space harr (- r 1) (+ c 1))))
639
640 (cond ((odd? ncols)
641 (display-hexbottom (cell:walls (href/rc harr r (- ncols 1)))))
642 ((not (zero? r)) (write-ch #\\)))
643 ; (newline)
644 (write-ch #\newline))))
645
646 (define (bit-test j bit)
647 (not (zero? (bitwise-and j bit))))
648
649 ;;; Return a . if harr[r,c] is marked, otherwise a space.
650 ;;; We use the dot to mark the solution path.
651 (define (dot/space harr r c)
652 (if (and (>= r 0) (cell:mark (href/rc harr r c))) #\. #\space))
653
654 ;;; Print a \_/ hex bottom.
655 (define (display-hexbottom hexwalls)
656 (write-ch (if (bit-test hexwalls south-west) #\\ #\space))
657 (write-ch (if (bit-test hexwalls south ) #\_ #\space))
658 (write-ch (if (bit-test hexwalls south-east) #\/ #\space)))
659
660 ;;; _ _
661 ;;; _/ \_/ \
662 ;;; / \_/ \_/
663 ;;; \_/ \_/ \_/
664 ;;; / \_/ \_/
665 ;;; \_/ \_/ \
666 ;;; / \_/ \_/
667 ;;; \_/ \_/ \
668 ;;; / \_/ \_/
669 ;;; \_/ \_/ \_/
670
671 ;------------------------------------------------------------------------------
672
673 (define (run nrows ncols)
674 (set! output '())
675 (pmaze nrows ncols)
676 (reverse output))
677
678 (define (main . args)
679 (run-benchmark
680 "maze"
681 maze-iters
682 (lambda (result)
683 (equal? result '
684 (#\ #\ #\ #\_ #\ #\ #\ #\_ #\ #\ #\ #\_ #\newline
685 #\ #\_ #\/ #\ #\\ #\_ #\/ #\ #\\ #\_ #\/ #\. #\\ #\ #\newline
686 #\/ #\ #\\ #\ #\ #\ #\\ #\_ #\ #\. #\ #\ #\/ #\. #\\ #\newline
687 #\\ #\ #\ #\ #\\ #\ #\/ #\. #\ #\_ #\/ #\. #\\ #\ #\/ #\newline
688 #\/ #\ #\\ #\_ #\/ #\. #\ #\_ #\/ #\ #\\ #\_ #\ #\. #\\ #\newline
689 #\\ #\ #\/ #\ #\\ #\ #\/ #\ #\ #\_ #\/ #\ #\\ #\_ #\/ #\newline
690 #\/ #\ #\ #\_ #\/ #\. #\\ #\ #\/ #\ #\\ #\ #\/ #\ #\\ #\newline
691 #\\ #\ #\/ #\ #\\ #\ #\/ #\ #\ #\_ #\/ #\ #\ #\ #\/ #\newline
692 #\/ #\ #\\ #\ #\/ #\. #\\ #\ #\/ #\. #\\ #\_ #\/ #\ #\\ #\newline
693 #\\ #\_ #\/ #\ #\\ #\ #\/ #\. #\ #\_ #\ #\. #\\ #\ #\/ #\newline
694 #\/ #\ #\\ #\_ #\ #\. #\ #\_ #\/ #\ #\\ #\ #\ #\ #\\ #\newline
695 #\\ #\_ #\ #\ #\\ #\_ #\/ #\ #\ #\_ #\/ #\. #\\ #\ #\/ #\newline
696 #\/ #\ #\ #\_ #\/ #\ #\ #\ #\/ #\ #\\ #\ #\/ #\ #\\ #\newline
697 #\\ #\_ #\ #\ #\\ #\ #\/ #\ #\\ #\_ #\ #\. #\\ #\_ #\/ #\newline
698 #\/ #\ #\\ #\_ #\ #\ #\\ #\_ #\ #\ #\\ #\_ #\ #\. #\\ #\newline
699 #\\ #\_ #\ #\ #\\ #\_ #\/ #\ #\ #\_ #\/ #\. #\\ #\ #\/ #\newline
700 #\/ #\ #\\ #\_ #\ #\ #\\ #\ #\/ #\. #\\ #\ #\ #\. #\\ #\newline
701 #\\ #\ #\/ #\. #\\ #\_ #\ #\. #\ #\ #\/ #\. #\\ #\ #\/ #\newline
702 #\/ #\ #\ #\ #\ #\. #\ #\_ #\/ #\. #\\ #\ #\/ #\ #\\ #\newline
703 #\\ #\ #\/ #\. #\\ #\_ #\/ #\. #\\ #\_ #\ #\. #\\ #\ #\/ #\newline
704 #\/ #\ #\\ #\_ #\ #\. #\ #\ #\/ #\ #\ #\_ #\/ #\ #\\ #\newline
705 #\\ #\_ #\ #\ #\\ #\_ #\/ #\. #\\ #\_ #\ #\ #\\ #\_ #\/ #\newline
706 #\/ #\ #\ #\_ #\/ #\ #\\ #\ #\/ #\ #\\ #\_ #\ #\ #\\ #\newline
707 #\\ #\_ #\/ #\ #\ #\_ #\/ #\. #\\ #\_ #\ #\ #\\ #\_ #\/ #\newline
708 #\/ #\ #\\ #\ #\/ #\ #\ #\_ #\ #\. #\ #\_ #\ #\ #\\ #\newline
709 #\\ #\ #\/ #\ #\\ #\_ #\/ #\. #\ #\_ #\ #\ #\\ #\_ #\/ #\newline
710 #\/ #\ #\ #\_ #\ #\ #\\ #\ #\ #\ #\\ #\_ #\/ #\ #\\ #\newline
711 #\\ #\_ #\/ #\. #\\ #\_ #\ #\. #\\ #\_ #\/ #\ #\ #\_ #\/ #\newline
712 #\/ #\ #\\ #\ #\ #\. #\ #\_ #\/ #\ #\ #\ #\/ #\ #\\ #\newline
713 #\\ #\ #\/ #\. #\\ #\_ #\/ #\ #\\ #\_ #\/ #\. #\\ #\ #\/ #\newline
714 #\/ #\ #\\ #\_ #\ #\. #\ #\_ #\/ #\. #\ #\ #\ #\ #\\ #\newline
715 #\\ #\ #\ #\ #\ #\ #\ #\. #\ #\ #\/ #\. #\\ #\_ #\/ #\newline
716 #\/ #\ #\\ #\_ #\/ #\ #\\ #\_ #\/ #\ #\\ #\_ #\ #\. #\\ #\newline
717 #\\ #\_ #\/ #\ #\ #\ #\/ #\ #\\ #\_ #\/ #\. #\ #\ #\/ #\newline
718 #\/ #\ #\ #\ #\/ #\ #\ #\_ #\ #\ #\\ #\ #\/ #\ #\\ #\newline
719 #\\ #\_ #\/ #\ #\\ #\_ #\/ #\ #\\ #\_ #\/ #\. #\\ #\_ #\/ #\newline
720 #\/ #\ #\\ #\_ #\/ #\ #\ #\_ #\/ #\ #\\ #\_ #\ #\. #\\ #\newline
721 #\\ #\ #\ #\ #\ #\_ #\/ #\. #\ #\ #\/ #\. #\ #\_ #\/ #\newline
722 #\/ #\ #\\ #\ #\/ #\. #\ #\ #\/ #\ #\\ #\_ #\ #\. #\\ #\newline
723 #\\ #\_ #\/ #\. #\ #\_ #\/ #\. #\\ #\_ #\/ #\. #\\ #\ #\/ #\newline
724 #\/ #\ #\ #\_ #\ #\. #\\ #\_ #\ #\. #\ #\_ #\ #\. #\\ #\newline
725 #\\ #\_ #\/ #\ #\\ #\ #\/ #\ #\\ #\_ #\/ #\ #\\ #\_ #\/ #\newline)))
726 (lambda (nrows ncols) (lambda () (run nrows ncols)))
727 20
728 7))