Golfing a signature


#1

Hello people of Ruby-Talk!
My first email to the list, to any list for that matter.
Saying Hi and introducing myself, I’m joey AKA j`ey from IRC.

Asking for a bit of help, I have the following code at
http://www.eachmapinject.com/code/game_of_life_golf.rb which is an
implemintation of the Game of Life
I would like to be able to have it as a signature, you know 80*4, but
its 34
characters over.
Any suggestions on golfing/shortening it anymore?

Thanks
j`ey
eachmapinject.com


#2

Joey wrote:

Any suggestions on golfing/shortening it anymore?

Do you have a non-obfuscated version you could share? Do that and I’m
certain you’ll see some impressive golf.

–Steve


#3

On 5/3/06, Joey removed_email_address@domain.invalid wrote:

Asking for a bit of help, I have the following code at
http://www.eachmapinject.com/code/game_of_life_golf.rb which is an
implemintation of the Game of Life
I would like to be able to have it as a signature, you know 80*4, but its 34
characters over.
Any suggestions on golfing/shortening it anymore?

I’ve taken it down by 15 (to 347 not including newlines) trivially:

  • -7: inline values of L and s
  • -3: add a var, N, which is nil, and replace all () with u
  • -3: remove redundant ,() from initialization sequence (a,b,c=1,2; c
    #=> nil)
  • -2: replace " " with f on line 2
  • 0: uppercase constants O and F

O,F,N,c="#","
“;b=([N]20).map{(0…20).map{(rand<0.3)?O:F}};$><<"\e[2J";until
c==b;$><<"\e[H"<<b.map{|x|x
F}*”\n";c=b.map{|z|z.dup};b.each_index{|i|b[i].each_index{
|e|n=(q,z=-1…1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-=[[0,0]]).map{|x|c[i+x[0]][e+x[1]]rescue
N}-[F,N]).size;(b[i][e]==O)?(n>3||n<2)?b[i][e]=F:N:(n==3)?b[i][e]=O:N}};sleep
0.2;end

Cheers,
Dave


#4

On 5/3/06, Roy S. removed_email_address@domain.invalid wrote:

This doesn’t work with my Ruby one-click 1.8.2:

SyntaxError: compile error

Sorry about that. I didn’t test it because I’m on Windows and the ANSI
escape codes don’t work.

Apparently the problem is that (foo)?bar:baz is parsed as (foo) ? bar
: baz (a trigraph) but (foo)?Bar:baz is trying to see :baz as a
symbol. I think this is a (minor) parser bug - it could easily be
parsed the same as (foo)?bar:baz.

Anyway, just replace all O,F,N with o,f,u respectively, and it works
like a charm. Seriously this time.

Cheers,
Dave


#5

Dave B. wrote:

c==b;$><<"\e[H"<<b.map{|x|xF}"\n";c=b.map{|z|z.dup};b.each_index{|i|b[i].each_index{

|e|n=(q,z=-1…1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-=[[0,0]]).map{|x|c[i+x[0]][e+x[1]]rescue

N}-[F,N]).size;(b[i][e]==O)?(n>3||n<2)?b[i][e]=F:N:(n==3)?b[i][e]=O:N}};sleep

0.2;end

This doesn’t work with my Ruby one-click 1.8.2:

SyntaxError: compile error
(irb):1: syntax error
O,F,N,c="#","
“;b=([N]*20).map{(0…20).map{(rand<0.3)?O:F}};$><<”\e[2J";until
^
(irb):4: syntax error
N}-[F,N]).size;(b[i][e]==O)?(n>3||n<2)?b[i][e]=F:N:(n==3)?b[i][e]=O:N}};sleep
^
(irb):4: syntax error
N}-[F,N]).size;(b[i][e]==O)?(n>3||n<2)?b[i][e]=F:N:(n==3)?b[i][e]=O:N}};sleep
^
from (irb):5


#6

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


#7

Dave B. wrote:

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

You know what I mean. That first newline is meant to be a space in
quotes (" "). Can I blame my mail agent for this?

Cheers,
Dave


#8

Thanks! This is very nice of you!


#9

de-obfuscated it a bit to understand it :slight_smile:

L = 0.3 # % of #
s = 20 # size
o = “#”
f = " "
c = nil
b = ([nil]*s).map{ (0…s).map{ (rand < L) ? o : f } };

$> << “\e[2J”;

until c==b;
$> << “\e[H” << b.map{|x|x * " "} * “\n”

c = b.map{|z| z.dup };
b.each_index{|y|
b[y].each_index{|x|
q = -1…1
z = []
q.map{|dx| q.map{|dy| z << [dx,dy]}};
z -= [[0,0]]
n = ( z.map{|dxy| c[y + dxy[0]][x + dxy[1]] rescue nil } - [f,nil]
).size
if b[y][x]==o
b[y][x] = f if (n>3||n<2)
else
b[y][x] = o if n==3
end
}
}
sleep 0.2
end

some ways i found to decrease the size:
remove “-[[0,0]]” and change the condition to (n>4||n<3) to account for
the +1
“rescue f” over “rescue ()”, and -[f] over -[f,nil]

possible bug here though:
c[i+x[0]][e+x[1]]
take i=e=0 and x=[-1,-1], this results in c[-1][-1] which is
bottom-right, i don’t think this is what you want (unless i
misunderstood the ‘game’).


#10

I now have my signature! It is 305 charcters long, wit help from Florian
and
Dave!
you can check it out at
http://code.eachmapinject.com/game_of_life_golf.rb
or just below my name!

Joey
http://eachmapinject.com

s,o,f,c,u=20,"#","
“;b=(z=0…s).map{z.map{(rand<0.3)?o:f}};$><<”\e[2J";until

c==b;$><<"\e[H"<<b.map{|x|x*"
“}*”\n";c=b.map{|z|z.dup};s.times{|i|s.times{|
e|n=(q,z=-1…1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-=[[0,0]]).map{|x|c[i+x[0]][
e+x[1]]rescue u}-[f,u]).size;b[i][e]=(n==2?b[i][e]:n==3?o:f)}};sleep
0.2;end


#11

Sander L. wrote:

de-obfuscated it a bit to understand it :slight_smile:

Here’s what I used:

o = “#”
f = " "
c = nil # copy of b

u = nil # alias for nil

b # main grid

b = ([nil] * 20).map { (0…20).map { (rand < 0.3) ? o : f } }

$> << “\e[2J” # this line is not strictly necessary (11 bytes)

until c == b
$> << “\e[H” << b.map {|x| x * f } * “\n”
c = b.map {|x| x.dup }
20.times {|i| # -4: b.each_index => 20.times
20.times {|e| # -7: b[i].each_index => 20.times
n = (
q, z = -1…1, []
q.map {|x| q.map {|y| z << [x, y] } }
(z - [[0, 0]]).map {|x| # -1: -= => -
c[i + x[0]][e + x[1]] rescue nil
} - [f, nil]
).size
b[i][e]=(n==2?b[i][e]:n==3?o:f) # -23
}
}
sleep 0.1
end

some ways i found to decrease the size:
remove “-[[0,0]]” and change the condition to (n>4||n<3) to account for

Good one. Can still do that to my result. -8

the +1
“rescue f” over “rescue ()”, and -[f] over -[f,nil]

-2

possible bug here though:
c[i+x[0]][e+x[1]]
take i=e=0 and x=[-1,-1], this results in c[-1][-1] which is
bottom-right, i don’t think this is what you want (unless i
misunderstood the ‘game’).

It’s sort of a feature - it’s half a wrap-rule. Here’s both halves and
minus another character:

< c[i+x[0]][e+x[1]] rescue f

c[(i+x[0])%s][(e+x[1])%s]

That’s assuming you put s=20 back in, which costs you that character
right back again, but is worth it, because that’s now 6 esses.

Cheers,
Dave