life in one line of q

105 views
Skip to first unread message

Aaron Davies

unread,
Feb 1, 2009, 10:33:08 AM2/1/09
to personal...@googlegroups.com
life in one line of APL, recently popular on reddit, etc.

fps:"I"$first((.Q.opt .z.x)`fps),"5"
.q.clear:{1"\033[H\033[J";}
disp:{1@-1_` sv .Q.s each x;}
life:{any(1b;x)&'3 4=\:sum sum f(f:rotate'\:[1 0 -1])x}
gen:{y life/x}
frame:{clear[];-1 ssr[;"0";" "]each raze each string x;system"sleep ",string 1%fps}
run:{frame each(life\)x;}
r:(3 3#til 9)in 1 2 3 4 7
R:-1 rotate -2 rotate'(5#7#'r)&(3#enlist 1110000b),2 7#0b
RR:((5 35#0b),(13#0b),/:R,\:15#0b),5 35#0b
F:"1"=(
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                11                 ";
 "               11                  ";
 "                1                  ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   "
 )
glider:"1"=(
 " 1                                 ";
 "  1                                ";
 "111                                ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   ";
 "                                   "
 )
\
run F

Attila Vrabecz

unread,
Feb 1, 2009, 6:23:00 PM2/1/09
to personal...@googlegroups.com
quite fun

life:{any(1;x)&3 4=\:2 sum/2(1 0 -1 rotate'\:)/x}
   Attila 

Attila Vrabecz

unread,
Feb 1, 2009, 6:28:50 PM2/1/09
to [kdb+]
life:{(3=a)|x&4=a:2 sum/2 rotate'\:[1 0 -1]/x}
   Attila 

Attila Vrabecz

unread,
Feb 1, 2009, 8:52:35 PM2/1/09
to [kdb+]
 {3=a-x*4=a:2 sum/2(1 0 -1 rotate'\:)/x}
  Attila 

Swee Heng

unread,
Feb 3, 2009, 10:33:25 AM2/3/09
to Kdb+ Personal Developers
/ same code length but without the local variable
life:{0<x+0 1@4-2 sum/2(1 0 -1 rotate'\:)/x}

/ modified frame[] that draws a nice box around the cells
box:{" @|-+"b,a,b:3 3 4@1#a:2,/:x,\:2}
frame:{clear[];-1 box x;system"sleep ",string 1%fps;}

Swee Heng

Chris Burke

unread,
Feb 3, 2009, 7:41:08 PM2/3/09
to Kdb+ Personal Developers
Another approach is to precompute the neighborhoods and do a lookup.
This is more efficient, with a simple life step, but not so youtube-
friendly.

F:"1"=(
" ";
" ";
" ";
" ";
" ";
" ";
" 11 ";
" 11 ";
" 1 ";
" ";
" ";
" ";
" ";
" ";
" "
)

lifeSH:{0<x+0 1@4-2 sum/2(1 0 -1 rotate'\:)/x} / Swee Heng

/ life by lookup:
t:0b vs ' til "i"$2 xexp 9
ctr:t[;27]
cnt:sum each t
trans:(cnt=3) or (ctr=1) and cnt=4

lifeinit:{
r:count x;c:count first x;
index::raze each raze 2 (1 0 -1 rotate'\:)/ (r,c) # til r*c;
raze x}

life:{trans 2 sv x index} / single step
liferun:{(life\) lifeinit x}

-----------------

count liferun F
383

(liferun F) ~ raze each (lifeSH\)F
1b

\t do[20;liferun F]
277

\t do[20;(lifeSH\) F]
1511

Swee Heng

unread,
Feb 11, 2009, 11:29:42 AM2/11/09
to Kdb+ Personal Developers
For those interested in Game of Life...

I've written a summary of the q implementations posted here.
- http://thesweeheng.wordpress.com/2009/02/10/game-of-life-in-one-line-of-q/

Plus implementations in k4.
- http://thesweeheng.wordpress.com/2009/02/10/game-of-life-in-one-line-of-k/

SH
Reply all
Reply to author
Forward
0 new messages