Place a repeating sequence of pentomino pieces on a 3 by 10 board with Tetris drops and row clears to maximize the count, or report forever.
Medium7GraphDFSSimulationBrute forceNo attempts yetTime limit4sMemory limit256 MB
Falling Blocks is a Tetris-like arcade game played on a board with 3 columns and 10 rows. Pieces fall from the top of the board one at a time. There are 8 pieces, and every piece is a pentomino, five unit squares joined edge to edge. The name and shape of each piece are shown below, where # is a filled square and . is empty.
T X R S
### .#. .## .##
.#. ### ##. .#.
.#. .#. .#. ##.
Z V 7 W
##. #.. ##. #..
.#. #.. .## ##.
.## ### .#. .##
A piece can be rotated freely by 0, 90, 180 or 270 degrees, but it cannot be flipped.
The rules are close to Tetris. You pick a rotation and a horizontal position for the new piece, and the piece then falls straight down from the top of the board until it hits the floor or a block already on the board. You cannot push it sideways and you cannot slide it under an overhang. Once the piece stops, every completely full row is removed and the rows above move down, with no change inside those rows.
Drop an R piece on an empty board without rotating it and you get the first board in the picture above. Dropping a rotated Z piece on top of it fills two rows. Those two rows are removed and the rows above are pushed down, which gives the last board in the picture. The last board has a hanging block, and that block does not fall any further.
One rule differs from Tetris. The top three rows of the board must be completely empty before a piece can be placed. That is, if any of the top three rows is not empty after every full row has been removed, the game is over.
The score is the number of pieces placed on the board before the game is over. Given a sequence of pieces that repeats indefinitely, find the largest number of pieces that can be placed.
Here are a few sequences that show how the rules work.
With the sequence X, every drop of an X piece leaves two rows that no later X piece can clear. After four pieces there are eight non-empty rows, so the fifth X piece cannot be placed, and the answer is 4.
With the sequence XXXXR, the R piece could be rotated so that it avoids the square left in the highest non-empty row, but the top three rows must be empty before any piece is placed, so that placement is not allowed. The answer is 4.
With the sequence VZV, two V pieces and one Z piece clear the board completely, so the game goes on forever.
The input is a single line with one string that gives the sequence of pieces. The string is between 1 and 20 characters long, and every character is one of T, X, R, S, Z, V, 7, W. The sequence repeats indefinitely.
Print on a single line the largest number of pieces that can be placed before no further piece can be placed. If the game can go on indefinitely, print forever.