TopCoder problem "ClockWalk" used in SRM 175 (Division II Level One)



Problem Statement

    

Let's walk around the clock. We start at twelve o'clock and flip a coin. If it comes up heads, we step forward by one hour. If it's tails, we step one hour backward. We flip the coin a second time, but now we step two hours forward in the case of heads, or two hours backward for tails. On the third flip, we step three hours forward or backward on the same principle. In general, then, the nth flip decides the direction in which we step n hours: we move forward on heads, and backward on tails.

You are given a String describing a sequence of coin flips such that the nth character is either 'h', meaning that the nth flip is heads, or 't' to signify that the nth flip is tails. Return the hour at which we end up by following the above procedure.

 

Definition

    
Class:ClockWalk
Method:finalPosition
Parameters:String
Returns:int
Method signature:int finalPosition(String flips)
(be sure your method is public)
    
 

Notes

-The clock has an ordinary twelve-hour dial, numbered from 1 to 12.
-Stepping forward means that we advance clockwise around the dial; to step backward is to advance counterclockwise.
 

Constraints

-flips contains between 5 and 50 characters, inclusive
-each character in flips is either 'h' or 't'
 

Examples

0)
    
"hhthh"
Returns: 9
We step forward to one o'clock on the first flip of the coin, and to three o'clock on the second flip. The third flip sends us back to twelve o'clock. On the fourth flip, we step forward to four o'clock. With the fifth and final flip, we arrive at nine o'clock.
1)
    
"hhtht"
Returns: 11
As before, the initial sequence "hhth" takes us to four o'clock. The fifth flip sends us backward to eleven o'clock.
2)
    
"hthth"
Returns: 3
Having stepped one hour forward, two backward, and three forward, we arrive at two o'clock. Stepping backward four hours and forward five, we end up at three o'clock.
3)
    
"hthhhhh"
Returns: 12
4)
    
"hthhthtththhtttthttthhhthtttthh"
Returns: 6

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4680&pm=2241

Writer:

Eeyore

Testers:

lbackstrom , brett1479

Problem categories:

Simple Math