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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.