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 java.io.InputStream
import java.io.PrintStream

import scala.io.Source

case class Dice(numbers: Seq[Int])

sealed trait Command
object N extends Command
object E extends Command
object W extends Command
object S extends Command

case class DiceState(
    top: Int = 0,
    south: Int = 1,
    east: Int = 2,
    west: Int = 3,
    north: Int = 4,
    bottom: Int = 5) {

  def runCommand(command: Command): DiceState = command match {
    case N => copy(top = south, south = bottom, bottom = north, north = top)
    case E => copy(top = west, west = bottom, bottom = east, east = top)
    case W => copy(top = east, east = bottom, bottom = west, west = top)
    case S => copy(top = north, north = bottom, bottom = south, south = top)
  }
}

object Main {

  def run(in: InputStream, out: PrintStream): Unit = {
    val (dice, commands) = input(in)
    val state = commands.foldLeft(new DiceState())((s, c) => s.runCommand(c))
    out.println(dice.numbers(state.top))
  }

  private def input(in: InputStream): Tuple2[Dice, Seq[Command]] = {
    val lines = Source.fromInputStream(in).getLines()
    val dice = toDice(nextLine(lines, "??????????????¢?????°"))
    val commands = toCommands(nextLine(lines, "??????"))
    return (dice, commands)
  }

  private def toDice(line: String): Dice = {
    val numbers = line.split(' ')
    if (numbers.length != 6) {
      throw new IllegalArgumentException(s"??????????????¢?????°????????£??§???: $line");
    }

    return new Dice(numbers.map(_.toInt))
  }

  private def toCommands(line: String): Seq[Command] = {
    return line.map(toCommand)
  }

  private def toCommand(char: Char): Command = char match {
    case 'N' => N
    case 'E' => E
    case 'W' => W
    case 'S' => S
    case _ => throw new IllegalArgumentException(s"?????????????????????????????§???: ${char}")
  }

  def nextLine(lines: Iterator[String], label: String): String = {
    if (!lines.hasNext) {
      throw new IllegalArgumentException(s"${label}??????????????\????????????????????????")
    }
    return lines.next()
  }

  def main(args: Array[String]): Unit = {
    try {
      run(System.in, System.out)
    } catch {
      case e: Exception => {
        System.err.println(s"??¨??????: ${e.getMessage()}")
        e.printStackTrace()
      }
    }
  }
}