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

import scala.io.StdIn.readLine

object Main extends App {

  val labels = readLine().split(" ")
  val directions = readLine()

  val dice: Dice = new Dice(labels)
  dice.move(directions)
  println(dice.getLabel(0))

  class Dice(labels: Array[String]) {

    object f extends Enumeration {
      val top = 0
      val front = 1
      val right = 2
      val left = 3
      val back = 4
      val bottom = 5
    }

    val faces = Array.fill(6){0}
    for (i <- 0 until faces.length) {
      faces(i) = i
    }

    def getLabel(i: Int): String = {
      labels(faces(i))
    }

    def setFace(topLabel: String, frontLabel: String): Boolean = {
      var currentPos = getCurrentPos(topLabel)
      currentPos match {
        case p if p == faces(f.top) => {}
        case p if p == faces(f.front) => move("N")
        case p if p == faces(f.right) => move("W")
        case p if p == faces(f.left) => move("E")
        case p if p == faces(f.back) => move("S")
        case p if p == faces(f.bottom) => move("EE")
        case -1 => return false
      }
      currentPos = getCurrentPos(frontLabel)
      currentPos match {
        case p if p == faces(f.top) => return false
        case p if p == faces(f.front) => {}
        case p if p == faces(f.right) => move("SEN")
        case p if p == faces(f.left) => move("SWN")
        case p if p == faces(f.back) => move("SEEN")
        case p if p == faces(f.bottom) => return false
        case -1 => return false
      }
      true
    }

    def getCurrentPos(label: String): Int = {
      for (i <- 0 until labels.length) {
        if (labels(faces(i)).equals(label)) {
          return faces(i)
        }
      }
      -1
    }

    def move(direction: String): Unit = {
      direction.foreach(move(_))
    }

    def move(direction: Char): Unit = {
      val oldTop = faces(f.top);
      direction match {
        case 'S' => {
          faces(f.top) = faces(f.back)
          faces(f.back) = faces(f.bottom)
          faces(f.bottom) = faces(f.front)
          faces(f.front) = oldTop
        }
        case 'N' => {
          faces(f.top) = faces(f.front)
          faces(f.front) = faces(f.bottom)
          faces(f.bottom) = faces(f.back)
          faces(f.back) = oldTop
        }
        case 'W' => {
          faces(f.top) = faces(f.right)
          faces(f.right) = faces(f.bottom)
          faces(f.bottom) = faces(f.left)
          faces(f.left) = oldTop
        }
        case 'E' => {
          faces(f.top) = faces(f.left)
          faces(f.left) = faces(f.bottom)
          faces(f.bottom) = faces(f.right)
          faces(f.right) = oldTop
        }
      }
    }

    def isIdentical(other: Dice): Boolean = {
      for (i <- 0 until labels.length; j <- 0 until labels.length) {
        val success1 = this.setFace(labels(i), labels(j))
        val success2 = other.setFace(labels(i), labels(j))
        if (success1 && success2 && checkIdentical(other)) {
          return true
        }
      }
      false
    }

    def checkIdentical(other: Dice): Boolean = {
      for (i <- 0 until labels.length) {
        if (!getLabel(i).equals(other.getLabel(i))) { return false }
      }
      true
    }
  }

}