[racket] Challenge: Game of life in 140 chars

From: Jeremiah Willcock (jewillco at osl.iu.edu)
Date: Sun Feb 19 22:52:28 EST 2012

On Sun, 19 Feb 2012, Jeremiah Willcock wrote:

> On Sun, 19 Feb 2012, SF wrote:
>
>> Here's a rough draft that has tons of room for improvement:
>> 
>> #lang racket
>> 
>> (define glider
>>  (string->number "#x\
>> 010000\
>> 001000\
>> 111000\
>> 000000\
>> 000000\
>> 000000\
>> "))
>> 
>> (define (life s)
>>  (define (c s n)
>>    (if (= -1 n)
>>        0
>>        (+ (if (bitwise-bit-set? 3080 (arithmetic-shift s (* -4 n)))
>>               (expt 16 n)
>>               0)
>>           (c (modulo s (expt 16 n)) (sub1 n)))))
>>  (c (* s #x111000181000111/10000000)
>>     36))

Here's a 136-character version (after unnecessary spaces are removed):

(define (life s)
   (let* ((a bitwise-and)
          (s (bitwise-ior (* s #x111000101000111/10000000) s)))
     (a (* (a (- -1 s) (* s 4)) 3/32)
        (/ (- (expt 8 48) 1) 15))))

-- Jeremiah Willcock

Posted on the users mailing list.