[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] Code Golf Challenge : Oblongular Number Spirals

Carl Drinkwater

9/27/2006 12:32:00 PM

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://cod...oblongular-numb...

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://cod... - "allows you to show off your code-fu by
trying to solve coding problems using the least number of keystrokes."

19 Answers

Michael Greenly

9/27/2006 1:26:00 PM

0

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://cod...oblongular-numb...
>
> 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://cod... - "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://cod...vigenere-cipher

--
Posted via http://www.ruby-....

Paul Robinson

9/27/2006 9:41:00 PM

0

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

--
Paul

Amos King

9/27/2006 9:45:00 PM

0

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


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

James Gray

9/27/2006 10:09:00 PM

0

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

Aaron Pfeifer

9/28/2006 3:57:00 AM

0

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

--
Posted via http://www.ruby-....

Michael Ulm

9/28/2006 6:24:00 AM

0

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://cod...oblongular-numb...
>
> 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://cod... - "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

James Gray

9/28/2006 3:38:00 PM

0

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/vigen...

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

James Gray

9/28/2006 3:42:00 PM

0

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

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

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

James Edward Gray II


James Gray

9/28/2006 3:50:00 PM

0

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

James Edward Gray II

James Gray

9/28/2006 4:11:00 PM

0

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

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