Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice I

Write a program to simulate rolling a dice, which can be constructed by the following net.

As shown in the figures, each face is identified by a different label from 1 to 6.

Write a program which reads integers assigned to each face identified by the label and a sequence of commands to roll the dice, and prints the integer on the top face. At the initial state, the dice is located as shown in the above figures.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.

In the second line, a string which represents a sequence of commands, is given. The command is one of 'E', 'N', 'S' and 'W' representing four directions shown in the above figures.

Output

Print the integer which appears on the top face after the simulation.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

$0 \leq $ the length of the command $\leq 100$

Sample Input 1

1 2 4 8 16 32
SE

Sample Output 1

8

You are given a dice where 1, 2, 4, 8, 16 are 32 are assigned to a face labeled by 1, 2, ..., 6 respectively. After you roll the dice to the direction S and then to the direction E, you can see 8 on the top face.

Sample Input 2

1 2 4 8 16 32
EESWN

Sample Output 2

32

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Globalization;

enum direction { north, east, west, south }
class dice{
    public int number;
    public int me;
    public dice[] east;
    public dice[] north;
    public dice[] south;
    public dice[] west;
    public int[,] direction;
};


class Program
 {
    static void Main(string[] args)
    {
        string nyuryoku;
        dice[] face = new dice[7];
        string [] temp = new string[6];
        for (int i = 1; i < 7; i++)
        {
            face[i] = new dice();
            face[i].east = new dice[4];
            face[i].west = new dice[4];
            face[i].north = new dice[4];
            face[i].south = new dice[4];
            face[i].direction = new int[4,4];
            face[i].me = i;
        }

        face[1].east[0] = face[4];
        face[1].direction[0, 1] = 1;
        face[1].north[0] = face[2];
        face[1].direction[0, 0] = 0;
        face[1].west[0] = face[3];
        face[1].direction[0, 3] = 3;
        face[1].south[0] = face[5];
        face[1].direction[0, 2] = 2;

        face[1].east[1] = face[2];
        face[1].direction[1, 1] = 1;
        face[1].north[1] = face[3];
        face[1].direction[1, 0] = 0;
        face[1].west[1] = face[5];
        face[1].direction[1, 3] = 3;
        face[1].south[1] = face[4];
        face[1].direction[1, 2] = 2;

        face[1].east[2] = face[3];
        face[1].direction[2, 1] = 1;
        face[1].north[2] = face[5];
        face[1].direction[2, 0] = 0;
        face[1].west[2] = face[4];
        face[1].direction[2, 3] = 3;
        face[1].south[2] = face[2];
        face[1].direction[2, 2] = 2;

        face[1].east[3] = face[5];
        face[1].direction[3, 1] = 1;
        face[1].north[3] = face[4];
        face[1].direction[3, 0] = 0;
        face[1].west[3] = face[2];
        face[1].direction[3, 3] = 3;
        face[1].south[3] = face[3];
        face[1].direction[3, 2] = 2;
//--------------------------------------
        face[2].east[0] = face[4];
        face[2].direction[0, 1] = 0;
        face[2].north[0] = face[6];
        face[2].direction[0, 0] = 0;
        face[2].west[0] = face[3];
        face[2].direction[0, 3] = 0;
        face[2].south[0] = face[1];
        face[2].direction[0, 2] = 0;

        face[2].east[1] = face[6];
        face[2].direction[1, 1] = 1;
        face[2].north[1] = face[3];
        face[2].direction[1, 0] = 1;
        face[2].west[1] = face[1];
        face[2].direction[1, 3] = 1;
        face[2].south[1] = face[4];
        face[2].direction[1, 2] = 1;

        face[2].east[2] = face[3];
        face[2].direction[2, 1] = 2;
        face[2].north[2] = face[1];
        face[2].direction[2, 0] = 2;
        face[2].west[2] = face[4];
        face[2].direction[2, 3] = 2;
        face[2].south[2] = face[6];
        face[2].direction[2, 2] = 2;

        face[2].east[3] = face[1];
        face[2].direction[3, 1] = 3;
        face[2].north[3] = face[4];
        face[2].direction[3, 0] = 3;
        face[2].west[3] = face[6];
        face[2].direction[3, 3] = 3;
        face[2].south[3] = face[3];
        face[2].direction[3, 2] = 3;
//--------------------------------------
        face[3].east[0] = face[2];
        face[3].direction[0, 1] = 0;
        face[3].north[0] = face[6];
        face[3].direction[0, 0] = 3;
        face[3].west[0] = face[5];
        face[3].direction[0, 3] = 0;
        face[3].south[0] = face[1];
        face[3].direction[0, 2] = 1;

        face[3].east[1] = face[6];
        face[3].direction[1, 1] = 0;
        face[3].north[1] = face[5];
        face[3].direction[1, 0] = 1;
        face[3].west[1] = face[1];
        face[3].direction[1, 3] = 2;
        face[3].south[1] = face[2];
        face[3].direction[1, 2] = 1;

        face[3].east[2] = face[5];
        face[3].direction[2, 1] = 2;
        face[3].north[2] = face[1];
        face[3].direction[2, 0] = 3;
        face[3].west[2] = face[2];
        face[3].direction[2, 3] = 2;
        face[3].south[2] = face[6];
        face[3].direction[2, 2] = 1;

        face[3].east[3] = face[1];
        face[3].direction[3, 1] = 0;
        face[3].north[3] = face[2];
        face[3].direction[3, 0] = 3;
        face[3].west[3] = face[6];
        face[3].direction[3, 3] = 2;
        face[3].south[3] = face[5];
        face[3].direction[3, 2] = 3;
//-------------------------------------
        face[4].east[0] = face[5];
        face[4].direction[0, 1] = 0;
        face[4].north[0] = face[6];
        face[4].direction[0, 0] = 1;
        face[4].west[0] = face[2];
        face[4].direction[0, 3] = 0;
        face[4].south[0] = face[1];
        face[4].direction[0, 2] = 3;

        face[4].east[1] = face[6];
        face[4].direction[1, 1] = 2;
        face[4].north[1] = face[2];
        face[4].direction[1, 0] = 1;
        face[4].west[1] = face[1];
        face[4].direction[1, 3] = 0;
        face[4].south[1] = face[5];
        face[4].direction[1, 2] = 1;

        face[4].east[2] = face[2];
        face[4].direction[2, 1] = 2;
        face[4].north[2] = face[1];
        face[4].direction[2, 0] = 1;
        face[4].west[2] = face[5];
        face[4].direction[2, 3] = 2;
        face[4].south[2] = face[6];
        face[4].direction[2, 2] = 3;

        face[4].east[3] = face[1];
        face[4].direction[3, 1] = 2;
        face[4].north[3] = face[5];
        face[4].direction[3, 0] = 3;
        face[4].west[3] = face[6];
        face[4].direction[3, 3] = 0;
        face[4].south[3] = face[2];
        face[4].direction[3, 2] = 3;
//------------------------------------
        face[5].east[0] = face[3];
        face[5].direction[0, 1] = 0;
        face[5].north[0] = face[6];
        face[5].direction[0, 0] = 2;
        face[5].west[0] = face[4];
        face[5].direction[0, 3] = 0;
        face[5].south[0] = face[1];
        face[5].direction[0, 2] = 2;

        face[5].east[1] = face[6];
        face[5].direction[1, 1] = 3;
        face[5].north[1] = face[4];
        face[5].direction[1, 0] = 1;
        face[5].west[1] = face[1];
        face[5].direction[1, 3] = 3;
        face[5].south[1] = face[3];
        face[5].direction[1, 2] = 1;

        face[5].east[2] = face[4];
        face[5].direction[2, 1] = 2;
        face[5].north[2] = face[1];
        face[5].direction[2, 0] = 0;
        face[5].west[2] = face[3];
        face[5].direction[2, 3] = 2;
        face[5].south[2] = face[6];
        face[5].direction[2, 2] = 0;

        face[5].east[3] = face[1];
        face[5].direction[3, 1] = 1;
        face[5].north[3] = face[3];
        face[5].direction[3, 0] = 3;
        face[5].west[3] = face[6];
        face[5].direction[3, 3] = 1;
        face[5].south[3] = face[4];
        face[5].direction[3, 2] = 3;
//-----------------------------------
        face[6].east[0] = face[4];
        face[6].direction[0, 1] = 3;
        face[6].north[0] = face[5];
        face[6].direction[0, 0] = 2;
        face[6].west[0] = face[3];
        face[6].direction[0, 3] = 1;
        face[6].south[0] = face[2];
        face[6].direction[0, 2] = 0;

        face[6].east[1] = face[5];
        face[6].direction[1, 1] = 3;
        face[6].north[1] = face[3];
        face[6].direction[1, 0] = 2;
        face[6].west[1] = face[2];
        face[6].direction[1, 3] = 1;
        face[6].south[1] = face[4];
        face[6].direction[1, 2] = 0;

        face[6].east[2] = face[3];
        face[6].direction[2, 1] = 3;
        face[6].north[2] = face[2];
        face[6].direction[2, 0] = 2;
        face[6].west[2] = face[4];
        face[6].direction[2, 3] = 1;
        face[6].south[2] = face[5];
        face[6].direction[2, 2] = 0;

        face[6].east[3] = face[2];
        face[6].direction[3, 1] = 3;
        face[6].north[3] = face[4];
        face[6].direction[3, 0] = 2;
        face[6].west[3] = face[5];
        face[6].direction[3, 3] = 1;
        face[6].south[3] = face[3];
        face[6].direction[3, 2] = 0;

        dice inital = face[1];


        nyuryoku = Console.ReadLine();
        temp = nyuryoku.Split(' ');
        for (int i = 1; i < 7; i++)
            face[i].number = int.Parse(temp[i-1]);
     
        nyuryoku = Console.ReadLine();
        int position = 1;
        int muki = 0;
        for (int i = 0; i < nyuryoku.Length; i++)
        {
           // Console.WriteLine(" me = {0} muki = {1}", position, muki);
            switch (nyuryoku[i])
            {
                case 'S':
                    inital = face[position].south[muki];
                    muki = face[position].direction[muki, 2];
                    position = inital.me;
                    break;
                case 'N':
                    inital = face[position].north[muki];
                    muki = face[position].direction[muki, 0];
                    position = inital.me;
                    break;
                case 'E':
                    inital = face[position].east[muki];
                    muki = face[position].direction[muki, 1];
                    position = inital.me;
                    break;
                case 'W':
                    inital = face[position].west[muki];
                    muki = face[position].direction[muki, 3];
                    position = inital.me;
                    break;
            }
           // Console.WriteLine(" me = {0} muki = {1}", position, muki);

        }
 
        Console.WriteLine("{0}", inital.number);

    }
 }