This page is still under construction.

Parts of this page are still being built. What you see may change.

Frame by Frame

Time limit1sMemory limit256 MB

Summary
Parse bowling notation for ten frames and compute the total score with spare and strike bonuses.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Compute the final score of a game of bowling. A game has 10 frames, and only the tenth frame works differently from the first nine.

Each of frames 1 to 9 consists of one or two rolls. Ten pins are set up at the start of the frame. If you knock down all 10 pins with the first roll you make a strike and the frame ends there. Otherwise you get a second roll at the remaining pins. If the second roll knocks down every remaining pin you make a spare, and if it does not you have an open frame.

An open frame scores the total number of pins knocked down in that frame. A spare scores 10 points plus the number of pins knocked down on the next roll. A strike scores 10 points plus the number of pins knocked down on the next two rolls. When the next roll is also a strike, those two rolls fall in two different frames.

The tenth frame consists of two or three rolls. It goes like this.

  • Ten pins are set up.
  • If the first roll knocks down all 10 pins, you score a strike and 10 pins are set up again.
    • If the second roll also knocks down all 10 pins, you score another strike and 10 pins are set up one last time.
      • If the third roll knocks down all 10 pins as well, you score one more strike. (1)
      • If it knocks down fewer than 10 pins, you score that number of pins. (2)
    • If the second roll knocks down fewer than 10 pins, you score that number of pins and the remaining pins stay standing.
      • If the third roll knocks down every remaining pin, you score a spare. (3)
      • If it knocks down fewer, you score that number of pins. (4)
  • If the first roll knocks down fewer than 10 pins, the remaining pins stay standing.
    • If the second roll knocks down every remaining pin, you score a spare and 10 pins are set up again.
      • If the third roll knocks down all 10 pins, you score a strike. (5)
      • If it knocks down fewer than 10 pins, you score that number of pins. (6)
    • If the second roll leaves a pin standing, you score the number of pins knocked down in both rolls. (7)

The tenth frame scores the total number of pins knocked down over all of its rolls, with no bonus.

The notation is as follows. A strike is written X, a spare is written as the number of pins knocked down on the first roll followed by /, and an open frame is written as the two pin counts. Following the numbering above, the tenth frame is written as one of (1) X X X, (2) X X a, (3) X a /, (4) X a b, (5) a / X, (6) a / b, (7) a b, where a and b are numbers.

Input

The first line contains the number of games. Each of the following lines contains the notation of one game, with tokens separated by spaces.

Frames are not delimited in any way. Working out whether a frame took one roll, two rolls, or three rolls is part of the task. Every notation in the input is valid.

Output

Print the total score of each game, one per line, in the order the games are given.

Hint

The game 8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8 goes like this.

FrameNotationPointsTotal
18 18 + 1 = 99
2X10 + 10 + 3 = 2332
3X10 + 3 + 7 = 2052
43 /10 + 4 = 1466
54 54 + 5 = 975
61 /10 + 10 = 2095
7X10 + 10 + 3 = 23118
8X10 + 3 + 7 = 20138
93 /10 + 4 = 14152
104 / 84 + 6 + 8 = 18170

Examples2

  1. Example 1

    Input
    3
    2 6 5 0 4 4 0 9 3 4 8 1 1 4 3 2 5 4 6 1
    3 / 4 4 X 5 3 9 0 1 / X 8 / 7 2 X 4 3
    8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8
    
    Expected output
    72
    140
    170
    
  2. Example 2

    Input
    2
    X X X X X X X X X X X X
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    
    Expected output
    300
    0