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 05:56
(Received via mailing list)
And another bit:

b[i][e]=(n==2?b[i][e]:n==3?o:f

becomes:

n!=2?b[i][e]=n==3?o:f:f


----

The z array can be reduced further

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

becomes:

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

----

Then, the calculation of n can be improved by using separate variables:

n=((m.sort.zip(m)-[[0,0]]).map{|x|(c[i+x[0]]||[])[e+x[1]]}-[f,u]).size

becomes:

n=((m.sort.zip(m)-[[0,0]]).map{|z,x|(c[i+z]||[])[e+x]}-[f,u]).size

and then, turn it into a select statement:

n=(m.sort.zip(m)-[[0,0]]).select{|x,z|o==(c[i+x]||[])[e+z]}.size


----

This leaves:

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

272 Characters. I think I'll quit now.
Joey (Guest)
on 2006-05-04 10:30
(Received via mailing list)
The b.map{|x|x*" "} cannot be changed to b.map{|x|x*f} becasue f and o
should be able to change, and not effect it.
Thanks alot guys, youve been a great help!
I realize I have lots more to learn! :)

Joey
http://eachmapinject.com
(c="amBleQ==\n").unpack("#{c[1..1]}*").pop
This topic is locked and can not be replied to.