TopCoder problem "AzimuthMonitoring" used in SRM 360 (Division II Level One)



Problem Statement

    A robot is moving in a plane following a set of instructions.



The following instructions are possible (quotes for clarity only):

"LEFT" - turn left 90 degrees

"RIGHT" - turn right 90 degrees

"TURN AROUND" - turn around (a 180 degree turn)

"LEFT X" - turn left X degrees, where X is a positive integer

"RIGHT X" - turn right X degrees, where X is a positive integer

"HALT" - stop executing instructions; further instructions are not executed



You are given a String[] containing the robot's instructions. Initially, the robot is facing north. Return the robot's final azimuth - the angle between north and the direction the robot is facing. Azimuth is always measured clockwise, and is a number between 0, inclusive, and 360, exclusive. For example, west corresponds to azimuth 270.
 

Definition

    
Class:AzimuthMonitoring
Method:getAzimuth
Parameters:String[]
Returns:int
Method signature:int getAzimuth(String[] instructions)
(be sure your method is public)
    
 

Notes

-Turning right corresponds to turning clockwise, turning left corresponds to turning counterclockwise.
 

Constraints

-instructions will contain between 1 and 50 elements, inclusive.
-Each element of instructions will be a valid instruction, as listed in the problem statement.
-In an instruction of the form "LEFT X" or "RIGHT X", X will be an integer between 1 and 179, inclusive, with no leading zeroes.
 

Examples

0)
    
{"RIGHT"}
Returns: 90
Turning 90 degrees right changes the azimuth from 0 to 90.
1)
    
{"LEFT", "LEFT", "TURN AROUND"}
Returns: 0
After turning left, the robot is facing west (azimuth 270). After turning left again, the robot is facing south (azimuth 180). Finally, turning around makes the robot face north, azimuth 0.
2)
    
{"LEFT 5", "RIGHT 10", "LEFT 15", "RIGHT 20", "LEFT 25", "RIGHT 30", "LEFT 35", "RIGHT 40"}
Returns: 20
There are four pairs of instructions, each pair making the robot turn right 5 degrees.
3)
    
{"RIGHT 59", "RIGHT", "RIGHT", "HALT", "LEFT", "LEFT", "LEFT"}
Returns: 239
The fourth instruction is "HALT", thus none of the following "LEFT" instructions are executed.
4)
    
{"TURN AROUND", "HALT", "LEFT 5", "HALT", "LEFT 5", "HALT"}
Returns: 180
Note that there might be more then one "HALT" instruction.
5)
    
{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "HALT"}
Returns: 270
After the first 4 turns the robot is facing north again, and the fifth instruction turns him west. The last instruction doesn't change anything.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10772&pm=8027

Writer:

darnley

Testers:

PabloGilberto , Olexiy , lovro , ivan_metelsky

Problem categories:

Simple Math, String Manipulation