We don't bite newbies here... much PerlMonks

### Re: The golf course looks great, my swing feels good, I like my chances (Part I)

by eyepopslikeamosquito (Chancellor)
 on Jun 04, 2009 at 08:50 UTC ( #768354=note: print w/replies, xml ) Need Help??

After pondering my improved Python algorithm for a while, I was surprised to shave two further strokes from my previous best legitimate Perl solution with this 56 stroker:

```\$\+=(\$n="1E@-"%9995)-\$\%\$n*2while\$/=\1,IXCMVLD=~<>;print
Notice that this solution is a strange hybrid of regex and magic formula -- and so perhaps proves both Mtv's law of golf ("regexes always win") and eyepopslikeamosquito's law ("magic formulae always win"). :) Also unusual is that while trumps for on this occasion. The complete list of new Perl solutions I've found since my original post are:
```\$\+=(\$n="1E@-"%9995)-\$\%\$n*2while\$/=\1,IXCMVLD=~<>;print
\$\+=\$n-2*\$n%(\$n="1E@-"%9995)while\$/=\1,IXCMVLD=~<>;print
\$\+=\$n-2*\$\%(\$n="1E@-"%9995)while\$/=\1,IXCMVLD=~<>;print
\$\+=(\$n=IXCMVLD=~\$_*"1E@-"%9995)-\$\%\$n*2for<>=~/./g;print
\$\+=\$'-\$\%\$'*2while\$/=\1,I1V5X10L50C100D500M1e3=~<>;print
\$\+=I1V5X10L50C100D500M1e3=~\$_*\$'-\$\%\$'*2for<>=~/./g;print
\$\+=(\$n=(IXCMVLD=~\$_."E@-")%9995)-\$\%\$n*2for<>=~/./g;print
\$\+=(\$n=VLD=~\$_*5+IXCM=~\$_."E@-")-\$\%\$n*2for<>=~/./g;print
\$\+=M999D499C99L49X9V4I=~\$_+\$'-2*\$\%(\$'+1)for<>=~/./g;print
\$\+=(\$n=10**index(IXCMVLD,\$_)%9995)-\$\%\$n*2for<>=~/./g;print
\$\+=s//IXCMVLD=~\$&."E@-%9995"/ee*\$_-\$\%\$_*2for<>=~/./g;print
D6L5V4M3C2X1=~\$_,\$\+=(\$n="1E\$'"%9995)-\$\%\$n*2for<>=~/./g;print
\$\+=\$n-2*\$n%(\$n=uppp&7**(9671487%ord()/15)x1)for<>=~/./g;print
# update: some more after finding getc
\$\+=(\$n=1+substr'004999',"@-",3)-\$\%\$n*2while VLDMCXI=~getc;print
\$\+=\$'-\$\%\$'*2while I1V5X10L50C100D500M1e3=~getc;print
\$\+=(\$n="1E@-"%9995)-\$\%\$n*2while IXCMVLD=~getc;print

Update: An improved 74 stroke symref-based solution, after "finding" getc (see below):

```\$b=++\$I;\$\$_=\$b*=\$^F^=7for V,X,L,C,D,M;
\$\+=\$n-\$\%\$n*2while\$n=\${+getc};print
And here's a quirky 83-stroke PHP version:
```<?for(\$M=2*\$D=5*\$C=2*\$L=5*\$X=2*\$V=5*\$I=1;\$n=\${fgetc(STDIN)};\$t+=\$n-2*\$
+t%\$n)?><?=\$t;

Replies are listed 'Best First'.
Re^2: The golf course looks great, my swing feels good, I like my chances (Part I)
by eyepopslikeamosquito (Chancellor) on Aug 07, 2010 at 01:01 UTC

I was relaxing the other day, browsing through some old golfs at shinh's golf site, when I noticed ySas' solution to the "add_some_brainf..._code" game called the getc function. I thought, "that's odd-looking Ruby code with all those \$ characters in it", when it dawned on me that it was not Ruby code at all, but Perl! Perl has a getc function? Really? I'm deeply embarrassed to admit, despite having used Perl heavily for 10 years, I did not know that. After picking myself up off the floor, I realised I'd made an appalling oversight in that getc could shorten my previous best Roman Perl solution like so:

```\$\+=(\$n="1E@-"%9995)-\$\%\$n*2while IXCMVLD=~getc;print
53 strokes! What I find interesting here is that while is two strokes longer than for, and getc two strokes longer than <>, yet combining these two longer constructs produces a solution four strokes shorter than:
```\$\+=(\$n=IXCMVLD=~\$_*"1E@-"%9995)-\$\%\$n*2for<>=~/./g;print

During the opening months of this competition, Perl led Ruby by 60 strokes to 73. At that stage, I thought it "impossible" for Ruby to ever catch Perl. To my great surprise, I whittled away at my Ruby solution until (to my dismay) it overtook my shortest Perl solution. After more than two years of play, the situation was reversed, with Ruby leading Perl by 53 to 58. Now I was sure that Perl had no chance of ever catching Ruby. As you might expect then, I'm delighted that the shortest known Perl and Ruby solutions are now tied at 53 strokes:

```\$\+=(\$n="1E@-"%9995)-\$\%\$n*2while IXCMVLD=~getc;print
n=1;\$.+=n/2-n%n=10**(494254%C/9)%4999while C=getc;p\$.
And they both use the getc function. :-)

Update: a couple of Ruby equivalents of the Perl solution:

```t=0;t+=(n=10**I%9995)-t%n*2while I="IXCMVLD"=~/#{getc.chr}/;p t
t=0;t+=(n=10**I%9995)-t%n*2while I="IXCMVLD".index(getc);p t

Create A New User
Node Status?
node history
Node Type: note [id://768354]
help
Chatterbox?
 [karlgoethebier]: jedikaiti: i just wanted to ask ;-) [jedikaiti]: :-D

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2017-07-20 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (306 votes). Check out past polls.