row, and the player has a reserve of marbles reserve. Colors are written as letters: R red, Y yellow, B blue, G green, W white.
On each move the player takes any marble from the reserve and inserts it into the row: between any two neighboring marbles or at either end. If three or more marbles of the same color now stand next to each other, that whole group vanishes and the row closes up. If closing up forms a new group of three or more same-colored marbles, it vanishes too, and so on until no such group is left.
The player wins once the row is empty. Return the fewest marbles from the reserve needed to clear the row, or -1 if the reserve is not enough.row="WRRW", reserve="RW" → 2
Insert R next to RR: WRRRW → WW. Then insert W: WWW → empty. Two marbles were used.
row="YGGY", reserve="G" → -1
The only marble, G, clears the middle, but the two Y marbles stay and the reserve is empty.
row="B", reserve="BBB" → 2
Add two B marbles one by one: BB, then BBB → empty. The third marble is not needed.
row and reserve contain only the letters R, Y, B, G, Wrow="WRRW", reserve="RW"
2
Run checks the first 3 tests in your browser. Submit checks all 4 tests on the server, hidden ones included.