The following program was a puzzle in the 2011 MIT Mystery Hunt held last weekend. (I'm not linking directly to the site because they posted a solution and I don't want you to be tempted to look at the answer prematurely...) I was disappointed that Dr Racket doesn't seem to have a multi-threaded stepping debugger, at least not in a form that could easily run this program-- correct me if I'm wrong.<br>
<br>I would be interested to hear what other ideas people have about solving this puzzle, either automatically or by hand (or some combination). But, I haven't solved it myself yet, so I may be avoiding reading replies for a while...<br>
<br>--Doug<br><br><br><br>Efficiency
<p><i>The Prince is quite proud of his competitive Katamari-rolling
time record, having mastered the art of multitasking.</i></p>
<p>All procedure calls are evaluated from left to right.</p><p></p><p><br></p><p></p>
<pre>(define (send x)<br> (if (or (integer? x) (char? x))<br> (display x)<br> (send x))<br> (flush-output)<br> #t)<br>(define (sendc x)<br> (if (and (>= x 0) (< x 26))<br> (send (integer->char (+ 65 x)))<br>
(sendc x)))<br><br>(define a 2)<br>(define b '(11))<br>(define c 5)<br>(define d 30)<br>(define e 3)<br><br>(thread<br> (lambda ()<br> (set! a (* a (first a)))<br> (send a)<br> (set! c (lambda () (- b 4)))<br>
(set! c (lambda () (- b 11)))<br> (set! d (rest d))<br> (sendc (/ 4 (+ (- c) c)))<br> (set! a (sqrt (+ c (* c c))))<br> (sendc b)<br> (set! b '(((24 12 15 (2))) 1 (5 1) 3 2 8 10 (9 1 (3)) ((3) (1)) 5 14 10))<br>
(set! d '())<br> (sendc (length (append c b)))<br> (sendc (length (append a b c c c d)))<br> (set! d 2)<br> (map (lambda (x)<br> (set! c (first b))<br> (sendc x)<br> (set! b c)) a)<br>
(send (* (+ 3 (first c)) 100 (- (sqrt a) 1)))<br> (set! a (+ a 1))<br> (set! e (- e 1))<br> ))<br><br>(thread<br> (lambda ()<br> (set! a (list c))<br> (set! a (lambda (x) (if x (sendc x) x)))<br> (let loop ()<br>
(set! b (c))<br> (if (a b)<br> (loop)<br> (set! d '(2 4 8 16 32))))<br> (set! c (first d))<br> (set! c (second d))<br> (set! b (- a (sqrt (first d))))<br> (set! a (list 14 (- (length b) 1) c))<br>
(let loop ()<br> (set! d b)<br> (if (null? d)<br> (set! a (+ a 1))<br> (begin<br> (sendc (first d))<br> (set! b (rest d))<br> (loop))))<br> (set! e (- e 1))<br> ))<br>
<br>(thread<br> (lambda ()<br> (set! b d)<br> (set! c (lambda () (/ b 2)))<br> (let loop ()<br> (set! d (- d 10))<br> (if (and (= d 0)<br> (= b 0)<br>         (< b 0))<br> (set! b #f)<br>
(loop)))<br> (set! c (first d))<br> (set! a 0)<br> (sendc (length b))<br> (set! c (list (second b)))<br> (sendc (sqrt (+ 12 (length d))))<br> (send (+ (/ (foldl * 26 a)<br> (+ (length (append a b))<br>
                 (* (length c) (length c))))<br> d))<br> (set! a (first b))<br> (sendc (/ 204 a))<br> (set! e (- e 1))<br> ))<br><br>(let loop ()<br> (if (> e 0)<br> (loop)<br> (newline)))<br><br></pre>