Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: (Golf) Let's go bowling

by rchiav (Deacon)
on Aug 09, 2001 at 01:41 UTC ( #103247=note: print w/ replies, xml ) Need Help??


in reply to (Golf) Let's go bowling

Well this is my first golf, and I was pretty proud of it until I saw tilly's post. But then again, when doesn't he have the lowest score? :)

Anyway.. I'm sure this can be improved..

I'm not sure on the counting rules, but if it's everything inside the sub, not counting whitespace, it's 150

#!/usr/bin/perl $|=1; my @b = ('5', '/', '6', '3', 'X', 'X', '7', '0', '4', '3', 'X', '4', ' +/', '8', '1', '3', '/', '6'); $r = a(@b); print "score is $r\n"; sub a { map {s/(\/|X)/10/} @_ ; while ($_ = shift) { if (/^.$/) { $t += ($_[0] == 10 ? $_[1] + shift : $_ + shift) }else{ $t+= ($_[0] < 10 && $_[1] == 10 ? 20 : 10 + $_[0] + $_[1]) } last if ++$f ==10 } $t }
update: original was missing the pound symbol on the first line.. nothing to do with the sub.. just mised it in the copy-n-paste. Also, put the exact array from the example in case someone thought it was giving the wrong answer

update II The sub inluding nesisairy whitespace is now 150..
update III removed a space I didn't think I could and removed the closing } from the count... 148

sub a { map{s#(/|X)#10#}@_;while($_=shift){if(/10/){$t+=($_[0]<10&&$_[1]==10?2 +0:10+$_[0]+$_[1])}else{$t+=($_[0]==10?$_[1]+shift:$_+shift)}last if++ +$f==10}$t }
update III same principle, just trimming it down..removed while loop and if statements.. 131
sub a { map{s#(/|X)#10#}@_;for(1..10){$_=shift;/10/?{$t+=($_[0]<10&&$_[1]==10? +20:10+$_[0]+$_[1])}:{$t+=($_[0]==10?$_[1]+shift:$_+shift)}}$t }
yet anotherupdate IV: Got it down to 123. But as Tilly pointed out, it doesn't look like this runs on 5.005_03.. so I don't know what it's worth then. I have since tested this and it works on AIX with perl 5.005_03
sub a { map{s#[/X]#10#}@_;for(0..9){$_=shift;/10/?$t+=$_[0]<10&&$_[1]==10?20:1 +0+$_[0]+$_[1]:($t+=$_[0]==10?$_[1]+shift:$_+shift)}$t }
-Rich


Comment on Re: (Golf) Let's go bowling
Select or Download Code
Replies are listed 'Best First'.
Re: Re: (Golf) Let's go bowling
by dragonchild (Archbishop) on Aug 09, 2001 at 17:56 UTC
    I'm still choking on the map {} @_; ... I've got use 5.6.0; as my first line, so I know I'm not running 5.00x. But, if I change the map to:
    @b=@_; map {s#[X/]#10#} @b; @_=@b;
    Then, it works just fine. The error I'm getting is "Cannot modify a read-only variable." What's the about?

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      Hello

      I'm using 5.6.2 and got that same error. I fixed it by doing:

      @b=@_;@_=@b; # then preceed as usual
      I bet the problem is that you are calling the function as
      score(qw/X 6 5 7 9 ...../);
      instead of:
      @arr = qw/X 4 5 6 .../; score(@arr);
      Map is modifying the content of the list (using s#...#10#). Passing a list (constant) to the sub makes @_ elements aliases to the values passed. This is just like saying
      "helo" =~ s/l/ll/;
      which does not make sense.

      Did I make myself clear? Does it make sense?

      Aziz,,,

        Yes, you are correct! Wow, I didn't realize that could be an issue, though it makes perfect sense, once it's put into those terms.

        Thanks! :)

        ------
        /me is slowly becoming the brightest bulb in the chandelier! (though others are kW bulbs...)

        Vote paco for President!

Re: Re: (Golf) Let's go bowling
by dragonchild (Archbishop) on Aug 09, 2001 at 01:51 UTC
    I'd hope it could be improved. It doesn't work too well. As is, it doesn't run. By changing the map{}@_; to @b=@_;map{}@b; and all the references to $_[] to $b[], it runs, but gives the wrong answers. It looks neat, though!

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      define "doesn't work"? it ran with many test cases for me..

      update: downloaded my exact code posted here and ran it with the following games..

      my @b = qw(X X X X X X X X X X 7 2); [rchiav@aps:/home/rchiav]$ score score is 286 my @b = qw(X X X X X X X X X X X X); [rchiav@aps:/home/rchiav]$ score score is 300 my @b = qw(X 9 / X 9 / X 9 / X 9 / X 9 / X); [rchiav@aps:/home/rchiav]$ score score is 200 and the actual example given in the description.. my @b = qw(5 / 6 3 X X 7 0 4 3 X 4 / 8 1 3 / 6); [rchiav@aps:/home/rchiav]$ score score is 146
      If you could, please explain to me what's "not working".
        I bet that you are running 5.6.x and the code is failing to run in 5.005_03. The reason is that the aliasing was a little more aggressive in 5.005_03.

        Trust me. This is a bug I have hit quite often in golf.

        (BTW the current leader on the score-board is chipmunk.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://103247]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2015-07-31 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls