Problem Statement
Shells are numbered 1, 2, and 3. The number represents the position of the shell, not the shell itself, so if the 1 and 3 shells are swapped, the former 3 shell is now referenced at 1, and vice versa. Actions are all recorded in 2 character blocks.
A swapping action is recorded by the numbers of the shells swapped: example "13" means he swaps the first shell with the third shell.
An expose action is recorded by the number of the shell exposed, and whether the ball is present. "b" is for Ball, "n" is for No ball. example: "1b" means the gambler exposed the first shell and the ball was underneath, while "2n" means the gambler exposed the second shell and there was no ball underneath.
You are guaranteed that the gambler will expose enough times that you can identify which shell has the ball (expose a shell that has the ball, or expose both empty shells). Also, there is only one ball and the gambler doesn't do anything tricky with it (it moves with its shell during a swap).
Definition
- Class:
- ShellBallGame
- Method:
- findBall
- Parameters:
- String
- Returns:
- int
- Method signature:
- int findBall(String actions)
- (be sure your method is public)
Constraints
- actions is a string representing 1-25 valid actions (2-50 characters inclusive).
- actions contains only valid 2 character actions (# in this example can be 1, 2, or 3):## - swaps the shells # and #, cannot be the same (ie 11 is not a legal action)#n - exposes shell # and ball is not found#b - exposes shell # with ball underneath
Examples
"2331122n1213321232231313311n3n1313312113212n133113"
Returns: 1
"3n132n231223133231322323133131322n23232n2113311232"
Returns: 1
"1b"
Returns: 1
"1n3n"
Returns: 2
"1n2n3b1331133113311331"
Returns: 3
"1n3b13311331133113311331133113311331"
Returns: 3
"1n133n311n133n313n"
Returns: 2
"131313131313231323132313231b"
Returns: 1
"131313131n1313131313131313132323212n"
Returns: 1
"31133121122n2312121331211321123n313231211221132113"
Returns: 3
"13122132211n121232313123321331312b2323212131232n23"
Returns: 2
"12311221233223212n3n31131321131313123232133232"
Returns: 2
"32133121312b233232133223231b123213211312211232311n"
Returns: 3
"13122n1313123223122n211221311213121n12122131312132"
Returns: 2
"211212323212123b3112311321313131211212231323313121"
Returns: 3
"121332321n132n21123n3n231221322n3131123221131b213n"
Returns: 2
"3n1n"
Returns: 2
"2b"
Returns: 2
"121b"
Returns: 1
"2b31122113233221"
Returns: 1
"212112312131212b"
Returns: 2
"2b12"
Returns: 1
"2n1n"
Returns: 3
"3b31"
Returns: 1
"1b1231"
Returns: 2
"313b"
Returns: 3
"1b1332"
Returns: 2
"2b21"
Returns: 1
"122n3n"
Returns: 1
"1b12"
Returns: 2
"1b13"
Returns: 3
"3b"
Returns: 3
"1n2n"
Returns: 3
"2b311221132332"
Returns: 2
"1b21"
Returns: 2
"1b1b"
Returns: 1
"1n232323122323231n"
Returns: 2
"2n3n"
Returns: 1
"2b13"
Returns: 2
"122n321n123132"
Returns: 2
"1b"
Returns: 1
The ball is under the first shell and has not moved.
"3n1n"
Returns: 2
The ball must be under the second shell since it is
"1n121n31"
Returns: 1
The ball must have been under the third shell, and moved to the first when they swapped.
"122n1n32123132"
Returns: 2
"2b311221132332"
Returns: 2
"1n12233n32211n2b232n323n"
Returns: 2
"13231223133231322323131b"
Returns: 1