Saari Development

Ruby: Grid Computing Golf

Posted in code, golf, ruby by imsaar on January 8th, 2008

My first attempt at really playing code golf, that is solving a problem in minimum characters of code and I did not do very well (200 character compared to the best 43 perl or 63 ruby characters).
http://codegolf.com/grid-computing


m = []
r = []
c = Array.new(10, 0)
0.upto(9) {|n|m<<gets.chomp.split.map {|x|x.to_i};r<<m[n].inject(0){|s,e| s += e}}
0.upto(9) {|x|m.each_with_index{|v,z|c[x] += v[x]}}
puts [r.max, c.max].max

Update:
I further squeezed some white-space out of the code and it is starting to look uglier (now it is 184 characters). I am not sure I like the code golf concept although I like to solve coding problems.


m=[]
r=[]
c=Array.new(10,0)
0.upto(9){|n|m<<gets.chomp.split.map{|x|x.to_i};r<<m[n].inject(0){|s,e|s+=e}}
0.upto(9){|x|m.each_with_index{|v,z|c[x]+=v[x]}}
puts [r.max,c.max].max