cancel
Showing results for 
Search instead for 
Did you mean: 

life in one line of q

Aaron_Davies
New Contributor
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

6 REPLIES 6

Attila
New Contributor

quite fun

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

Attila
New Contributor

life:{(3=a)|x&4=a:2 sum/2 rotate'\:[1 0 -1]/x}
Attila

Attila
New Contributor

{3=a-x*4=a:2 sum/2(1 0 -1 rotate'\:)/x}
Attila

/ same code length but without the local variable life:{0 wrote: > � {3=a-x*4=a:2 sum/2(1 0 -1 rotate'\:)/x} > � �Attila > > On 1 Feb 2009, at 23:28, Attila Vrabecz wrote: > > > life:{(3=a)|x&4=a:2 sum/2 rotate'\:[1 0 -1]/x} > > � �Attila > > > On 1 Feb 2009, at 23:23, Attila Vrabecz wrote: > > >> quite fun > > >> life:{any(1;x)&3 4=\:2 sum/2(1 0 -1 rotate'\:)/x} > >> � �Attila > > >> On 1 Feb 2009, at 15:33, Aaron Davies wrote: > > >>> life in one line of APL, recently popular on reddit, etc. > > >>>http://www.youtube.com/watch?v=a9xAKttWgP4&fmt=18 > > >>> my q port > > >>> 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

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 wrote:> / same code length but without the local variable> life:{0

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/SHOn Feb 4, 8:41�am, Chris Burke wrote:> 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> / 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>> On Feb 3, 11:33�pm, Swee Heng wrote:>> > / same code length but without the local variable> > life:{0