Rolling a Die
InterviewTime limit1sMemory limit128 MB
Simulate rolling a die through a list of moves and sum the top face value after each move.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
A die is placed on a table in the orientation shown below.

In this initial orientation the die shows 1 on the top face, 2 on the south face, and 3 on the east face. Because the opposite faces of a die always sum to 7, the hidden faces are 5 on the north, 4 on the west, and 6 on the bottom.
Starting from this orientation, the die is moved according to a list of instructions. Every instruction is one of the following six operations.






- North, East, South, and West roll the die so that its top face tips over toward that compass direction. For example, North rolls the die northward: the top face moves to the north side, the north face moves to the bottom, the bottom face moves to the south, and the south face moves to the top, while the east and west faces stay in place. East, South, and West behave the same way toward their own directions.
- Right and Left turn the die about the vertical axis, keeping the top and bottom faces fixed. Right is a clockwise turn seen from above (the north face moves to the east); Left is a counter-clockwise turn seen from above (the north face moves to the west).
Take the value on the top face in the initial orientation () as the starting value. Each time an operation finishes, add the value now showing on the top face to a running total. After every instruction has been carried out, output the total.
Input
The first line contains the number of instructions (). Each of the following lines contains one instruction: one of North, East, South, West, Right, or Left.
Output
Print the final total on a single line, followed by a newline.