Rolling a Die

Interview

Time limit1sMemory limit128 MB

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

A die showing 1 on top, 2 on the south face, and 3 on the east face

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.

The six operations

  • North, East, South, and West roll the die 90∘90^\circ 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 90∘90^\circ 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 (11) 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 nn (0≤n≤100000 \le n \le 10000). Each of the following nn 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.

Examples2

  1. Example 1

    Input
    5
    North
    North
    East
    South
    West
    
    Expected output
    21
    
  2. Example 2

    Input
    8
    West
    North
    Left
    South
    Right
    North
    Left
    East
    
    Expected output
    34