[racket] Using Racket to solve Professor Layton puzzles

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Jul 27 15:02:16 EDT 2011

Yesterday, Jay McCarthy wrote:
> My blog now has what I believe is a solution. At least, the test
> case you provide passes and the next few elements make sense.

That looks right, though pretty verbose...  (And BTW, you're missing
some way to mark the code as such -- there's no indentation!)
Here's my code:

  #lang lazy
  (define (counts l num count)
    (cond [(= count 0) (counts (cdr l) (car l) 1)]
          [(= (car l) num) (counts (cdr l) num (add1 count))]
          [else (list* count num (counts l 0 0))]))
  (define foo (list* 1 1 2 1 (counts (cddr foo) 0 0)))

The interesting bit here is that I need to start it with 4 numbers.
(And an amusing fact: once I told Matthias what the problem was, it
took him about two seconds to see that problem coming up.)

I think that you're "cheating" around this problem here:

     (if (empty? elements)
       (loop #f 0 (list count last))

Another thing that looks like it could be unkosher is that instead of
feeding the sequence to itself you're building a description then go
in a loop with the new description.  It looks like that could break if
a description chunk begins with the same element the list it's
describing ends with.

In any case, what was nice about this is that when I first wrote the
code and used `1 1' to bootstrap it, it failed with a reentrant
promise.  I immediately thought that there was a bug in the code
before I realized that it works exactly as it should.  The lazy
stepper should help clarify that!

(But apparently Stephen is taking one of these mysterious "vakeyshon"
things that seem to be getting popular recently.  I don't know how one
looks but they sound very dangerous.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.