Forum: Ruby Re: Golfing a signature.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel S. (Guest)
on 2006-05-04 04:37
(Received via mailing list)
You can shave off another 12 characters by changing the construction of
the "z" array:

q,z=-1..1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-=[[0,0]]).map{
q=(m=[-1,0,1])-[0];(m*2+q).sort.zip(m+q+m).map{

Firstly, z isn't used after this, so you don't need to reassign it:

q,z=-1..1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-[[0,0]]).map{

But the whole thing is pointless, as it's not much better that spelling
out the entire array:

[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].map {

so shorten that with a zip:

[-1,-1,-1,0,0,1,1,1].zip([-1,0,1,-1,1,-1,0,1]).map{

removing some repetition makes it slightly longer:

[-1,-1,-1,0,0,1,1,1].zip((m=[-1,0,1])+(m-[0])+m).map{

but we can fix that:

q=(m=[-1,0,1])-[0];(m*2+q).sort.zip(m+q+m).map{


-----

Also the:

c[i+x[0]][e+x[1]]rescue u

can be rewritten as:

(c[i+x[0]]||[])[e+x[1]]

----

I also rejuggled things to move that "\n" so that it made use of the end
of the line, and There was a reference to " ", which I replaced with f.
I might have done some other things too, but I don't have much of a
memory.

$><<"\e[2J";s,o,f,c,u=20,"#","
";b,q=(z=0..s).map{z.map{(rand<0.3)?o:f}},
(m=[-1,0,1])-[0];until
c==b;c=b.map{|z|z.dup};$><<"\e[H"<<b.map{|x|x*f}*"
";s.times{|i|s.times{|e|n=((m*2+q).sort.zip(m+q+m).map{|x|(c[i+x[0]]||[
])[e+x[1]]}-[f,u]).size;b[i][e]=(n==2?b[i][e]:n==3?o:f)}};sleep 0.2;end

That's 292 characters.
This topic is locked and can not be replied to.