TopCoder problem "Thimbles" used in SRM 354 (Division II Level One)



Problem Statement

    

Thimbles is a hazard game with the following rules. The dealer puts three identical thimbles on the table, with a small ball underneath the first one. Then, he repeatedly selects a pair of thimbles and swaps their positions. Finally, he asks you "Where is the ball?". You win if you point to the right thimble and lose otherwise.

You are writing the computer version of this game, and in this problem, you must write the module that determines the position of the ball after all the thimble swaps have been done.

You will be given a String[] swaps which describes the swaps made, in order. Each element of swaps will be in the format "X-Y" (quotes for clarity), which means that the thimble in position X was swapped with the thimble in position Y. The positions are 1, 2 or 3. Your method should return the position of the thimble with the ball after all the swaps.

 

Definition

    
Class:Thimbles
Method:thimbleWithBall
Parameters:String[]
Returns:int
Method signature:int thimbleWithBall(String[] swaps)
(be sure your method is public)
    
 

Constraints

-swaps will contain between 1 and 50 elements, inclusive.
-Each element of swaps will be in the format "X-Y" (quotes for clarity) where X and Y are distinct digits between 1 and 3, inclusive.
 

Examples

0)
    
{"1-2", "3-1"}
Returns: 2
Initially the ball is under the first thimble. After the first swap it moves to the second position. The second swap doesn't affect the ball. So, the final position of the ball is 2.
1)
    
{"3-1", "2-3", "3-1", "3-2"}
Returns: 3
The path of the ball is:

1->3->2->2->3.
2)
    
{"2-3", "1-3", "2-3", "2-1", "3-1"}
Returns: 3
3)
    
{"1-2", "3-2", "1-2", "2-1", "2-1", "3-2", "1-3", "3-1", "1-2"}
Returns: 1

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7774

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10711&pm=7774

Writer:

Andrew_Lazarev

Testers:

PabloGilberto , brett1479 , Yarin , Olexiy , ivan_metelsky

Problem categories:

Simulation