[ANN] Code Golf Challenge : Oblongular Number Spirals

C

Carl Drinkwater

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."
 
M

Michael Greenly

Carl said:
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
 
P

Paul Robinson

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

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

Amos King

What is the problem?

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

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

James Edward Gray II

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

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 Gray II
 
A

Aaron Pfeifer

James said:
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 Gray 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!
 
M

Michael Ulm

Carl said:
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: (e-mail address removed)
Visit our Website: www.isis-papyrus.com
 
J

James Edward Gray II

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 Gray II
 
J

James Edward Gray II

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}*" "}

James Edward Gray II
 
J

James Edward Gray II

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}*" "}

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
 
S

Sander Land

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 :)
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?
 
P

Paul Robinson

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=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. :)
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. :)
 
S

Sander Land

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

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

Paul Robinson

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

Daniel Martin

Sander Land said:
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

Simon Kröger

Daniel said:
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
 
J

James Edward Gray II

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=20=
 
P

Paul Robinson

Oblongular Number Spirals:

well, recursion and rotation are the keys

Yup, my solution has the recursion but is bulked by iterating and =20
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 =20
people to prod with. It's only a little game after all. Now, if there =20=

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 =20=

I want. I really like it as it is.

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

Noah Easterly

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*$/}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top