Problem:


サイコロが以下の図のような向きで置かれている．

ここで使用するサイコロは，この図のように，上側に 1，南側に 2 があるときは，東側に 3 があるものとする．サイコロの向かいあう面の和は必ず 7 なので，見えない面はそれぞれ北側 5，西側 4，下側 6 になっている．

　この初期配置の状態から指示に従ってサイコロを動かしていく．ただし，指示は以下の 6 通りの操作を何回か行うものである． 

North，East，South，West の各操作は指示された方向へサイコロを 90 度回転させる．
　Right，Left の 2 つの操作は上下の面はそのままで水平方向に 90 度回転させる．（回転させる向きに要注意．）

　初期配置で上の面に出ている目 1 を初期値とし， 1 回の操作が終わるたびに，上の面に出ている目の数を加算していき，指示にしたがってすべての操作を終えたときの合計値を出力するプログラムを作成しなさい.

　入力ファイルの 1 行目には総指示回数 n が書かれていて，続く n 行の各行には「North，East，South，West，Right，Left」のいずれか 1 つの指示が書かれているものとする．ただし， n ≦ 10000 とする． 

 

  入力例１入力例２

 

58

NorthWest

NorthNorth

EastLeft

SouthSouth

WestRight

North

Left

East

  　

  出力例１  出力例２

2134

入力

入力は複数のデータセットからなる．n が 0 のとき入力が終了する．データセットの数は 5 を超えない．

出力

データセットごとに、合計値を１行に出力する．

入力例

5
North
North
East
South
West
8
West
North
Left
South
Right
North
Left
East
0

出力例

21
34

各データセットの出力（合計値）の後に改行を入れること。

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
5
North
North
East
South
West
8
West
North
Left
South
Right
North
Left
East
0


Sample output:
21
34

Submission status: Accepted

Solution code:
import sys
d=sys.stdin.readline
p={'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),
   'S':(4,0,2,3,5,1),'R':(0,2,4,1,3,5),'L':(0,3,1,4,2,5)}
for n in iter(d,'0\n'):
 a=[1];s=[1,2,3,4,5,6]
 for _ in[0]*int(n):
  s=[s[e]for e in p[d()[0]]]
  a+=[s[0]]
 print(sum(a))

Submission status: Accepted

Solution code:
import sys
e=sys.stdin.readline
p={'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),
   'S':(4,0,2,3,5,1),'R':(0,2,4,1,3,5),'L':(0,3,1,4,2,5)}
for n in iter(e,'0\n'):
 a=1;s=[1,2,3,4,5,6]
 for _ in[0]*int(n):
  s=[s[i]for i in p[e()[0]]]
  a+=s[0]
 print(a)

Submission status: Accepted

Solution code:
def main():
  while True:
    n = int(input())
    if not n:
      break
  
    D = [1, 2, 3, 4, 5, 6]
    ans = 1
  
    for i in range(n):
      s = input()[0]
  
      if s == "N":
        temp = D[0]
        D[0] = D[1]
        D[1] = D[5]
        D[5] = D[4]
        D[4] = temp
  
      elif s == "S":
        temp = D[0]
        D[0] = D[4]
        D[4] = D[5]
        D[5] = D[1]
        D[1] = temp
  
      elif s == "E":
        temp = D[0]
        D[0] = D[3]
        D[3] = D[5]
        D[5] = D[2]
        D[2] = temp
  
      elif s == "W":
        temp = D[0]
        D[0] = D[2]
        D[2] = D[5]
        D[5] = D[3]
        D[3] = temp
  
      elif s == "L":
        temp = D[1]
        D[1] = D[3]
        D[3] = D[4]
        D[4] = D[2]
        D[2] = temp
  
      elif s == "R":
        temp = D[1]
        D[1] = D[2]
        D[2] = D[4]
        D[4] = D[3]
        D[3] = temp
  
      ans += D[0]
  
    print(ans)

main()

Submission status: Accepted

Solution code:
# AOJ 0502: Dice
# Python3 2018.6.29 bal4u

d = { "North":1, "East":2, "West":3, "South":4, "Right":5, "Left":6 }

rot = ((0,1,2,3,4,5,6),(0,2,6,3,4,1,5),(0,4,2,1,6,5,3),(0,3,2,6,1,5,4), \
       (0,5,1,3,4,6,2),(0,1,3,5,2,4,6),(0,1,4,2,5,3,6))

import sys
while True:
	n = int(input())
	if n == 0: break
	dice = [[j for j in range(7)] for i in range(2)]
	ans = 1
	for i in range(n):
		j = d[input().strip()]
		for k in range(1, 7): dice[1-(i&1)][k] = dice[i&1][rot[j][k]]
		ans += dice[1-(i&1)][1]
	print(ans)

Submission status: Accepted

Solution code:
ans = []
while True:
    place = range(1,7)
    n = input()
    if n == 0:
        break
    p = 0
    sig = 1
    while p != n:
        direct = raw_input()
        if direct == "North":
            temp = place[1]
            place[1] = place[5]
            place[5] = place[4]
            place[4] = place[0]
            place[0] = temp
            sig += temp
        elif direct == "East":
            temp = place[3]
            place[3] = place[5]
            place[5] = place[2]
            place[2] = place[0]
            place[0] = temp
            sig += temp
        elif direct == "West":
            temp = place[2]
            place[2] = place[5]
            place[5] = place[3]
            place[3] = place[0]
            place[0] = temp
            sig += temp
        elif direct == "South":
            temp = place[4]
            place[4] = place[5]
            place[5] = place[1]
            place[1] = place[0]
            place[0] = temp
            sig += temp
        elif direct == "Right":
            temp = place[1]
            place[1] = place[2]
            place[2] = place[4]
            place[4] = place[3]
            place[3] = temp
            sig += place[0]
        elif direct == "Left":
            temp = place[1]
            place[1] = place[3]
            place[3] = place[4]
            place[4] = place[2]
            place[2] = temp
            sig += place[0]
        p += 1
    ans.append(sig)

for i in ans:
    print i

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys

if True:
    from sys import stdin as inputs
else:
    from test_aoj0502 import inputs

class Die:
    def __init__(self, **argv):
        if argv:
            self.faces = dict(argv)
        else:
            self.faces = dict(top=1,
             north=5,
             east=3,
             south=2,
             west=4,
             bottom=6)

    def North(self):
        return Die(
                top=self.faces["south"],
                north=self.faces["top"],
                east=self.faces["east"],
                west=self.faces["west"],
                south=self.faces["bottom"],
                bottom=self.faces["north"])
    def East(self):
        return Die(
                top=self.faces["west"],
                north=self.faces["north"],
                east=self.faces["top"],
                west=self.faces["bottom"],
                south=self.faces["south"],
                bottom=self.faces["east"])
    def South(self):
        return Die(
                top=self.faces["north"],
                north=self.faces["bottom"],
                east=self.faces["east"],
                west=self.faces["west"],
                south=self.faces["top"],
                bottom=self.faces["south"])
    def West(self):
        return Die(
                top=self.faces["east"],
                north=self.faces["north"],
                east=self.faces["bottom"],
                west=self.faces["top"],
                south=self.faces["south"],
                bottom=self.faces["west"])
    def Right(self):
        return Die(
                top=self.faces["top"],
                north=self.faces["west"],
                east=self.faces["north"],
                west=self.faces["south"],
                south=self.faces["east"],
                bottom=self.faces["bottom"])
    def Left(self):
        return Die(
                top=self.faces["top"],
                north=self.faces["east"],
                east=self.faces["south"],
                west=self.faces["north"],
                south=self.faces["west"],
                bottom=self.faces["bottom"])


for line in inputs:
    line = line.strip()
    try:
        n = int(line)
    except:
        n = None
    if n is None:
        count -= 1
        f = getattr(die, line)
        die = f()
        s += die.faces["top"]
        if count == 0:
            print s
    elif n == 0:
        sys.exit()
    else:
        die = Die()
        count = n
        s = 0
        s += die.faces["top"]

Submission status: Accepted

Solution code:
cmds = {
    'North': [0, 1, 5, 4],
    'South': [0, 4, 5, 1],
    'East': [0, 3, 5, 2],
    'West': [0, 2, 5, 3],
    'Right': [1, 2, 4, 3],
    'Left': [1, 3, 4, 2]
}

while 1:
    n = int(raw_input())
    if n == 0: break
    nums = range(1, 7)
    ans = 1
    for i in range(n):
        idxs = cmds[raw_input()]
        nums[idxs[0]], nums[idxs[1]], nums[idxs[2]], nums[idxs[3]] = (
                nums[idxs[1]], nums[idxs[2]], nums[idxs[3]], nums[idxs[0]])
        ans += nums[0]
    print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python
while 1:
    T,U,N,E,W,S=1,6,5,3,4,2
    n=int(raw_input())
    if n==0: break
    ans=T
    for i in range(n):
        di=raw_input()
        if di=="North":
            N,T,S,U=T,S,U,N
        elif di=="East":
            E,U,W,T=T,E,U,W
        elif di=="South":
            T,S,U,N=N,T,S,U
        elif di=="West":
            T,E,U,W=E,U,W,T
        elif di=="Left":
            E,S,W,N=S,W,N,E
        elif di=="Right":
            S,W,N,E=E,S,W,N
        else:
            print '[*] Error ->',di
            raise IOError
        ans+=T
    print ans

Submission status: Accepted

Solution code:
while 1:
    top = 1
    south = 2
    east = 3
    n = input()
    if n == 0:
        break
    Sum = 1
    for i in range(n):
        line = raw_input().strip()
        if line == "North":
            top, south, east = south, 7 - top, east
            Sum += top
        elif line == "East":
            top, south, east = 7- east, south, top
            Sum += top
        elif line == "West":
            top, south, east = east, south, 7 - top
            Sum += top
        elif line == "South":
            top, south, east = 7 - south, top, east
            Sum += top
        elif line == "Right":
            top, south, east = top, east, 7 - south
            Sum += top
        elif line == "Left":
            top, south, east = top, 7 - east, south
            Sum += top
    print Sum

Submission status: Accepted

Solution code:
t,s,e,w,n,b = [1,2,3,4,5,6]
def north():
    global t,s,e,w,n,b
    t,n,b,s = s,t,n,b
def east():
    global t,s,e,w,n,b
    t,e,b,w = w,t,e,b
def west():
    global t,s,e,w,n,b
    t,w,b,e = e,t,w,b
def south():
    global t,s,e,w,n,b
    t,s,b,n = n,t,s,b
def right():
    global t,s,e,w,n,b
    e,n,w,s = n,w,s,e
def left():
    global t,s,e,w,n,b
    e,s,w,n = s,w,n,e
dic = {"North":north,"East":east,
       "West":west,"South":south,
       "Right":right,"Left":left}
while True:
    N = input()
    if N == 0:
        break
    t,s,e,w,n,b = [1,2,3,4,5,6]
    ans = 1
    for i in xrange(N):
        dic[raw_input().strip()]()
        ans += t
    print ans

Submission status: Accepted

Solution code:
class Dice:

    def __init__(self):
        self._top = 1
        self._front = 2
        self._side = 3
        self._sum = 1

    def to_north(self):
        tmp = self._top
        self._top = self._front
        self._front = 7 - tmp 
        self._sum += self._top

    def to_east(self):
        tmp = self._top
        self._top = 7 - self._side
        self._side = tmp
        self._sum += self._top

    def to_west(self):
        tmp = self._top
        self._top = self._side
        self._side = 7 - tmp
        self._sum += self._top

    def to_south(self):
        tmp = self._top
        self._top = 7 - self._front
        self._front = tmp
        self._sum += self._top

    def to_right(self):
        tmp = self._front
        self._front = self._side
        self._side = 7 - tmp
        self._sum += self._top

    def to_left(self):
        tmp = self._front
        self._front = 7 - self._side
        self._side = tmp
        self._sum += self._top

    def get_sum(self):
        return self._sum

if __name__ == '__main__':
    d = ['North', 'East', 'South', 'West', 'Right', 'Left']

    while True:
        dice = Dice()
        n = input()
        if n == 0:
            break

        for i in xrange(n):
            t = raw_input()
            if t == d[0]:
                dice.to_north() 
            elif t == d[1]:
                dice.to_east()
            elif t == d[2]:
                dice.to_south()
            elif t == d[3]:
                dice.to_west()
            elif t == d[4]:
                dice.to_right()
            elif t == d[5]:
                dice.to_left()

        print(dice.get_sum())

Submission status: Accepted

Solution code:
cmdic = {'North':0,'East':1,'West':3,'South':2,'Right':4,'Left':5}
roll = int(raw_input())
valis = []
while roll > 0:
    val = 1
    num = 1
    adj = [None,[2,4,5,3],[6,4,1,3],[2,1,5,6],[2,6,5,1],[1,4,6,3],[5,4,2,3]]
    for i in range(roll):
        cmd = cmdic[raw_input()]
        if cmd < 4:
            r1 = adj[num][(cmd+3)%4]
            r2 = adj[num][(cmd+1)%4]
            r3 = adj[num][(cmd+2)%4]
            if cmd == 1 or cmd == 3:
                adj[7-num] = adj[7-num][2:]+adj[7-num][:-2] 
                adj[r3] = adj[r3][2:]+adj[r3][:-2]
            num = adj[num][cmd]
            adj[r1] = adj[r1][-1:]+adj[r1][:-1]
            adj[r2] = adj[r2][1:]+adj[r2][:1]
        else:
            if cmd == 4:
                adj[num] = adj[num][-1:]+adj[num][:-1]
                for n in adj[num]:
                    adj[n] = adj[n][-1:]+adj[n][:-1]
                adj[7-num] = adj[7-num][1:]+adj[7-num][:1]
            if cmd == 5:
                adj[num] = adj[num][1:]+adj[num][:1]
                for n in adj[num]:
                    adj[n] = adj[n][1:]+adj[n][:1]
                adj[7-num] = adj[7-num][-1:]+adj[7-num][:-1]
        val += num
    valis.append(val)
    roll = int(raw_input())
for v in valis: print v

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n == 0:break
    D = range(1,7)
    ans = 1
    for i in range(n):
        c = raw_input()[0]
        if c == "N":
            D = [D[1],D[5],D[2],D[3],D[0],D[4]]
        elif c == "S":
            D = [D[4],D[0],D[2],D[3],D[5],D[1]]
        elif c == "E":
            D = [D[3],D[1],D[0],D[5],D[4],D[2]]
        elif c == "W":
            D = [D[2],D[1],D[5],D[0],D[4],D[3]]
        elif c == "R":
            D = [D[0],D[2],D[4],D[1],D[3],D[5]]
        elif c == "L":
            D = [D[0],D[3],D[1],D[4],D[2],D[5]]
        ans += D[0]
    print ans

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0:break
	D = range(1,7)
	ans = 1
	for i in range(n):
		c = raw_input()[0]
		if c == "N":
			D = [D[1],D[5],D[2],D[3],D[0],D[4]]
		elif c == "S":
			D = [D[4],D[0],D[2],D[3],D[5],D[1]]
		elif c == "E":
			D = [D[3],D[1],D[0],D[5],D[4],D[2]]
		elif c == "W":
			D = [D[2],D[1],D[5],D[0],D[4],D[3]]
		elif c == "R":
			D = [D[0],D[2],D[4],D[1],D[3],D[5]]
		elif c == "L":
			D = [D[0],D[3],D[1],D[4],D[2],D[5]]
		ans += D[0]
	print ans

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0:break
	D = range(1,7)
	ans = 1
	for i in range(n):
		c = raw_input()[0]
		if   c == "N": S = [1,5,2,3,0,4]
		elif c == "S": S = [4,0,2,3,5,1]
		elif c == "E": S = [3,1,0,5,4,2]
		elif c == "W": S = [2,1,5,0,4,3]
		elif c == "R": S = [0,2,4,1,3,5]
		elif c == "L": S = [0,3,1,4,2,5]
		D = [D[s] for s in S]
		ans += D[0]
	print ans

Submission status: Accepted

Solution code:
import sys

def readline():
    return raw_input()

class Dice:
    def __init__(self):
        # [Top,East,North,West,South,Bottom]
        self.sides = [1,3,5,4,2,6]

    def move(self,direction):
        dirdir = { 'North': ([0,2,5,4],[4,0,2,5]),
                   'South': ([0,2,5,4],[2,5,4,0]),
                   'East':  ([0,1,5,3],[3,0,1,5]),
                   'West':  ([0,1,5,3],[1,5,3,0]),
                   'Left':  ([1,4,3,2],[4,3,2,1]),
                   'Right': ([1,4,3,2],[2,1,4,3])
                   }
        def _move(directive):
            b = self.sides[:]
            for i in range(0,4):
                b[directive[0][i]] = self.sides[directive[1][i]]
            self.sides = b
        _move(dirdir[direction])
    
    def top(self):
        return self.sides[0]

while 1:
    n = int(readline())
    if n == 0:
        exit(0)
    d = Dice()
    ans = d.top()
    for i in range(0,n):
        direction = readline()
        d.move(direction)
        ans += d.top()
    print ans

Submission status: Accepted

Solution code:
roll = {"North":("152304"), 
    "South":("402351"), 
    "East":("310542"), 
    "West":("215043"),
    "Right":("024135"),
    "Left":("031425")}
while True:
    n = int(input())
    if n == 0:
        break
    cnt = 1
    _dice = [i for i in range(1,7)]
    for i in range(n):
        _dice = [_dice[int(j)] for j in roll[input().strip()]]
        cnt+=_dice[0]
    print(cnt)

Submission status: Accepted

Solution code:
class diceClass:
	def __init__(self, valuelist):
		# [t, s, e, w, n, b]
		self.valuelist = valuelist
		self.t = valuelist[0]
		self.s = valuelist[1]
		self.e = valuelist[2]
		self.w = valuelist[3]
		self.n = valuelist[4]
		self.b = valuelist[5]

	def print(self):
		print('t={0}, s={1}, e={2}, w={3}, n={4}, b={5}'.format(self.t, self.s, self.e, self.w, self.n, self.b))

	def throw(self, direction):
		# N,S,W,E
		if direction == 'North':
			self.t, self.s, self.b, self.n = self.s, self.b, self.n, self.t
		elif direction == 'South':
			self.t, self.s, self.b, self.n = self.n, self.t, self.s, self.b
		elif direction == 'West':
			self.t, self.e, self.b, self.w = self.e, self.b, self.w, self.t
		elif direction == 'East':
			self.t, self.e, self.b, self.w = self.w, self.t, self.e, self.b
		elif direction == 'Right':
			self.s, self.e, self.w, self.n = self.e, self.n, self.s, self.w
		elif direction == 'Left':
			self.s, self.e, self.w, self.n = self.w, self.s, self.n, self.e


while True:
	n = int(input())
	if n == 0:
		break
	A = [1, 2, 3, 4, 5, 6]
	dice = diceClass(A)
	tsum = dice.t
	for i in range(n):
		s = input().strip()
		dice.throw(s)
		tsum = tsum + dice.t
	print(tsum)

Submission status: Accepted

Solution code:
while True:
  dice = [1,2,3,5,4,6]
  n = int(input())
  if n == 0:
    break
  a = 1
  while n>0:
    s = input()
    if s == 'North':
      dice[0],dice[1],dice[3],dice[5] = dice[1],dice[5],dice[0],dice[3]
    elif s == 'South':
      dice[1],dice[5],dice[0],dice[3] = dice[0],dice[1],dice[3],dice[5]
    elif s == 'East':
      dice[0],dice[2],dice[4],dice[5] = dice[4],dice[0],dice[5],dice[2]
    elif s == 'West':
      dice[4],dice[0],dice[5],dice[2] = dice[0],dice[2],dice[4],dice[5]
    elif s == 'Right':
      dice[1],dice[2],dice[3],dice[4] = dice[2],dice[3],dice[4],dice[1]
    else:
      dice[2],dice[3],dice[4],dice[1] = dice[1],dice[2],dice[3],dice[4]
    a += dice[0]
    n -= 1
  print(a)

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self):
        self.u = 1
        self.f = 2
        self.r = 3
        self.l = 4
        self.d = 6
        self.b = 5

    def rotate(self, d):
        cu, cf, cr, cl, cd, cb = self.u, self.f, self.r, self.l, self.d, self.b
        if d == 0:  # N
            self.u, self.f, self.d, self.b = cf, cd, cb, cu
        elif d == 1: # E
            self.u, self.r, self.d, self.l = cl, cu, cr, cd
        elif d == 2: # W
            self.u, self.r, self.d, self.l = cr, cd, cl, cu
        elif d == 3: # S
            self.u, self.f, self.d, self.b = cb, cu, cf, cd
        elif d == 4: # R
            self.f, self.r, self.b, self.l = cr, cb, cl, cf
        elif d == 5: # L
            self.f, self.r, self.b, self.l = cl, cf, cr, cb


while True:
    n = int(input())
    if n == 0:
        break

    dice = Dice()
    count = 1
    for _ in range(n):
        s = input()[0]
        if s == "N":
            dice.rotate(0)
        elif s == "E":
            dice.rotate(1)
        elif s == "W":
            dice.rotate(2)
        elif s == "S":
            dice.rotate(3)
        elif s == "R":
            dice.rotate(4)
        elif s == "L":
            dice.rotate(5)
        else:
            pass
        count += dice.u
    print(count)

Submission status: Accepted

Solution code:
M = ['North','East','West','South','Right','Left']
while 1:
    n = int(input())
    if n == 0:
        break
    D = [1,2,3]
    s = 1
    for i in range(n):
        m = input()
        if m == M[0]:
            D[0],D[1] = D[1],7-D[0]
        elif m == M[1]:
            D[0],D[2] = 7-D[2],D[0]
        elif m == M[2]:
            D[0],D[2] = D[2],7-D[0]
        elif m == M[3]:
            D[0],D[1] = 7-D[1],D[0]
        elif m == M[4]:
            D[1],D[2] = D[2],7-D[1]
        else:
            D[1],D[2] = 7-D[2],D[1]
        s += D[0]
    print (s)

Submission status: Accepted

Solution code:
def main():
    while True:
        N = int(input())
        if N == 0:
            return
        m = [1, 2, 3, 4, 5, 6]
        ans = 1
        for i in range(N):
            o = input()[0]
            if o == 'N':
                m = [m[1], m[5], m[2], m[3], m[0], m[4]]
            elif o == 'S':
                m = [m[4], m[0], m[2], m[3], m[5], m[1]]
            elif o == 'W':
                m = [m[2], m[1], m[5], m[0], m[4], m[3]]
            elif o == 'E':
                m = [m[3], m[1], m[0], m[5], m[4], m[2]]
            elif o == 'R':
                m = [m[0], m[2], m[4], m[1], m[3], m[5]]
            elif o == 'L':
                m = [m[0], m[3], m[1], m[4], m[2], m[5]]
            ans += m[0]

        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
U, F, R, L, B, D = range(6)
while True:
    n = int(input())
    if n == 0:
        break
    s = list(range(1, 6 + 1))
    result = 1
    for _ in range(n):
        raw = input()
        assert raw.find('\t') == -1
        op = raw.strip()
        if op == "North":
            s[U], s[F], s[D], s[B] = s[F], s[D], s[B], s[U]
        elif op == "South":
            s[U], s[F], s[D], s[B] = s[B], s[U], s[F], s[D]
        elif op == "West":
            s[U], s[R], s[D], s[L] = s[R], s[D], s[L], s[U]
        elif op == "East":
            s[U], s[R], s[D], s[L] = s[L], s[U], s[R], s[D]
        elif op == "Right":
            s[F], s[R], s[B], s[L] = s[R], s[B], s[L], s[F]
        elif op == "Left":
            s[F], s[R], s[B], s[L] = s[L], s[F], s[R], s[B]
        else:
            assert False
        result += s[U]
    print(result)

Submission status: Accepted

Solution code:
operation = {"North": (1, 5, 2, 3, 0, 4),
             "East": (3, 1, 0, 5, 4, 2),
             "West": (2, 1, 5, 0, 4, 3),
             "South": (4, 0, 2, 3, 5, 1),
             "Right": (0, 2, 4, 1, 3, 5),
             "Left": (0, 3, 1, 4, 2, 5)}

while True:
    n = int(input())
    if n == 0:
        break
    
    dice = list(range(1, 7))
    ans = 1
    for _ in range(n):
        indices = operation[input().strip()]
        dice = [dice[i] for i in indices]
        ans += dice[0]
        
    print(ans)

Submission status: Accepted

Solution code:
while True:
	try:
		list = [1, 2, 6, 5, 4, 3]
		list2 = [0, 0, 0, 0, 0, 0]
		judge = 1
		n = input()
		if n == 0:
			break
		else:
			for i in range(n):
				order = raw_input()
				if order == 'North':
					list2[0] = list[1]
					list2[1] = list[2]
					list2[2] = list[3]
					list2[3] = list[0]
					list2[4] = list[4]
					list2[5] = list[5]
				if order == 'East':
					list2[5] = list[0]
					list2[4] = list[2]
					list2[0] = list[4]
					list2[2] = list[5]
					list2[1] = list[1]
					list2[3] = list[3]
				if order == 'West':
					list2[4] = list[0]
					list2[5] = list[2]
					list2[2] = list[4]
					list2[0] = list[5]
					list2[1] = list[1]
					list2[3] = list[3]
				if order == 'South':
					list2[1] = list[0]
					list2[2] = list[1]
					list2[3] = list[2]
					list2[0] = list[3]
					list2[4] = list[4]
					list2[5] = list[5]
				if order == 'Right':
					list2[4] = list[1]
					list2[5] = list[3]
					list2[3] = list[4]
					list2[1] = list[5]
					list2[0] = list[0]
					list2[2] = list[2]
				if order == 'Left':
					list2[5] = list[1]
					list2[4] = list[3]
					list2[1] = list[4]
					list2[3] = list[5]
					list2[0] = list[0]
					list2[2] = list[2]
				list = [list2[0], list2[1], list2[2], list2[3], list2[4], list2[5]]
				judge += list[0]
		print judge
	except EOFError:
		break

Submission status: Runtime Error

Solution code:
def input_n(infile,outfile):
	while True:
		n = int(infile.readline())
		if n == 0:
			break
		dice(infile,outfile,n)

def dice(infile,outfile,n):
	point = 1
	spot = 1
	east = 3
	south = 2
	for i in range(n):
		buff = spot
		command = infile.readline()
		if command == "North":
			point += south
			spot = south
			south = (7 - buff)
		elif command == "South":
			point += (7 - south)
			spot = (7 - south)
			south = buff
		elif command == "East":
			point += (7 - east)
			spot = (7 - east)
			east = buff
		elif command == "West":
			point += east
			spot = east
			east = (7 - buff)
		elif command == "Right":
			remember = south
			south = east
			east = (7 - remember)
		elif command == "Left":
			remember = east
			east = south
			south = (7 - remember)
		else:
			break
	print >> outfile,point

if __name__ == "__main__"
import sys
input_n(sys.stdin, sys.stdout)

Submission status: Runtime Error

Solution code:
def input_n(infile,outfile):
	while True:
		n = int(infile.readline())
		if n == 0:
			break
		dice(infile,outfile,n)

def dice(infile,outfile,n):
	point = 1
	top = 1
	east = 3
	south = 2
	for i in range(n):
		buff = top
		command = infile.readline()
		if command == "North":
			point += south
			top = south
			south = (7 - buff)
		elif command == "South":
			point += (7 - south)
			top = (7 - south)
			south = buff
		elif command == "East":
			point += (7 - east)
			top = (7 - east)
			east = buff
		elif command == "West":
			point += east
			top = east
			east = (7 - buff)
		elif command == "Right":
			remember = south
			south = east
			east = (7 - remember)
		elif command == "Left":
			remember = east
			east = south
			south = (7 - remember)
		else:
			break
	print >> outfile,point

if __name__ == "__main__":
	import sys
	input_n(sys.stdin, sys.stdout)

Submission status: Runtime Error

Solution code:
def North(ls):
    ls[1-1],ls[2-1],ls[5-1],ls[6-1] = ls[2-1],ls[6-1],ls[1-1],ls[5-1]
def East(ls):
    ls[1-1],ls[3-1],ls[4-1],ls[6-1] = ls[4-1],ls[1-1],ls[6-1],ls[3-1]
def West(ls):
    ls[1-1],ls[3-1],ls[4-1],ls[6-1] = ls[3-1],ls[6-1],ls[1-1],ls[4-1]
def South(ls):
    ls[1-1],ls[2-1],ls[5-1],ls[6-1] = ls[5-1],ls[1-1],ls[6-1],ls[2-1]
def Right(ls):
    ls[2-1],ls[3-1],ls[4-1],ls[5-1] = ls[3-1],ls[5-1],ls[2-1],ls[4-1]
def Left(ls):
    ls[2-1],ls[3-1],ls[4-1],ls[5-1] = ls[4-1],ls[2-1],ls[5-1],ls[3-1]

while True:
    n = int(raw_input())
    dice = [1,2,3,4,5,6] 
    # top, south, east, west, north, bottom
    direction = ""
    total = 1
    for k in range(n):
        direction = str(raw_input())
        if direction=="North":
            North(dice)
        elif direction=="East":
            East(dice)
        elif direction=="West":
            West(dice)
        elif direction=="South":
            South(dice)
        elif direction=="Right":
            Right(dice)
        elif direction=="Left":
            Left(dice)
        total += dice[0]
    print total

Submission status: Runtime Error

Solution code:
while True:
    try:
    
        n = int(raw_input())
        if n==0:
            break
        dice = [1,2,3,4,5,6] 
        # top, south, east, west, north, bottom
        direction = ""
        total = 1
        for k in range(n):
            direction = str(raw_input())
            if direction=="North":
                North(dice)
            elif direction=="East":
                East(dice)
            elif direction=="West":
                West(dice)
            elif direction=="South":
                South(dice)
            elif direction=="Right":
                Right(dice)
            elif direction=="Left":
                Left(dice)
            total += dice[0]
        print total
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python

import copy

x = ['Front', 'Right', 'Back', 'Left']
y = ['Top', 'Front', 'Bottom', 'Back']
z = ['Top', 'Left', 'Bottom', 'Right']


def turn(dice, direction):
    turned_dice = copy.deepcopy(dice)
    # x?????????
    if direction == 'Right':
        for i in range(4):
            turned_dice[x[i]] = dice[x[(i+1)%4]]
    if direction == 'Left':
        for i in range(4):
            turned_dice[x[i]] = dice[x[(i-1)%4]]

    # y?????????
    if direction == 'North':
        for i in range(4):
            turned_dice[y[i]] = dice[y[(i+1)%4]]
    if direction == 'South':
        for i in range(4):
            turned_dice[y[i]] = dice[y[(i-1)%4]]

    # z?????????
    if direction == 'East':
        for i in range(4):
            turned_dice[z[i]] = dice[z[(i+1)%4]]
    if direction == 'West':
        for i in range(4):
            turned_dice[z[i]] = dice[z[(i-1)%4]]

    return turned_dice

if __name__ == '__main__':
    while True:
        n = int(input())
        if n == 0:
            break
        dice = {'Top': 1, 'Front': 2, 'Right': 3,
                'Left': 4, 'Back': 5, 'Bottom': 6}

        ret = 1
        for i in range(n):
            dice = turn(dice, input())
            ret += dice['Top']

        print(ret)

Submission status: Runtime Error

Solution code:
# -*- coding:shift-jis -*-

class Dice:

	def __init__(self):
		self.top = 1
		self.south = 2
		self.north = 5
		self.east = 3
		self.west = 4
		self.bottom = 6

	def rotate(self,operation):
		if operation == "North":
			self.top,self.north,self.bottom,self.south = self.south,self.top,self.north,self.bottom
		elif operation == "East":
			self.top,self.east,self.bottom,self.west = self.west,self.top,self.east,self.bottom
		elif operation == "West":
			self.top,self.east,self.bottom,self.west = self.east,self.bottom,self.west,self.top
		elif operation == "South":
			self.top,self.north,self.bottom,self.south = self.north,self.bottom,self.south,self.top
		elif operation == "Right":
			self.north,self.east,self.south,self.west = self.west,self.north,self.east,self.south
		elif operation == "Left":
			self.north,self.east,self.south,self.west = self.east,self.south,self.west,self.north,

n = int(input()[0])

while n!=0:
	inputs = [input() for _ in range(n)]
	value = 1
	d = Dice()
	for i in inputs:
		d.rotate(i)
		value += d.top
	print(value)
	n = int(input())

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

n = raw_input()
while N != 0:
    a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
    s = 0
    for i in range(n):
        op = raw_input()
        if op = "North":
            a, b, f, d = b, f, d, a
        elif op = "East":
            a, c, f, e = e, a, c, f
        elif op = "West":
            a, e, f, c = c, a, e, f
        elif op = "South":
            a, b, f, d = d, a, b, f
        elif op = "Right":
            b, c, d, e = c, d, e, b
        elif op = "Left":
            b, c, d, e = e, b, c, d
        s += a
    print s
    n = raw_input()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

n = raw_input()
while n != 0:
    a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
    s = 0
    for i in range(n):
        op = raw_input()
        if op = "North":
            a, b, f, d = b, f, d, a
        elif op = "East":
            a, c, f, e = e, a, c, f
        elif op = "West":
            a, e, f, c = c, a, e, f
        elif op = "South":
            a, b, f, d = d, a, b, f
        elif op = "Right":
            b, c, d, e = c, d, e, b
        elif op = "Left":
            b, c, d, e = e, b, c, d
        s += a
    print s
    n = raw_input()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

n = raw_input()
while n != 0:
    a, b, c, d, e, f = 1, 2, 3, 4, 5, 6
    s = 0
    for i in range(n):
        op = raw_input()
        if op == "North":
            a, b, f, d = b, f, d, a
        elif op == "East":
            a, c, f, e = e, a, c, f
        elif op == "West":
            a, e, f, c = c, a, e, f
        elif op == "South":
            a, b, f, d = d, a, b, f
        elif op == "Right":
            b, c, d, e = c, d, e, b
        elif op == "Left":
            b, c, d, e = e, b, c, d
        s += a
    print s
    n = raw_input()