Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

  You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

 In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

  In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

  For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6



Sample input:
1 2 3 4 5 6
3
6 5
1 3
3 2


Sample output:
3
5
6

Submission status: Accepted

Solution code:
class dice:
    def __init__(self):
        self.d = [0] + list(map(int, input().split()))
        
    def move(self):
        dir = list(input())
        up = [0,1,2,3] 
        for a in self.dir:
            if a == "E":
                up = [0, 7-up[3], up[2], up[1]]
            elif a == "W":
                up = [0, up[3], up[2], 7-up[1]]
            elif a == "N":
                up = [0, up[2], 7-up[1], up[3]]
            else:
                up = [0, 7-up[2], up[1], up[3]]
        print(self.d[up[1]])
        
    def solve(self):
        top, front = map(int, input().split())
        T = self.d.index(top)
        F = self.d.index(front)
        L =[[1,2,3], [2,6,3], [6,5,3], [5,1,3], [2,3,1], [3,5,1], [5,4,1], [4,2,1], [1,4,2], [4,6,2], [6,3,2], [3,1,2]]
        for l in L:
            if l[0] == T and l[1] == F:
                print(self.d[l[2]])
                break
            elif l[1] == T and l[0] == F:
                print(self.d[7-l[2]])
                break

ob = dice()
for x in range(int(input())):
    ob.solve()

Submission status: Accepted

Solution code:
# Dice I

d = [int(i) for i in input().rstrip().split()]

class Dice:
    def __init__(self, a1, a2, a3, a4, a5, a6):
        # サイコロを縦横にたどると書いてある数字(index1は真上、index3は真下の数字)
        self.face = [a1, a2, a3, a4, a5, a6]
        self.v = [a5, a1, a2, a6] # 縦方向
        self.h = [a4, a1, a3, a6] # 横方向
        self.det = 1
        # print(self.v, self.h)

    # サイコロの上面の数字を表示
    def top(self):
        return self.v[1]

    # サイコロを北方向に倒す
    def north(self):
        newV = [self.v[1], self.v[2], self.v[3], self.v[0]]
        self.v = newV
        self.h[1] = self.v[1]
        self.h[3] = self.v[3]
        return self.v, self.h

    # サイコロを南方向に倒す
    def south(self):
        newV = [self.v[3], self.v[0], self.v[1], self.v[2]]
        self.v = newV
        self.h[1] = self.v[1]
        self.h[3] = self.v[3]
        return self.v, self.h

    # サイコロを東方向に倒す
    def east(self):
        newH = [self.h[3], self.h[0], self.h[1], self.h[2]]
        self.h = newH
        self.v[1] = self.h[1]
        self.v[3] = self.h[3]
        return self.v, self.h

    # サイコロを西方向に倒す
    def west(self):
        newH = [self.h[1], self.h[2], self.h[3], self.h[0]]
        self.h = newH
        self.v[1] = self.h[1]
        self.v[3] = self.h[3]
        return self.v, self.h

    def searchFace(self, a):
        b = 0
        for i in range(6):
            if a == self.face[i]:
                # print('一致しました')
                b = i + 1
        return b

    def smallen(self, x): # a は 1 から 6 のどれか
        y = int(7 / 2 - abs(x - 7 / 2))
        if x != y:
            self.det *= -1
        # print(self.det)
        return y

    def rightSide(self, top, front):
        r = 0
        if top == 1 and front == 2:
            r = 3
        elif top == 2 and front == 3:
            r = 1
        elif top == 3 and front == 1:
            r = 2
        elif top == 1 and front == 3:
            r = 5
        elif top == 3 and front == 2:
            r = 6
        elif top == 2 and front == 1:
            r = 4
        if self.det == -1:
            r = 7 - r
        return r

dice1 = Dice(d[0], d[1], d[2], d[3], d[4], d[5])

# command = list(input().rstrip())
# print(command)

# for i, a in enumerate(command):
#     if a == 'N':
#         dice1.north()
#     elif a == 'S':
#         dice1.south()
#     elif a == 'E':
#         dice1.east()
#     elif a == 'W':
#         dice1.west()
# print(dice1.top())

questionAmount = int(input())
for i in range(questionAmount):
    # Initialize det
    dice1.det = 1
    question = [int(i) for i in input().rstrip().split()]
    a = dice1.searchFace(question[0])
    b = dice1.searchFace(question[1])
    # print(a, b)
    top = dice1.smallen(a)
    front = dice1.smallen(b)
    # print(top, front)
    position = dice1.rightSide(top, front)
    answer = dice1.face[position - 1]
    print(answer)

Submission status: Accepted

Solution code:
_rule = {'N':(1,5,2,3,0,4),'S':(4,0,2,3,5,1),'E':(0,3,1,4,2,5),'W':(0,2,4,1,3,5)}
_idx = {'top':0, 'front':1, 'right':2, 'left':3, 'back':4, 'bottom':5}
_idxmap = ((1,2),(1,1),(2,1),(0,1),(3,1),(1,0))

def motion(dlist, direction):
    templist = dlist
    for i in range(len(direction)):
        templist = [templist[r] for r in _rule[direction[i]]]
    return templist

def stopmotion(dlist, num, distination):
    return dlist.index(num) == _idxmap[distination]
    
def search(dlist, top, front):
    ret = ''
    prepos = _idxmap[dlist.index(top)]
    topxpos, topypos = _idxmap[0]
    xpos = topxpos - prepos[0]
    ypos = topypos - prepos[1]
    if xpos <= 0:
        ret = 'W' * abs(xpos)
    else:
        ret = 'E' * abs(xpos)
    ret += 'N' * abs(ypos)
    
    templist = motion(x, ret)
    
    ret = ''
    prepos = _idxmap[templist.index(front)]
    frontxpos, frontypos = _idxmap[1]
    xpos = frontxpos - prepos[0]
    if xpos <= 0:
        ret = 'W' * abs(xpos)
    else:
        ret = 'E' * abs(xpos)
    return motion(templist, ret)
        
x = input().split()

for _ in range(int(input())):
    top, front = input().split()
    templist = search(x, top, front)
    print(templist[_idx['right']])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,numbers):
        self.surface=numbers

    def find_right_side(self,top,flont):
        tf=str(self.surface.index(top))+str(self.surface.index(flont))
        if tf in '12431':
            return int(self.surface[0])
        elif tf in '03520':
            return int(self.surface[1])
        elif tf in '01540':
            return int(self.surface[2])
        elif tf in '04510':
            return int(self.surface[3])
        elif tf in '02530':
            return int(self.surface[4])
        else:
            return int(self.surface[5])

dice=Dice(input().split())
q=int(input())
for i in range(q):
    top,flont=input().split()
    print(dice.find_right_side(top,flont))

Submission status: Accepted

Solution code:
face = list(map(int,input().split()))
def Nroll(list):
	a = [list[1],list[5],list[2],list[3],list[0],list[4]]
	return a
def Wroll(list):
	a = [list[2],list[1],list[5],list[0],list[4],list[3]]
	return a
q = int(input())
ql = []
tes = [0,1,2,3,4,5]
for i in range(q):
	qk = list(map(int,input().split()))
	for ii in range(2):
		tes = Wroll(tes)
		for k in range(4):
			tes = Nroll(tes)
			for kk in range(4):
				tes = Wroll(tes)
				if tes[0] == face.index(qk[0]) and tes[1] == face.index(qk[1]):
					ql.append(face[tes[2]])
					break
			else:
				continue
			break
		else:
			continue
		break
for ss in ql:
	print(ss)

Submission status: Accepted

Solution code:
#11_A
import random
class Dice:
    def __init__(self):
        self.dice=[i for i in range(6)]
        self.workspace=[i for i in range(6)]
        self.top=self.dice=[0]
        
    def set(self,numbers):
        self.dice=[i for i in numbers]
        self.workspace=[i for i in numbers]
        
    def rotate(self,command):
        if command == "W":
            self.workspace=[self.dice[2], self.dice[1], self.dice[5], self.dice[0], self.dice[4], self.dice[3]]
            self.dice=self.workspace
        elif command == "E":
            self.workspace=[self.dice[3], self.dice[1], self.dice[0], self.dice[5], self.dice[4], self.dice[2]]
            self.dice = self.workspace
        
        if command == "S":
            self.workspace=[self.dice[4], self.dice[0], self.dice[2], self.dice[3], self.dice[5], self.dice[1]]
            self.dice=self.workspace
        elif command == "N":
            self.workspace=[self.dice[1], self.dice[5], self.dice[2], self.dice[3], self.dice[0], self.dice[4]]
            self.dice=self.workspace
            
    def get_top(self):
        self.top=self.dice[0]
        print(self.top)
        
    def predict(self, num):
        for i in range(num):
            top, front = map(int, input().split())
            while(True):
                if top == self.dice[0] and front == self.dice[1]:
                    break
                command_num=random.randint(1,4)
                if command_num == 1:
                    self.rotate("W")
                elif command_num == 2:
                    self.rotate("E")
                elif command_num == 3:
                    self.rotate("S")
                elif command_num == 4:
                    self.rotate("N")
            print(self.dice[2])
            
    def getter(self):
        return self.dice[0], self.dice[1], self.dice[2]
    
    def predict_same(self, top, front, right):
        for n in range(30):
            if top ==self.dice[0] and front== self.dice[1] and right ==self.dice[2]:
                print("Yes")
                break
            command_num=random.randint(1,4)
            if command_num == 1:
                    self.rotate("W")
            elif command_num == 2:
                    self.rotate("E")
            elif command_num == 3:
                    self.rotate("S")
            elif command_num == 4:
                    self.rotate("N")
        
        if top!=self.dice[0] or front!=self.dice[1] or front!=self.dice[2]:
            print("No")
        
            
                
    
        
numbers=list(map(int,input().split()))
dice=Dice()
dice.set(numbers)
# commands=input()
# for i in range(len(commands)):
#     dice.rotate(commands[i])
# dice.get_top()
q=int(input())
dice.predict(q)

Submission status: Accepted

Solution code:
def N(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[1]
  t1[1]=d1[5]
  t1[2]=d1[2]
  t1[3]=d1[3]
  t1[4]=d1[0]
  t1[5]=d1[4]
  return t1
def S(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[4]
  t1[1]=d1[0]
  t1[2]=d1[2]
  t1[3]=d1[3]
  t1[4]=d1[5]
  t1[5]=d1[1]
  return t1
def E(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[3]
  t1[1]=d1[1]
  t1[2]=d1[0]
  t1[3]=d1[5]
  t1[4]=d1[4]
  t1[5]=d1[2]
  return t1
def W(d1):
  t1=[0 for i in range(6)]
  t1[0]=d1[2]
  t1[1]=d1[1]
  t1[2]=d1[5]
  t1[3]=d1[0]
  t1[4]=d1[4]
  t1[5]=d1[3]
  return t1
d1=[0 for i in range(6)]
d1[:]=(int(x) for x in input().split())
q=int(input())

for i in range(q):
  t,f=(int(x) for x in input().split())
  if d1[1]==t:
    d1=N(d1)
  elif d1[2]==t:
    d1=W(d1)
  elif d1[3]==t:
    d1=E(d1)
  elif d1[4]==t:
    d1=S(d1)
  elif d1[5]==t:
    d1=N(d1)
    d1=N(d1)
  
  if d1[2]==f:
    d1=W(d1)
    d1=S(d1)
    d1=E(d1)
  elif d1[3]==f:
    d1=E(d1)
    d1=S(d1)
    d1=W(d1)
  elif d1[4]==f:
    d1=E(d1)
    d1=S(d1)
    d1=S(d1)
    d1=W(d1)
  print(d1[2])

Submission status: Accepted

Solution code:
class Dice():
    
    def __init__(self, labels):
        self.__pip = labels
        
    def roll_e(self): # 0352 → 3520
        self.__pip[0], self.__pip[3], self.__pip[5], self.__pip[2] = \
            self.__pip[3], self.__pip[5], self.__pip[2], self.__pip[0]
    
    def roll_n(self): # 0154 → 1540 
        self.__pip[0], self.__pip[1], self.__pip[5], self.__pip[4] = \
            self.__pip[1], self.__pip[5], self.__pip[4], self.__pip[0]

    def roll_s(self): # 0451 →  4510
        self.__pip[0], self.__pip[4], self.__pip[5], self.__pip[1] = \
            self.__pip[4], self.__pip[5], self.__pip[1], self.__pip[0]

    def roll_w(self): # 0253 → 2530
        self.__pip[0], self.__pip[2], self.__pip[5], self.__pip[3] = \
            self.__pip[2], self.__pip[5], self.__pip[3], self.__pip[0]

    def roll_nn(self): # 0154 → 5401
        self.roll_n()
        self.roll_n()
        
    def turn_dice(self): # 1243 → 2431
        self.__pip[1], self.__pip[2], self.__pip[4], self.__pip[3] = \
            self.__pip[2], self.__pip[4], self.__pip[3], self.__pip[1]
            
    def get_right(self, top, front):
        if top != self.__pip[0]:
            if top == self.__pip[1]:
                self.roll_n()
            elif top == self.__pip[2]:
                self.roll_w()
            elif top == self.__pip[3]:
                self.roll_e()
            elif top == self.__pip[4]:
                self.roll_s()
            else:
                self.roll_nn()
        for i in range(3):
            if front == self.__pip[1]:
                break
            self.turn_dice()
        return self.__pip[2]
        
        
def main():
    dice = Dice(list(input().split()))
    n = int(input())
    for i in range(n):
        top, front = input().split()
        right = dice.get_right(top, front)
        print("{}".format(right))
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,dice):
        self.dice = dice
        
    
    
    def dice_num(self,a,b):
        if (a==2 and b == 4) or (a==3 and b==1) or (a==4 and b==3) or (a==1 and b== 2):
            return self.dice[0]
        elif (a==2 and b == 0) or (a==0 and b==3) or (a==3 and b==5) or (a==5 and b== 2):
            return self.dice[1]
        elif (a==4 and b == 0) or (a==0 and b==1) or (a==5 and b==4) or (a==1 and b== 5):
            return self.dice[2]
        elif (a==0 and b == 4) or (a==4 and b==5) or (a==5 and b==1) or (a==1 and b== 0):
            return self.dice[3]
        elif (a==2 and b == 5) or (a==3 and b==0) or (a==0 and b==2) or (a==5 and b== 3):
            return self.dice[4]
        else:
            return self.dice[5]
            
    
    
dice = list(map(int,input().split()))

n = int(input())
        
for _ in range(n):
    a,b = map(int,input().split())
    a_num = dice.index(a)
    b_num = dice.index(b)
    dice_2 = Dice(dice)
    print(dice_2.dice_num(a_num,b_num))

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,u,w,r,s,t,y):
        self.s1 = u
        self.s2= w
        self.s3= r
        self.s4= s
        self.s5 = t
        self.s6= y
    def right_s3(self):
        prev_s3 = self.s3
        self.s3 = prev_s3
    def right_s4(self):
        prev_s4 = self.s4
        self.s3 = prev_s4
    def right_s5(self):
        prev_s5 = self.s5
        self.s3 = prev_s5
    def right_s6(self):
        prev_s6 = self.s6
        self.s3 = prev_s6
    def right_s1(self):
        prev_s1 = self.s1
        self.s3 = prev_s1
    def right_s2(self):
        prev_s2 = self.s2
        self.s3 = prev_s2
    def top(self):
        return self.s3
s1,s2,s3,s4,s5,s6 = map(int,input().split())
order = int(input())
for c in range(order):
    dice = Dice(s1,s2,s3,s4,s5,s6)
    new_s1 , new_s2 = map(int,input().split())
    if (new_s1 == s1 and new_s2 == s2) or (new_s1 ==s2 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s5) or (new_s1==s5 and new_s2 == s1):
        dice.right_s3()
    elif (new_s1 == s1 and new_s2 == s5) or (new_s1 ==s5 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s2) or (new_s1==s2 and new_s2 == s1):
        dice.right_s4()
    elif (new_s1 == s1 and new_s2 == s3) or (new_s1 ==s3 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s4) or (new_s1==s4 and new_s2 == s1):
        dice.right_s5()
    elif (new_s1 == s2 and new_s2 == s4) or (new_s1 ==s4 and new_s2 == s5) or (new_s1 == s5 and new_s2 ==s3) or (new_s1==s3 and new_s2 == s2):
        dice.right_s6()
    elif (new_s1 == s2 and new_s2 == s3) or (new_s1 ==s3 and new_s2 == s5) or (new_s1 == s5 and new_s2 ==s4) or (new_s1==s4 and new_s2 == s2):
        dice.right_s1()
    elif (new_s1 == s1 and new_s2 == s4) or (new_s1 ==s4 and new_s2 == s6) or (new_s1 == s6 and new_s2 ==s3) or (new_s1==s3 and new_s2 == s1):
        dice.right_s2()
    print(dice.top())

Submission status: Accepted

Solution code:
N = list(map(int,input().split()))
q = int(input())
S = []
S += [42,23,35,54],
S += [14,46,63,31],
S += [12,26,65,51],
S += [21,15,56,62],
S += [13,36,64,41],
S += [24,45,53,32],
for i in range(q):
    a,b = map(int,input().split())
    c = [str(N.index(a)+1)]
    c += str(N.index(b)+1),
    c = "".join(c)
    for j in range(6):
        if int(c) in S[j]:
            print(N[j])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,dim=list(range(1,7))):
        self.dim = dim

    def roll(self,direction):
        if direction == "N":
            buff = self.dim[0]
            self.dim[0] = self.dim[1]
            self.dim[1] = self.dim[5]
            self.dim[5] = self.dim[4]
            self.dim[4] = buff

        if direction == "E":
            buff = self.dim[3]
            self.dim[3] = self.dim[5]
            self.dim[5] = self.dim[2]
            self.dim[2] = self.dim[0]
            self.dim[0] = buff

        if direction == "W":
            buff = self.dim[0]
            self.dim[0] = self.dim[2]
            self.dim[2] = self.dim[5]
            self.dim[5] = self.dim[3]
            self.dim[3] = buff

        if direction == "S":
            buff = self.dim[0]
            self.dim[0] = self.dim[4]
            self.dim[4] = self.dim[5]
            self.dim[5] = self.dim[1]
            self.dim[1] = buff

lst = list(map(int,input().split()))

D = Dice(lst)
q = int(input())
for i in range(q):
    top_front = [int(x) for x in input().split()]
    for op in 'EEENEEENEEESEEESEEENEEEN':
        if D.dim[0] == top_front[0] and D.dim[2] == top_front[1]:
            print(D.dim[4])
            break
        D.roll(op)

Submission status: Accepted

Solution code:
#サイコロの問題B
class Dice:
    def __init__(self, input_dice):
        self.flow = [input_dice[3], input_dice[1], input_dice[0], input_dice[5], input_dice[2], input_dice[4]]
        self.dice = [input_dice[3], input_dice[1], input_dice[0], input_dice[5], input_dice[2], input_dice[4]]
  
    def setNumber(self, n0, n1, n2, n3, n4, n5):
        self.dice[0] = n0
        self.dice[1] = n1
        self.dice[2] = n2
        self.dice[3] = n3
        self.dice[4] = n4
        self.dice[5] = n5
        return self.dice
    
    def roll(self, dir):
        if dir == 'S':
            self.setNumber(self.flow[0], self.flow[2], self.flow[5], self.flow[1], self.flow[4], self.flow[3])
            self.flow = self.dice
            return self.dice
        if dir == 'N':
            self.setNumber(self.flow[0], self.flow[3], self.flow[1], self.flow[5], self.flow[4], self.flow[2])
            self.flow = self.dice
            return self.dice
        if dir == 'W':
            self.setNumber(self.flow[2], self.flow[1], self.flow[4], self.flow[0], self.flow[3], self.flow[5])
            self.flow = self.dice
            return self.dice
        if dir == 'E':
            self.setNumber(self.flow[3], self.flow[1], self.flow[0], self.flow[4], self.flow[2], self.flow[5])
            self.flow = self.dice
            return self.dice

    def right_side(self, ue, mae):
        #以下メモ書き[1,2,3,4,5,6]の入力が与えられたとする
        #以下南北方向を軸に回転させる
        #最初2が前面
        i = 0
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        #1を前面に持ってきて回転させる
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        #5を前面に持ってきて回転させる
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        #6を前面に持ってきて回転させる
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        #4を前面に持ってきて回転させる
        self.roll('E')
        self.roll('S')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break
        #3を前面に持ってきて回転させる
        self.roll('N')
        self.roll('N')
        for i in range(4):
            self.roll('W')
            if self.dice[2] == ue and self.dice[1] == mae:
                print(self.dice[4])
                break

dice = list(map(int, input().split()))
n = int(input())
ue = [0]*n
mae = [0]*n
for i in range(n):
    ue[i], mae[i] = map(int, input().split())

d = Dice(dice)
for i in range(n):
    d.right_side(ue[i], mae[i])

Submission status: Accepted

Solution code:
from random import choice

class dice:
    def __init__(self, X):
        self.x = [0] * 6
        self.x[0] = X[0]
        self.x[1] = X[1]
        self.x[2] = X[2]
        self.x[3] = X[3]
        self.x[4] = X[4]
        self.x[5] = X[5]

    def roll(self, d):
        if d == 'S':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[4], self.x[0], self.x[2], self.x[3], self.x[5], self.x[1]
        elif d == 'E':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[3], self.x[1], self.x[0], self.x[5], self.x[4], self.x[2]
        elif d == 'W':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[2], self.x[1], self.x[5], self.x[0], self.x[4], self.x[3]
        elif d == 'N':
            self.x[0], self.x[1], self.x[2], self.x[3], self.x[4], self.x[5] = \
            self.x[1], self.x[5], self.x[2], self.x[3], self.x[0], self.x[4]

X = list(map(int, input().split()))
n = int(input())
dice1 = dice(X)
distances = ['S', 'E', 'W', 'N']
for i in range(n):
    x1, x2 = map(int, input().split())
    while True:
        d = choice(distances)
        dice1.roll(d)
        if dice1.x[0] == x1 and dice1.x[1] == x2:
            print(dice1.x[2])
            break

Submission status: Accepted

Solution code:
import random

class Dice():
    def __init__(self, numbers):
        self.numbers = numbers
        S = [1, 5, 2, 3, 0, 4]
        N = [4, 0, 2, 3, 5, 1]
        W = [3, 1, 0, 5, 4, 2]
        E = [2, 1, 5, 0, 4, 3]
        self.tumble = {0: S, 1: N, 2: W, 3: E}
        
    def rol(self, command):
        prev = self.numbers.copy()
        for i, t in enumerate(self.tumble[command]):
            self.numbers[t] = prev[i]
                
numbers = list(map(int, input().split()))
q = int(input())

dice = Dice(numbers)
for _ in range(q):
    query = list(map(int, input().split()))
    while True:
        dice.rol(random.randint(0, 3))
        if dice.numbers[0] == query[0] and dice.numbers[1] == query[1]:
            print(dice.numbers[2])
            break

Submission status: Accepted

Solution code:

def dice():
    import random as ran

    dnum = list(map(int,input().split()))
    q = int(input())
    up = dnum[0]
    un = dnum[5]
    ri = dnum[2]
    le = dnum[3]
    fr = dnum[1]
    ba = dnum[4]
    tmp = int(0)
    tmp2 = int(0)
    order = ["S","N","E","W"]

    for i in range(q):
        qup,qfr = (int(x) for x in input().split())
        while True :
            x = ran.choice(order)
            if (x == "S"):
                tmp = fr
                tmp2 = un
                fr = up
                up = ba
                un = tmp
                ba = tmp2

            elif (x == "N"):
                tmp = un
                tmp2 = up
                up = fr
                un = ba
                fr = tmp
                ba = tmp2

            elif (x == "E"):
                tmp = up
                tmp2 = un
                up = le
                un = ri
                ri = tmp
                le = tmp2

            elif (x == "W"):
                tmp = un
                tmp2 = up
                up = ri
                un = le
                ri = tmp
                le = tmp2

            if (up == qup and fr == qfr):
                print(ri)
                break
dice()

Submission status: Accepted

Solution code:
def rollDice(dice, direction):
    buf = [x for x in dice]
    if direction == "S":
        buf[0] = dice[4]
        buf[1] = dice[0]
        buf[4] = dice[5]
        buf[5] = dice[1]
    elif direction == "E":
        buf[0] = dice[3]
        buf[2] = dice[0]
        buf[3] = dice[5]
        buf[5] = dice[2]
    elif direction == "W":
        buf[0] = dice[2]
        buf[2] = dice[5]
        buf[3] = dice[0]
        buf[5] = dice[3]
    elif direction == "N":
        buf[0] = dice[1]
        buf[1] = dice[5]
        buf[4] = dice[0]
        buf[5] = dice[4]

    for i in range(6):
        dice[i] = buf[i]


def rollDiceSide(dice):
    buf = [x for x in dice]
    buf[1] = dice[2]
    buf[2] = dice[4]
    buf[3] = dice[1]
    buf[4] = dice[3]

    for i in range(6):
        dice[i] = buf[i]


d = list(map(int, input().split()))
n = int(input())

for i in range(n):
    q1, q2 = list(map(int, input().split()))

    wq = d.index(q1)
    if wq == 1:
        rollDice(d, "N")
    elif wq == 2:
        rollDice(d, "W")
    elif wq == 3:
        rollDice(d, "E")
    elif wq == 4:
        rollDice(d, "S")
    elif wq == 5:
        rollDice(d, "S")
        rollDice(d, "S")

    wq = d.index(q2)
    if wq == 2:
        rollDiceSide(d)
    elif wq == 3:
        rollDiceSide(d)
        rollDiceSide(d)
        rollDiceSide(d)
    elif wq == 4:
        rollDiceSide(d)
        rollDiceSide(d)

    print(d[2])

Submission status: Accepted

Solution code:
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

#0:中央 1:南 2:東 3:西 4:北 5:裏側
class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'

    def setNumber(self,n0,n1,n2,n3,n4,n5):
        self.number[0] = n0
        self.number[1] = n1
        self.number[2] = n2
        self.number[3] = n3
        self.number[4] = n4
        self.number[5] = n5


    def roll(self,loc):
        for i in range(6):
            self.work[i] = self.number[i]

        if loc == 'E':
            self.setNumber(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
        elif loc == 'N':
            self.setNumber(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
        elif loc == 'S':
            self.setNumber(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
        elif loc == 'W':
            self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])


    def query(self,top_num,front_num):

        self.save_data = [i for i in range(6)]
        #元の値の保存
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]

        ret = -1;

        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break

        #元の値の復元
        for i in range(len(self.number)):
            self.number[i] = self.save_data[i]

        return ret;




dice = Dice()
table = list(map(int,input().split()))
dice.setNumber(table[0], table[1], table[2], table[3], table[4], table[5])

num_query = int(input())
for loop in range(num_query):
    top_num,front_num = map(int,input().split())
    print("%d"%(dice.query(top_num, front_num)))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

class Dice:
    def __init__(self,lst):
        self.face=lst
        self.order="NNNNWNNNWNNNENNNENNNWNNN"
    
    def roll(self,x):
        tmp=self.face
        if x=="E":
            self.face=[tmp[3],tmp[1],tmp[0],tmp[5],tmp[4],tmp[2]]
        if x=="W":
            self.face=[tmp[2],tmp[1],tmp[5],tmp[0],tmp[4],tmp[3]]
        if x=="N":
            self.face=[tmp[1],tmp[5],tmp[2],tmp[3],tmp[0],tmp[4]]
        if x=="S":
            self.face=[tmp[4],tmp[0],tmp[2],tmp[3],tmp[5],tmp[1]]
    
    def showDown(self):
        return self.face[0]
    
    def query(self,lst):
        tmp=self.face
        ans=-1
        for i in self.order:
            self.roll(i)
            if self.face[0]==lst[0] and self.face[1]==lst[1]:
                ans=self.face[2]
                break
        self.face=tmp
        return ans
    
dice=Dice(list(map(int,input().split(" "))))
num=int(input())
q=[list(map(int,input().split())) for i in range(num)]
for i in q:
    print(dice.query(i))

Submission status: Accepted

Solution code:
# 10_*
class Dice:
    def __init__(self, label: list):
        self.top, self.front, self.right, self.left, self.back, self.bottom = label

    def roll(self, direction: str):
        if direction == "N":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.front,
                self.bottom,
                self.right,
                self.left,
                self.top,
                self.back,
            )
        elif direction == "W":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.right,
                self.front,
                self.bottom,
                self.top,
                self.back,
                self.left,
            )
        elif direction == "S":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.back,
                self.top,
                self.right,
                self.left,
                self.bottom,
                self.front,
            )
        elif direction == "E":
            self.top, self.front, self.right, self.left, self.back, self.bottom = (
                self.left,
                self.front,
                self.top,
                self.bottom,
                self.back,
                self.right,
            )

    def output_top(self):
        print(self.top)

    def get_top_front(self):
        return f"{self.top} {self.front}"

    def print_right(self):
        print(self.right)


# 10_A
# (*label,) = map(int, input().split())
# dice = Dice(label)
# for i in input():
#     dice.roll(i)
# dice.output_top()

# 10_B
(*label,) = map(int, input().split())
dice = Dice(label)
q = int(input())
for _ in range(q):
    t, f = map(int, input().split())
    for i in "EEEN" * 2 + "EEES" * 2 + "EEEN" * 2:
        if f"{t} {f}" == dice.get_top_front():
            dice.print_right()
            break
        dice.roll(i)