Syntactic Confectionery Delight PerlMonks

### Re: (Golf) Let's go bowling

by chipmunk (Parson)
 on Aug 08, 2001 at 23:15 UTC ( #103177=note: print w/ replies, xml ) Need Help??

in reply to (Golf) Let's go bowling

This is a great golf challenge!

Here's my best solution:

```sub score {
my\$s;sub S{\$_[0]=~X?10:\$_[0]}\$s+=(\$l=shift)=~X?10+(\$_[1]eq'/'?10:S(\$_[
+0])+S\$_[1]):(\$r=shift)eq'/'?10+S\$_[0]:\$l+\$r for 1..10;\$s
}
126 characters. (Or 125, if \$r is replaced by a punctuation variable, since a space could then be eliminated.)

During a Chatterbox discussion, tye suggested using a regex to parse the score. This sounded promising, but unfortunately I ended up with a longer solution.

```sub score {
\$"=my\$s;sub S{\$_[0]=~X?10:\$_[0]}\$S="@_";\$S=~/(X)(?=(.)(.))|(.\/)(?=(.)
+)|(.)(.)/g,\$s+=\$1?10+(\$3eq'/'?10:S(\$2)+S(\$3)):\$4?10+S(\$5):\$6+\$7for 1.
+.10;\$s
}
145 characters.
Comment on Re: (Golf) Let's go bowling
Replies are listed 'Best First'.
Re: Re: (Golf) Let's go bowling
by Brovnik (Hermit) on Aug 09, 2001 at 03:17 UTC
This fails on
```X 0 / X 0 / X 0 / X 0 / X 0 / X -> 200, should be 210
Update following comments below : I stand by the 210 score here. Hitting a spare in the 10th frame means you get one extra ball.
In this case it is a strike (meaning here that you score 10 for the pincount but don't get any further extra balls.)

This is the same principle that allows a score of 300 for a perfect game, counting the 11th and 12th shots as purely pincount of 10 each.
--
Brovnik

chipmunk has the right answer here. Here is how to score that example:
```Recorded  FrameScore Total
--------  ---------- -----
X          20       20

0
/          20       40

X          20       60

0
/          20       80

X          20      100

0
/          20      120

X          20      140

0
/          20      160

X          20      180

0
/          20      200

X       (NOT PART OF ANY FRAME)
-----
200
Does that make sense now?
X (NOT PART OF ANY FRAME)

Not true. It's part of the 10th frame. The so-called "extra" or "bonus" ball given for a mark (strike or spare) in the 10th frame is merely a natural consequence of the scoring rules. In any frame, a strike is 10 + next 2 balls, and a spare is 10 + the next ball. All frames are scored the same, so there must be two balls thrown after a strike or one ball after a spare.

```X 0 / X 0 / X 0 / X 0 / X 0 / X -> 200, should be 210
Update following comments below : I stand by the 210 score here. Hitting a spare in the 10th frame means you get one extra ball.

No, it's 200. tilly's slightly unusual exegesis of the 10th frame above may have thrown you off. As you yourself say, the tenth frame is scored exactly like the previous 9 - the spare gives 10 + the next ball (X) -> 10 + 10 = 20, just as in the other 9 frames. Each frame in the game scores 20, and 20 * 10 = 200. Perhaps seeing it in a more bowling-alley-like style will help:

```   1   2   3   4   5    6    7    8    9    10
X   0/  X   0/  X    0/   X    0/   X    0/X
20  40  60  80  100  120  140  160  180  200
Okay, let's try this out, on a perfect game. (The extra balls for the strike in the tenth frame are in the 'eleventh' and 'twelfth' frames, respectively.)
```      +-----------------------------------------------+
Frame | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |
+-----------------------------------------------+
Tally |  X| X | X | X | X | X | X | X | X | X | X | X |
+-----------------------------------------------+
Score | 30| 60| 90|120|150|180|210|240|270|300|310|320|
+-----------------------------------------------+
The extra balls should be counted only for their bonuses in the previous frames. They do not get counted on their own. Thus, the correct score for a game of X 0 / X 0 / X 0 / X 0 / X 0 / X is 200, not 210.
(tye)Re: (Golf) Let's go bowling
by tye (Sage) on Aug 11, 2001 at 12:00 UTC

I think you misunderstood what I was thinking. This routine requires a single string instead of a list of single-character scalars and weighs in at just 103 strokes!

```sub bowl {
\$_=pop;
s#(\d)([\d/](?=(.)))|X(?=(..))|.#\$3?"X\$3":\$4?"X\$4":\$1.\$2#ge;
s#\d/#/#g;
map{(y./X.aa.)x hex}split//
}

Or, you can handle getting the pinfall data any way you want. I don't care, so long as the code calculates scores correctly.
...so I don't consider this cheating. (: You can replace theh first statement with \$_=join'',@_; if you disagree and I'm still only at 109. This is a fairly long thread so I'm almost certain I missed something, but I didn't see any scores that low.

Update: I can golf that down a bit if you don't mind me using more undef strings which would generate warnings if such were enabled (the original only does that for the bonus balls). Also, I golfed the tr down by one stroke:

```sub bowl {
\$_=pop;
s#(\d)(\d|/(?=(.)))|X(?=(..))|.#\$3.\$4?"X\$3\$4":\$1.\$2#ge;
s#\d/#/#g;
map{(y./X.a.)x hex}split//
}
which gets me to 98!

Oh, and I did cheat in that you have to call this in a scalar context or you get what looks like a binary number. I'll take a 2-stroke penalty ("0+") for that if required.

Update: Darn, I just noticed that a late bug fix (for /X\d\d/ in the tenth frame) got left out but that fix breaks things worse. An alternate bug fix hurts a little more:

```sub bowl {
\$_=pop;
s#(\d)(\d(?=.)|/(?=(.)))|X(?=(..))|.#\$3.\$4?"X\$3\$4":\$1.\$2#ge;
s#\d/#/#g;
map{(y./X.a.)x hex}split//
}
which puts me back up at 103.

- tye (but my friends call me "Tye")

I got down to 98:

```sub bowl {
\$_=pop;
s#(\d)(\d|(/))(?=(.))|X(?=(..))|.#\$3.\$5?"X\$4\$5":\$1.\$2#ge;
s#./#X#g;
map{(y.X.a.)x hex}split//
}

- tye (but my friends call me "Tye")
Same thing that caught me:
```print bowl(join'', qw(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 X 3 4));
53!
should be 60.
(but you can save a few strokes using /./g for split//)

p

Create A New User
Node Status?
node history
Node Type: note [id://103177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2016-05-04 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (79 votes). Check out past polls.