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

<?php

//統一性を持たすためにやり直した
namespace egg1to3;

class Dice
{
    protected $top;
    protected $front;
    protected $left;
    protected $right;
    protected $back;
    protected $bottom;
    protected $save;

    public function __construct($arr)
    {
        $this->save = $arr;
        $this->init();
    }

    public function init()
    {
        foreach ($this->save as $key => $val) {
            $this->{$key} = (int)$val;
        }
    }

    public function parse($str)
    {
        for ($i = 0, $len = strlen($str); $i < $len; $i++) {
            $direction = $str[$i];
            $this->move($direction);
        }
    }

    //指定された面を上面に移動させる
    public function positionChange($from)
    {
        switch ($from) {
            case 'top':
                break;
            case 'front':
                $this->move('N');
                break;
            case 'right':
                $this->move('W');
                break;
            case 'left':
                $this->move('E');
                break;
            case 'back':
                $this->move('S');
                break;
            case 'bottom':
                $this->parse('NN');
                break;
        }
    }

    //90度回転させる
    public function rotate($left = false)
    {
        $this->parse($left ? 'NES' : 'NWS');
    }

    //指定された面の値を取得
    public function getNumber($from)
    {
        return $this->{$from};
    }

    //面の値を配列にして返す
    public function getArray()
    {
        $arr = array();
        foreach ($this as $key => $val) {
            if (preg_match('/top|front|right|left|back|bottom/', $key)) {
                $arr[$key] = $val;
            }
        }
        return $arr;
    }

    //同じサイコロかチェック
    public function isEqual($dice)
    {
        $arr1 = $this->getArray();
        $arr2 = $dice->getArray();
        $diff = array_diff($arr1, $arr2);
        //片方にしか存在しない値がある
        if (count($diff)) {
            return false;
        }

        $keys = explode(',', 'top,front,right,left,back,bottom');
        foreach ($keys as $key) {
            //ある面を上面に移動する
            $dice->positionChange($key);
            $cnt = 0;
            //前面を同じ値にする(合わなければ一致しない)
            while ($this->front !== $dice->getNumber('front')) {
                //前面を同じ値にできなかった
                if ($cnt++ === 4) {
                    break;
                }
                $dice->rotate();
            }

            //初期化して次の面へ
            if ($cnt === 4) {
                $dice->init();
                continue;
            }

            $arr2 = $dice->getArray();
            $diff = array_diff_assoc($arr1, $arr2);
            //全ての面が一致すれば返り値は0
            if (count($diff) === 0) {
                return true;
            }

            $dice->init();
        }

        //一致しなかった
        return false;
    }

    protected function move($direction)
    {
        $top = $this->top;
        switch ($direction) {
            case 'S':
                $this->top = $this->back;
                $this->back = $this->bottom;
                $this->bottom = $this->front;
                $this->front = $top;
                break;
            case 'N':
                $this->top = $this->front;
                $this->front = $this->bottom;
                $this->bottom = $this->back;
                $this->back = $top;
                break;
            case 'E':
                $this->top = $this->left;
                $this->left = $this->bottom;
                $this->bottom = $this->right;
                $this->right = $top;
                break;
            case 'W':
                $this->top = $this->right;
                $this->right = $this->bottom;
                $this->bottom = $this->left;
                $this->left = $top;
                break;
            default:
                new Error('error');
        }
    }
}

$line = trim(fgets(STDIN));
$nums = explode(' ', $line);
$keys = explode(',', 'top,front,right,left,back,bottom');
$arr = array_combine($keys, $nums);
fscanf(STDIN, '%s', $str);
$dice = new Dice($arr);
$dice->parse($str);
echo $dice->getNumber('top'), PHP_EOL;