Debian Codegraph @

benchmarks/rnrs-benchmarks/maze.ss

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