[ANN] Code Golf Challenge : Oblongular Number Spirals

Hi all,

For those who found the 1,000 Digits of Pi problem too daunting, here's something a little simpler for you. The new Code Golf[1] challenge involves you having to create a number spiral such as :

    1 2 3
   10 11 4
    9 12 5
    8 7 6

and

    1 2 3 4 5
   18 19 20 21 6
   17 28 29 22 7
   16 27 30 23 8
   15 26 25 24 9
   14 13 12 11 10

Seems pretty simple, huh? The URL to the full challenge description is

   http://codegolf.com/oblongular-number-spirals

I found it more difficult than I first thought it would be, but the code was a lot of fun to write. At the time of writing the best Ruby entry is 169 bytes. Can you beat it?

Regards,
Carl.

[1] http://codegolf.com/ - "allows you to show off your code-fu by trying to solve coding problems using the least number of keystrokes."

Carl Drinkwater wrote:

Hi all,

For those who found the 1,000 Digits of Pi problem too daunting, here's
something a little simpler for you. The new Code Golf[1] challenge
involves you having to create a number spiral such as :

    1 2 3
   10 11 4
    9 12 5
    8 7 6

and

    1 2 3 4 5
   18 19 20 21 6
   17 28 29 22 7
   16 27 30 23 8
   15 26 25 24 9
   14 13 12 11 10

Seems pretty simple, huh? The URL to the full challenge description is

   http://codegolf.com/oblongular-number-spirals

I found it more difficult than I first thought it would be, but the code
was a lot of fun to write. At the time of writing the best Ruby entry
is 169 bytes. Can you beat it?

Regards,
Carl.

[1] http://codegolf.com/ - "allows you to show off your code-fu by
trying to solve coding problems using the least number of keystrokes."

I'm sure I'll give it a whirl on Sunday when I have a bit of spare
time... but I'd almost (not quite) be willing to pay money to find out
what you did use 8 less bytes than me on the
http://codegolf.com/vigenere-cipher

···

--
Posted via http://www.ruby-forum.com/\.

Seems pretty simple, huh? The URL to the full challenge description is

[snip]

I've just spent since 4pm (it's now 22:40 local time) working on this. :smiley:

I've managed to get it down to 426 bytes which is *rubbish* - 20/21 overall, 11/12 for Ruby - but I'm now going to actually, you know, get some other stuff done. I give in. There are clearly better men than I on that thing.

I found it more difficult than I first thought it would be, but the code was a lot of fun to write. At the time of writing the best Ruby entry is 169 bytes. Can you beat it?

This is now down to 128. I can only assume there is a better formula out there than the one I'm using - I built my formula off my own back. If there is an easier way to calculate it, feel free to shout me off-list so I can take another stab at it.

···

On 27 Sep 2006, at 13:31, Carl Drinkwater wrote:

--
Paul

Carl Drinkwater wrote:

Hi all,

For those who found the 1,000 Digits of Pi problem too daunting, here's something a little simpler for you. The new Code Golf[1] challenge involves you having to create a number spiral such as :

   1 2 3
  10 11 4
   9 12 5
   8 7 6

and

   1 2 3 4 5
  18 19 20 21 6
  17 28 29 22 7
  16 27 30 23 8
  15 26 25 24 9
  14 13 12 11 10

Seems pretty simple, huh? The URL to the full challenge description is

  http://codegolf.com/oblongular-number-spirals

I found it more difficult than I first thought it would be, but the code was a lot of fun to write. At the time of writing the best Ruby entry is 169 bytes. Can you beat it?

Regards,
Carl.

[1] http://codegolf.com/ - "allows you to show off your code-fu by trying to solve coding problems using the least number of keystrokes."

I finally found some time for golfing again. Here is my current solution.
Not very impressive, but may give others some ideas.

def f(n,m,k,o)
n*m==0?:(k>0?f(n-1,m-1,m-k-1,n+m-1+o).reverse<< n+k+o:(o+1..n+o).to_a)
end
x,y=ARGV.map{|t|t.to_i}
y.times{|i|puts f(x,y,i,0).map{|t|t.to_s.rjust((x*y).to_s.size)}*" "}

regards,

Michael

···

--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com

What is the problem?

···

On 9/27/06, Paul Robinson <paul@iconoplex.co.uk> wrote:

On 27 Sep 2006, at 13:31, Carl Drinkwater wrote:

> Seems pretty simple, huh? The URL to the full challenge
> description is

[snip]

I've just spent since 4pm (it's now 22:40 local time) working on
this. :smiley:

I've managed to get it down to 426 bytes which is *rubbish* - 20/21
overall, 11/12 for Ruby - but I'm now going to actually, you know,
get some other stuff done. I give in. There are clearly better men
than I on that thing.

> I found it more difficult than I first thought it would be, but the
> code was a lot of fun to write. At the time of writing the best
> Ruby entry is 169 bytes. Can you beat it?

This is now down to 128. I can only assume there is a better formula
out there than the one I'm using - I built my formula off my own
back. If there is an easier way to calculate it, feel free to shout
me off-list so I can take another stab at it.

--
Paul

--
Amos King
USPS
Programmer/Analyst
St. Louis, MO

It is a hell of a problem. I too lost a couple of hours to it this afternoon and still have nothing decent to show. :wink:

James Edward Gray II

···

On Sep 27, 2006, at 4:40 PM, Paul Robinson wrote:

On 27 Sep 2006, at 13:31, Carl Drinkwater wrote:

Seems pretty simple, huh? The URL to the full challenge description is

[snip]

I've just spent since 4pm (it's now 22:40 local time) working on this. :smiley:

I've managed to get it down to 426 bytes which is *rubbish* - 20/21 overall, 11/12 for Ruby - but I'm now going to actually, you know, get some other stuff done. I give in. There are clearly better men than I on that thing.

I'm curious how either of you did it! I struggled and struggled only to get stuck at 94 bytes:

k,i=gets,-1
gets.scan(/./){$><<$&.tr("A-Z","#{(c=k[(i+=1)%(k.size-1)]).chr}-ZA-#{(c-1).chr}")}

James Edward Gray II

···

On Sep 27, 2006, at 8:26 AM, Michael Greenly wrote:

I'd almost (not quite) be willing to pay money to find out
what you did use 8 less bytes than me on the
http://codegolf.com/vigenere-cipher

Does that work? I thought the script received the numbers on STDIN.

James Edward Gray II

···

On Sep 28, 2006, at 1:23 AM, Michael Ulm wrote:

x,y=ARGV.map{|t|t.to_i}

I haven't figured out completely how it works yet, but I did shave some bytes off of it:

def f n,m,k,o
n*m==0?:(k>0?f(n-1,m-1,m-k-1,n+m-1+o).reverse<< n+k+o:(o+1..n+o).to_a)
end
x,y=$*.map{|t|t.to_i}
y.times{|i|puts f(x,y,i,0).map{|t|"%#{"#{x*y}".size}s"%t}*" "}

James Edward Gray II

···

On Sep 28, 2006, at 1:23 AM, Michael Ulm wrote:

Not very impressive, but may give others some ideas.

def f(n,m,k,o)
n*m==0?:(k>0?f(n-1,m-1,m-k-1,n+m-1+o).reverse<< n+k+o:(o+1..n+o).to_a)
end
x,y=ARGV.map{|t|t.to_i}
y.times{|i|puts f(x,y,i,0).map{|t|t.to_s.rjust((x*y).to_s.size)}*" "}

James Gray wrote:

···

On Sep 27, 2006, at 4:40 PM, Paul Robinson wrote:

I've managed to get it down to 426 bytes which is *rubbish* - 20/21
overall, 11/12 for Ruby - but I'm now going to actually, you know,
get some other stuff done. I give in. There are clearly better men
than I on that thing.

It is a hell of a problem. I too lost a couple of hours to it this
afternoon and still have nothing decent to show. :wink:

James Edward Gray II

Lost a few hours as well :slight_smile: I was able to get it down to 273 bytes.
Not so sure my algorithm was that great, but it got the job done. I'm
going to have to stay away from this website.. encourages me to
procrastinate :wink: Onto something more productive...

P.S. Thanks for the challenge though, very interesting!

--
Posted via http://www.ruby-forum.com/\.

One byte larger, but supporting the quiz interface:

f=proc{|n,m,k,o|n*m==0?:(k>0?f[n-1,m-1,m-k-1,n+m-1+o].reverse<<n+k+o:[*o+1..n+o])}
x,y=gets(" ").to_i,gets.to_i
y.times{|i|puts f[x,y,i,0].map{|t|"%#{"#{x*y}".size}s"%t}*" "}

(3 lines--first line was likely wrapped)

James Edward Gray II

···

On Sep 28, 2006, at 10:49 AM, James Edward Gray II wrote:

On Sep 28, 2006, at 1:23 AM, Michael Ulm wrote:

Not very impressive, but may give others some ideas.

def f(n,m,k,o)
n*m==0?:(k>0?f(n-1,m-1,m-k-1,n+m-1+o).reverse<< n+k+o:(o+1..n+o).to_a)
end
x,y=ARGV.map{|t|t.to_i}
y.times{|i|puts f(x,y,i,0).map{|t|t.to_s.rjust((x*y).to_s.size)}*" "}

I haven't figured out completely how it works yet, but I did shave some bytes off of it:

def f n,m,k,o
n*m==0?:(k>0?f(n-1,m-1,m-k-1,n+m-1+o).reverse<< n+k+o:(o+1..n+o).to_a)
end
x,y=$*.map{|t|t.to_i}
y.times{|i|puts f(x,y,i,0).map{|t|"%#{"#{x*y}".size}s"%t}*" "}

f=proc{|n,m,k,o|n*m<1?:k>0?f[n-1,m-=1,m-k,n+m+o].reverse<<n+k+o-1:[*o...n+o]}
x,y=gets(" ").to_i,gets.to_i
y.times{|i|puts f[x,y,i,1].map{|t|"%3s"%t}*" "}

Few bytes off the first line, but the largest gain is from the last line :slight_smile:
And yes this works, although it needed 2 tries to pass.

Also, this was my 63 byte solution to the pi problem, in case anybody
is interested.
a=b=10**1000;4000.downto(1){|k|a=a*k/(2*k+1)+2*b};print"3.",a%b

btw: what's up with the ultra-small PHP solutions? pi in 34 bytes and
this one in 113? Is there some network/filesystem call going on there?

···

On 9/28/06, James Edward Gray II <james@grayproductions.net> wrote:

On Sep 28, 2006, at 10:49 AM, James Edward Gray II wrote:
One byte larger, but supporting the quiz interface:

f=proc{|n,m,k,o|n*m==0?:(k>0?f[n-1,m-1,m-k-1,n+m-1+o].reverse<<n+k
+o:[*o+1..n+o])}
x,y=gets(" ").to_i,gets.to_i
y.times{|i|puts f[x,y,i,0].map{|t|"%#{"#{x*y}".size}s"%t}*" "}

(3 lines--first line was likely wrapped)

James Edward Gray II

f=proc{|n,m,k,o|n*m<1?:k>0?f[n-1,m-=1,m-k,n+m+o].reverse<<n+k+o-1:[*o...n+o]}
x,y=gets(" ").to_i,gets.to_i
y.times{|i|puts f[x,y,i,1].map{|t|"%3s"%t}*" "}

Few bytes off the first line, but the largest gain is from the last line :slight_smile:
And yes this works, although it needed 2 tries to pass.

2 tries to pass? It doesn't work for all cases then! Cheat! :slight_smile:

My solution works for all cases but it a bloated 428 bytes when the linewraps are gone. It's on my blog here:

http://vagueware.com/2006/9/29/code-golf

Also, this was my 63 byte solution to the pi problem, in case anybody
is interested.
a=b=10**1000;4000.downto(1){|k|a=a*k/(2*k+1)+2*b};print"3.",a%b

Hmmmm. I think I can see a way of getting a few bytes off that.

Thanks for the starter. :slight_smile:

btw: what's up with the ultra-small PHP solutions? pi in 34 bytes and
this one in 113? Is there some network/filesystem call going on there?

You can't 'include' anything, but PHP comes bundled with a huge standard API compared to the standard Ruby stack. This makes it bloated and horrible to work with (especially as how you call it and what you get back is inconsistent across the whole thing), but it does mean they can do some interesting things when playing Code Golf.

It's been ages since I did any PHP, but I can immediately think how to do your pi solution in less bytes in PHP, and I think there's a way of using the API to do it in even less than that. As it is, I'm Golfing to improve my Ruby kung-fu, not to win, so I'm happy with my bloated solutions that are elegant in their own way. :slight_smile:

···

On 28 Sep 2006, at 23:03, Sander Land wrote:

--
Paul

2 tries to pass? It doesn't work for all cases then! Cheat! :slight_smile:

Only for x*y >= 100, which is true for the majority of the test cases
(2/3 or so), and it's only tested three times :slight_smile:

You can't 'include' anything, but PHP comes bundled with a huge
standard API compared to the standard Ruby stack. This makes it
bloated and horrible to work with (especially as how you call it and
what you get back is inconsistent across the whole thing), but it
does mean they can do some interesting things when playing Code Golf.

It's been ages since I did any PHP, but I can immediately think how
to do your pi solution in less bytes in PHP, and I think there's a
way of using the API to do it in even less than that. As it is, I'm
Golfing to improve my Ruby kung-fu, not to win, so I'm happy with my
bloated solutions that are elegant in their own way. :slight_smile:

I'm familiar with PHP, but don't know any 'give me pi to n digits'
function. I thought it was more likely there was function in there
somewhere which could be used to cheat.
The PHP solutions aren't there anymore, so I guess they were removed
for cheating.

As for getting bytes off the pi program, did you actually manage to do
this or do you only think it's possible? For me, when thinking of a
way to make something shorter, the result often turns out to be the
same size (or even larger) than the original.

···

On 9/29/06, Paul Robinson <paul@iconoplex.co.uk> wrote:

Only for x*y >= 100, which is true for the majority of the test cases
(2/3 or so), and it's only tested three times :slight_smile:

Hmmmm... letter of the rules, rather than the spirit. I can tell you've never played cricket. :slight_smile:

I'm familiar with PHP, but don't know any 'give me pi to n digits'
function.

No, but there is a built-in API for getting stuff from websites, which is why our ruby attempts failed.

As for getting bytes off the pi program, did you actually manage to do
this or do you only think it's possible? For me, when thinking of a
way to make something shorter, the result often turns out to be the
same size (or even larger) than the original.

It was just an idea, but I will be playing golf with it over the weekend, and will let you know. :slight_smile:

···

On 29 Sep 2006, at 15:08, Sander Land wrote:

"Sander Land" <sander.land@gmail.com> writes:

As for getting bytes off the pi program, did you actually manage to do
this or do you only think it's possible? For me, when thinking of a
way to make something shorter, the result often turns out to be the
same size (or even larger) than the original.

Part of the problem here is that there isn't a long tradition of ruby
golf competitions where you can see the result after some point. (as
there is with perl) Combine this with the state of readable ruby
documentation (where is the "alias" built-in documented? where's the
list of all the global variables?) and I can see why rubyists might
not be able to golf as well as perl fans, even given the differences
in the languages.

I'd like it if more people would post some hints of how they got their
code size down; numbers only are good for a competition, but there
really needs to be some chance to see how people did what they did. I
still wanna know how the brainfuck interpreter got to be that small.

···

--
s=%q( Daniel Martin -- martin@snowplow.org
       puts "s=%q(#{s})",s.map{|i|i}[1] )
       puts "s=%q(#{s})",s.map{|i|i}[1]

Daniel Martin wrote:

I'd like it if more people would post some hints of how they got their
code size down; numbers only are good for a competition, but there
really needs to be some chance to see how people did what they did. I

For the pi problem:

10**1000 == ?d**500

'downto' is to long

Oblongular Number Spirals:

well, recursion and rotation are the keys

I think the whole point of codegolf.com would
vanish if you paste code of one of the top tens
for each problem.

Perhaps they should open the code for all
challenges older than 8 weeks or so - but than
they have to freeze the scores at this point.

cheers

Simon

I think this would be a great feature.

James

···

On Sep 29, 2006, at 11:57 AM, Simon Kröger wrote:

Perhaps they should open the code for all
challenges older than 8 weeks or so - but than
they have to freeze the scores at this point.

Oblongular Number Spirals:

well, recursion and rotation are the keys

Yup, my solution has the recursion but is bulked by iterating and using a case statement.

I think the whole point of codegolf.com would
vanish if you paste code of one of the top tens
for each problem.

Well, I'm happy to put my solutions out there into the wild for people to prod with. It's only a little game after all. Now, if there were prizes on offer...

Perhaps they should open the code for all
challenges older than 8 weeks or so - but than
they have to freeze the scores at this point.

I like the fact that I can go back and compete in older challenges if I want. I really like it as it is.

Perhaps somebody could set up an OpenCodeGolf.com where that happens for people who want a different way to play, with different challenges (so as not to spoil the ones on codegolf.com).

···

On 29 Sep 2006, at 17:57, Simon Kröger wrote:

non-recursive 170 bytes: (around and around and around we go)

$/=' '
b,a=$<.map{|a|a.to_i}
r=Array.new(a){}
c="#{a*b}".size
x=k=-1
i=y=j=0
a.times{r[y+=k][x+=j]="#{i+=1}".rjust c}while(a,b,j,k=b,a-1,-k,j;a>0)
r.each{|o|puts o*$/}

Paul Robinson wrote:

···

On 29 Sep 2006, at 17:57, Simon Kröger wrote:

> Oblongular Number Spirals:
>
> well, recursion and rotation are the keys

Yup, my solution has the recursion but is bulked by iterating and
using a case statement.

> I think the whole point of codegolf.com would
> vanish if you paste code of one of the top tens
> for each problem.

Well, I'm happy to put my solutions out there into the wild for
people to prod with. It's only a little game after all. Now, if there
were prizes on offer...

> Perhaps they should open the code for all
> challenges older than 8 weeks or so - but than
> they have to freeze the scores at this point.

I like the fact that I can go back and compete in older challenges if
I want. I really like it as it is.

Perhaps somebody could set up an OpenCodeGolf.com where that happens
for people who want a different way to play, with different
challenges (so as not to spoil the ones on codegolf.com).