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.



Method signature:int finalPosition(String flips)
(be sure your method is public)


-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.


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


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.
Returns: 11
As before, the initial sequence "hhth" takes us to four o'clock. The fifth flip sends us backward to eleven o'clock.
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.
Returns: 12
Returns: 6

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Simple Math