4
Does anyone have any suggestions as to how I could shorten this program?
_=map(ord,raw_input());_=[10if y==88else 0if y==45else 58-_[x-1]if y==47else y-48for x,y in enumerate(_)];t=0;exec('t+=sum(_[:2+(_[0]+_[1]>=10)]);_=_[2-(_[0]==10):];'*10);print t
I'd like to write a bowling calculator. I'm fairly proficient in python so I chose that language.
The trick is that the input string is made up of frames not split by white-space and the spares, strikes, and misses are marked by /
, X
, and -
respectively. (note that there are no 0
in the code, -
is used instead)
Here is an example line X7/9-X-88/-6XXX81
, this line would have a score of 167
.
As you can see, most of my characters are spent on mapping these special characters to their actual bowl scores. I do this by taking their ord
values and then replacing the X
and -
with 10
and 0
respectively. I then find the /
rolls and set their value as ten minus the previous roll.
After this, I calculate the final game score by looping over 10 frames (the size of a line of bowling), chunking the line array based on if the frame was a strike spare or normal roll, and then reducing the array going forward.
I feel like this is the best way to perform this challenge as a golf challenge, but I'm not sure if I'm reducing it enough.
@user202729 The TIO link, as well as the use of print strongly suggests it is not. – Post Rock Garf Hunter – 2018-07-12T03:20:43.093
@user202729 Just changed it, thanks! – BigBerger – 2018-07-12T04:00:29.207
1To be clear a spare (
/
) can never come after a gutter (-
)? Your current code doesn't seem to handle this. – Post Rock Garf Hunter – 2018-07-12T04:05:17.187Oh and welcome to the site by the way! – Post Rock Garf Hunter – 2018-07-12T04:08:07.750
3I'm getting a score of 176 for your example. Could you maybe write out the bowling scoring rules to make sure we're all on the same page about how they are calculated? – xnor – 2018-07-12T04:27:39.827
Here are the scores I have after each roll: `X 10; 7 24; / 30; 9 48;
X 58;
8 74; 8 82; / 84;
6 90; X 100; X 120; X 150; 8 174; 1 176`. – xnor – 2018-07-12T04:46:18.847
@user202729 I can use any language. – BigBerger – 2018-07-12T04:57:42.813
@xnor I added the wikipedia with the rules, it's easier than writing them out. – BigBerger – 2018-07-12T04:58:03.190
@user202729 yes it is guaranteed – BigBerger – 2018-07-12T04:58:26.343
@user202729 Feel free! But my office decided that only "real" languages are allowed to compete for the free lunch (python, perl, ruby, javascript, etc). I'm guessing it will be up for debate depending on our submissions (it's a small office). – BigBerger – 2018-07-12T05:01:54.237
I suppose the last edit is only for the bonus section? Because the question is Python only? ... – user202729 – 2018-07-12T05:01:58.463
Let us continue this discussion in chat.
– BigBerger – 2018-07-12T05:02:14.1171OK, I was missing that if the game goes into extra frames, those frames are not scored. Are we guaranteed that the game as listed lasts exactly ten frames and those extra rolls are listed exactly if the the game goes into those extra frames? – xnor – 2018-07-12T05:03:34.800
A naive translation to J. Just for fun. Try it online!
– user202729 – 2018-07-12T06:09:16.360This could make for an interesting challenge, assuming it hasn't been done already. – Shaggy – 2018-07-12T08:58:26.563
Closely related. – pajonk – 2018-07-12T09:47:56.497
Yeah, it's been done already
– Shaggy – 2018-07-12T11:22:39.100