# 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 D. 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

On 27 Sep 2006, at 13:31, Carl D. 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.

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 Sep 27, 2006, at 4:40 PM, Paul R. 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.

James Edward G. II

James G. wrote:

On Sep 27, 2006, at 4:40 PM, Paul R. 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.

James Edward G. II

Lost a few hours as well 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 Onto something more productive…

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

What is the problem?

Carl D. wrote:

[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)
nm==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

On Sep 27, 2006, at 8:26 AM, Michael G. 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

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 G. II

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

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

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

James Edward G. II

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

Not very impressive, but may give others some ideas.

def f(n,m,k,o)
nm==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
nm==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|"%#{"#{xy}".size}s"%t}" "}

James Edward G. II

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

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
nm==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|"%#{"#{xy}".size}s"%t}" "}

One byte larger, but supporting the quiz interface:

f=proc{|n,m,k,o|nm==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 G. II

On 9/28/06, James Edward G. II [email protected] wrote:

James Edward G. 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

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=ak/(2k+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/29/06, Paul R. [email protected] wrote:

2 tries to pass? It doesn’t work for all cases then! Cheat!

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

bloated solutions that are elegant in their own way.
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 29 Sep 2006, at 15:08, Sander L. 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

Hmmmm… letter of the rules, rather than the spirit. I can tell
you’ve never played cricket.

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.

“Sander L.” [email protected] 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.

Daniel M. 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:

101000 == ?d500

‘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

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.

I think this would be a great feature.

James

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).

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

\$/=’ ’
b,a=\$<.map{|a|a.to_i}
r=Array.new(a){[]}
c="#{ab}".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
\$/}

On 28 Sep 2006, at 23:03, Sander L. wrote:

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
And yes this works, although it needed 2 tries to pass.

2 tries to pass? It doesn’t work for all cases then! Cheat!

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=ak/(2k+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.

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.