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 JAVA (judge verdict: Accepted)

import java.util.*;

// サイコロの面の向き
enum Direction {
    UP,
    FRONT,
    RIGHT,
    LEFT,
    BACK,
    DOWN
};

public class Main {
   public static void main(String[] args) throws Exception {
        // サイコロの面の数値を取得する。
        Scanner scan = new Scanner(System.in);
        int[] labels = new int[6];
        for(int i=0; i < 6 ; i++) {
            int input = scan.nextInt();
            if(0 <= input && input <= 100) {
                labels[i]=input;
            }else{
                System.out.println("サイコロの面の数字の値は０以上１００以下でお願いします。");
            }
        }
        
        Dice dice = new Dice(labels);
        // 命令取得
        String orders = scan.next();
        if(0 <= orders.length() && orders.length() <= 100) {
            String[] strArray = orders.split("");
            for(String s : strArray) {
                switch(s){
                    case "E": 
                        dice.ToRight();
                        break;
                    case "N": 
                        dice.ToBack();
                        break;
                    case "S": 
                        dice.ToFront();
                        break;
                    case "W": 
                        dice.ToLeft();
                        break;
                    default:
                        System.out.println("値おかしい");
                        break;
                }
            }
        } else {
            System.out.println("命令文は０以上１００以下でお願いします。");
        }
        dice.ShowAnswer();
    }
}

class Dice{

    // 面
    Surface[] surfaces;
    
    // コンストラクタ
    Dice(int[] inputLabels) {
        // 面を作成
        surfaces = new Surface[6]; 
        surfaces[0] = new Surface(inputLabels[0], Direction.UP);
        surfaces[1] = new Surface(inputLabels[1], Direction.FRONT);
        surfaces[2] = new Surface(inputLabels[2], Direction.RIGHT);
        surfaces[3] = new Surface(inputLabels[3], Direction.LEFT);
        surfaces[4] = new Surface(inputLabels[4], Direction.BACK);
        surfaces[5] = new Surface(inputLabels[5], Direction.DOWN);
    }

    // 右に転がす
    void ToRight() {
        for (Surface surface : surfaces) {
            surface.ToRight();
        }
    }
    // 左に転がす
    void ToLeft() {
        for (Surface surface : surfaces) {
            surface.ToLeft();
        }
    }
    // 前に転がす
    void ToFront() {
        for (Surface surface : surfaces) {
            surface.ToFront();
        }
    }
    // 後ろに転がす
    void ToBack() {
        for (Surface surface : surfaces) {
            surface.ToBack();
        }
    }
    
    void ShowAnswer(){
        for (Surface surface : surfaces) {
            if(surface.GetDirection() == Direction.UP){
                System.out.println(surface.GetLabel());
            }
        }
    }
}

class Surface {
    // ラベル
    private int label;
    // 向き
    private Direction direction;
    
    
    Surface(int label, Direction direction){
        this.label = label;
        this.direction = direction;
    }

    void ToRight() {
        if(this.direction == Direction.UP) {
            this.direction = Direction.RIGHT;
        } else if(this.direction == Direction.RIGHT) {
            this.direction = Direction.DOWN;
        } else if(this.direction == Direction.DOWN) {
            this.direction = Direction.LEFT;
        } else if(this.direction == Direction.LEFT) {
            this.direction = Direction.UP;
        }
    }
    void ToLeft() {
        if(this.direction == Direction.UP) {
            this.direction = Direction.LEFT;
        } else if(this.direction == Direction.RIGHT) {
            this.direction = Direction.UP;
        } else if(this.direction == Direction.DOWN) {
            this.direction = Direction.RIGHT;
        } else if(this.direction == Direction.LEFT) {
            this.direction = Direction.DOWN;
        }
    }
    void ToFront() {
        if(this.direction == Direction.UP) {
            this.direction = Direction.FRONT;
        } else if(this.direction == Direction.FRONT) {
            this.direction = Direction.DOWN;
        } else if(this.direction == Direction.DOWN) {
            this.direction = Direction.BACK;
        } else if(this.direction == Direction.BACK) {
            this.direction = Direction.UP;
        }
    }
    void ToBack() {
        if(this.direction == Direction.UP) {
            this.direction = Direction.BACK;
        } else if(this.direction == Direction.BACK) {
            this.direction = Direction.DOWN;
        } else if(this.direction == Direction.DOWN) {
            this.direction = Direction.FRONT;
        } else if(this.direction == Direction.FRONT) {
            this.direction = Direction.UP;
        }
    }
    int GetLabel(){
        return this.label;
    }
    
    Direction GetDirection(){
        return this.direction;
    }
    
}