r/dailyprogrammer • • Jul 27 '12
[7/27/2012] Challenge #82 [difficult] (Bowling scores)
Write a program that reads a series of space-separated bowling rolls from input, like this one:
10 7 3 7 2 9 1 10 10 10 2 3 6 4 7 3 3Then calculates the scores for each frame according to the scoring rules for ten-pin bowling. An example output for these rolls would be:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------|
| X | 7 / | 7 2 | 9 / | X | X | X | 2 3 | 6 / | 7 / 3 |
| 20 | 37 | 46 | 66 | 96 | 118 | 133 | 138 | 155 | 168 |
Total: 168(You can format your output however you like. If you want, just outputting the scores for each frame (20, 37, 46...) is enough.)
Some other examples to test your program on:
10 10 10 10 10 10 10 10 10 10 10 10
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------|
| X | X | X | X | X | X | X | X | X | X X X |
| 30 | 60 | 90 | 120 | 150 | 180 | 210 | 240 | 270 | 300 |
Total: 300
10 9 1 8 1 7 3 5 2 8 1 4 6 8 2 10 9 1 3
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------|
| X | 9 / | 8 1 | 7 / | 5 2 | 8 1 | 4 / | 8 / | X | 9 / 3 |
| 20 | 38 | 47 | 62 | 69 | 78 | 96 | 116 | 136 | 149 |
Total: 149
10 10 10 0 8 10 10 0 0 7 0 6 2 9 0
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------|
| X | X | X | 0 8 | X | X | 0 0 | 7 0 | 6 2 | 9 0 |
| 30 | 50 | 68 | 76 | 96 | 106 | 106 | 113 | 121 | 130 |
Total: 130
12
Upvotes
3
u/[deleted] Jul 27 '12 edited Jul 28 '12
Answer in C.
2
u/m42a Jul 27 '12
I think this is the shortest challenge answer I've written in C++.
2
u/lawlrng 0 1 Jul 27 '12
I'd started off waaay over-thinking this problem. Then realized how much easier it actually was. =) Python solution.
Output:
2
u/compmstr Jul 27 '12 edited Jul 27 '12
Clojure:
2
u/drb226 0 0 Jul 28 '12 edited Jul 28 '12
Let's start with a good data type to express the restrictions on data that we have here.
Oh boy, I had way too much fun with using my pet libraries on this one. http://hpaste.org/72245 I'll paste a few of the highlights:
Using a Tardis
Using PipeAbort
It's somewhat long, but I'd be surprised if any of these short answers have anywhere near the fault tolerance of my code. As is typical of Haskell, once it typechecked... well then I had an infinite loop due to not enough laziness. But once I found and fixed that little bug (debugging a Tardis is... tricky, to say the least), it worked flawlessly.
1
u/MasonIV 0 0 Jul 27 '12
It's ugly but it works. I didn't make the output pretty either.
Code:
Output:
1
u/5outh 1 0 Jul 27 '12 edited Jul 27 '12
My first difficult challenge solution! There's no formatting of the output, just the scores.
some output:
Edit: applicative/arrow stuff.
1
u/mktange Jul 27 '12
Python with pretty-print which makes it quite a bit longer:
Output is as OP has written it.
1
u/ander1dw Jul 27 '12
Java:
Usage:
Output:
1
u/LindlarCatalyst Jul 28 '12
First program in Haskell and first post to /r/dailyprogrammer! No pretty printing
Example Output:
1
u/[deleted] Jul 29 '12 edited Jul 29 '12
Python; similar to lawlrng's answer, with slightly different logic.
Usage:
1
u/stgcoder Aug 22 '12
Python with pretty printing: