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:
import sys

DIRECTIONS = (
    (0, 0, 1),
    (0, -1, 0),
    (1, 0, 0),
    (-1, 0, 0),
    (0, 1, 0),
    (0, 0, -1)
)

DIRECTION_INDEX = {vec: i for i, vec in enumerate(DIRECTIONS)}


def cross(x, y):
    return (
        x[1] * y[2] - x[2] * y[1],
        x[2] * y[0] - x[0] * y[2],
        x[0] * y[1] - x[1] * y[0]
    )


values = [int(v) for v in sys.stdin.readline().strip().split()]
value_index = {value: i for i, value in enumerate(values)}

q = int(sys.stdin.readline())
for _ in range(q):
    top, front = (
        value_index[value]
        for value in map(int, sys.stdin.readline().strip().split())
    )
    index = DIRECTION_INDEX[cross(DIRECTIONS[top], DIRECTIONS[front])]
    print(values[index])

Submission status: Accepted

Solution code:
import random

def main():
    dir = ['W', 'N', 'S', 'E']
    dice = Dice([int(a) for a in input().split()])
    n = int(input())
    for _ in range(n):
        top, front = (int(x) for x in input().split())
        while True:
            if top == dice.getSuf(0) and front == dice.getSuf(1):
                print(dice.getSuf(2))
                break
            dice.move(random.choice(dir))


class Dice():
    def __init__(self, surface):
        self.l = surface
 
    def move(self, dir):
        if(dir == 'N'):
            self.l = [self.l[1], self.l[5], self.l[2], self.l[3], self.l[0], self.l[4]]
        elif(dir == 'W'):
            self.l = [self.l[2], self.l[1], self.l[5], self.l[0], self.l[4], self.l[3]]
        elif(dir == 'E'):
            self.l = [self.l[3], self.l[1], self.l[0], self.l[5], self.l[4], self.l[2]]
        elif(dir == 'S'):
            self.l = [self.l[4], self.l[0], self.l[2], self.l[3], self.l[5], self.l[1]]

    def show(self):
        print(self.l[0])
    
    def getSuf(self, surface):
        return self.l[surface]


main()

Submission status: Accepted

Solution code:
class Dice:
    __TOP = 0
    __FRONT = 1
    __RIGHT = 2
    __LEFT = 3
    __BACK = 4
    __BOTTOM = 5

    def __init__(self, face_val=None):
        self.f_keys = [self.__TOP,  # 0
                       self.__FRONT,  # 1
                       self.__RIGHT,  # 2
                       self.__LEFT,  # 3
                       self.__BACK,  # 4
                       self.__BOTTOM  # 5
                       ]

        if face_val is None or len(face_val) != 6:
            face_val = ['1', '2', '3', '4', '5', '6']

        self.f_key_to_val = {}
        self.f_val_to_key = {}
        for f_key, val in zip(self.f_keys, face_val):
            self.f_key_to_val[f_key] = val
            self.f_val_to_key[val] = f_key

    MOVE_SWAP_FACES = {
        'S': [(__BACK, __TOP), (__TOP, __FRONT), (__FRONT, __BOTTOM), (__BOTTOM, __BACK)],
        'W': [(__RIGHT, __TOP), (__TOP, __LEFT), (__LEFT, __BOTTOM), (__BOTTOM, __RIGHT)],
        'E': [(__LEFT, __TOP), (__TOP, __RIGHT), (__RIGHT, __BOTTOM), (__BOTTOM, __LEFT)],
        'N': [(__FRONT, __TOP), (__TOP, __BACK), (__BACK, __BOTTOM), (__BOTTOM, __FRONT)],
        'RCW' : [(__LEFT, __FRONT), (__FRONT, __RIGHT), (__RIGHT, __BACK), (__BACK, __LEFT)],   # rotate clockwise
        'RCCW': [(__RIGHT, __FRONT), (__FRONT, __LEFT), (__LEFT, __BACK), (__BACK, __RIGHT)],   # rotate counter clockwise
    }

    def dice_move(self, direction_list):
        for direction in direction_list:
            prev_faces = self.f_keys[:]
            for prev_f, next_f in self.MOVE_SWAP_FACES[direction]:
                self.f_keys[next_f] = prev_faces[prev_f]
            #print(self.f_keys)

    MOVE_TO_TOP = {__TOP:[], __FRONT:['N'], __RIGHT:['W'], __LEFT:['E'], __BACK:['S'], __BOTTOM:['N','N']}

    def dice_fix(self, top, front):
        if type(top) == str:
            top = self.f_val_to_key[top]
            front = self.f_val_to_key[front]

        # 上面を指定された面にする
        if top != self.f_keys[self.__TOP]:
            now_top = self.f_keys.index(top)
            move = self.MOVE_TO_TOP[now_top]
            self.dice_move(move)

        if top != self.f_keys[self.__TOP]:
            raise AssertionError # not top

        # 全面を指定された面にする
        if front != self.f_keys[self.__FRONT]:
            for i in range(3):
                self.dice_move(['RCW'])
                if front == self.f_keys[self.__FRONT]:
                    break
            else:
                raise AssertionError    # 3回回転するうちに指定された面がfrontに来ないとおかしい

    def get_value(self, face):
        return self.f_key_to_val[self.f_keys[face]]

    def __repr__(self):
        return f'({",".join(map(str, self.f_keys))})'


faces = input().split()
nq = int(input())

dice = Dice(faces)
for q in range(nq):
    top_val, front_val = input().split()
    dice.dice_fix(top=top_val, front=front_val)
    print(dice.get_value(Dice._Dice__RIGHT))

Submission status: Accepted

Solution code:
# Dice II
# 2018/11/17

class Dice:
    def __init__(self, *num):
        if len(num) != 6:
            raise ValueError("6!")
        self.numbers = num

    def east(self):
        index = (3, 1, 0, 5, 4, 2)
        self.numbers = tuple((self.numbers[i] for i in index))

    def west(self):
        index = (2, 1, 5, 0, 4, 3)
        self.numbers = tuple((self.numbers[i] for i in index))

    def south(self):
        index = (4, 0, 2, 3, 5, 1)
        self.numbers = tuple((self.numbers[i] for i in index))

    def north(self):
        index = (1, 5, 2, 3, 0, 4)
        self.numbers = tuple((self.numbers[i] for i in index))

    def rot_top(self):
        """topを変えないように時計周りにくるくる回す"""
        index = (0, 2, 4, 1, 3, 5)
        self.numbers = tuple((self.numbers[i] for i in index))

    @property
    def top(self):
        return self.numbers[0]

    @property
    def front(self):
        return self.numbers[1]

    @property
    def right(self):
        return self.numbers[2]

    def __repr__(self):
        return "Dice(" + ", ".join([str(i) for i in self.numbers]) + ")"


def main():
    num = [int(i) for i in input().rstrip().split()]
    dice = Dice(*num)
    n = int(input().rstrip())
    for _ in range(n):
        top, front = (int(i) for i in input().rstrip().split())

        # topを合わせる
        for i in range(4):
            if dice.top == top:
                break
            dice.east()

        for i in range(4):
            if dice.top == top:
                break
            dice.north()

        # frontを合わせる
        for i in range(4):
            if dice.front == front:
                break
            dice.rot_top()

        print(dice.right)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class dice:
    def __init__(self,label):
        self.top, self.front, self.right, self.left, self.rear, self.bottom \
                = label
  
    def roll(self,op):
        if   op=='N': self.top, self.front, self.bottom, self.rear = \
                                self.front, self.bottom, self.rear , self.top
        elif op=='E': self.top, self.left , self.bottom, self.right = \
                                self.left , self.bottom, self.right, self.top
        elif op=='W': self.top, self.right, self.bottom, self.left = \
                                self.right, self.bottom, self.left , self.top
        elif op=='S': self.top, self.rear , self.bottom, self.front = \
                                self.rear , self.bottom, self.front, self.top
  
    def print_top(self): print(self.top)
    def print_right(self): print(self.right)
  
    def get_label(self):
        return [self.top, self.front, self.right, self.left, self.rear, self.bottom]
label = list(map(int,input().split()))
d = dice(label)
q = int(input())
for _ in range(q):
    top_front = [int(x) for x in input().split()]
    for op in 'EEENEEENEEESEEESEEENEEEN':
        if d.get_label()[:2] == top_front:
            d.print_right()
            break
        d.roll(op)

Submission status: Accepted

Solution code:


class Dice:
    diceNum = []
    pos = 0
    def setNum(self,i):
        self.pos = i[0]
        self.diceNum.append(i[0])
        self.diceNum.append(i[1])
        self.diceNum.append(i[2])
        self.diceNum.append(i[3])
        self.diceNum.append(i[4])
        self.diceNum.append(i[5])
    def getTopPos(self):
        return self.diceNum[0]    
    def moveToTopnum(self,n):
        i = 0
        for i in range(0,4):
            if (self.getTopPos() == n ):
                return True
            self.move("S")
        else:
            for j in range(0,4):
                if (self.getTopPos() == n ):
                    return True
        return False
    def getFrontPos(self):
        return self.diceNum[1]
    def getRightPos(self):
        return self.diceNum[2]
    def yokoRotate(self):
            i0=self.diceNum[0]
            i1=self.diceNum[2] 
            i2=self.diceNum[4] 
            i3=self.diceNum[1]
            i4=self.diceNum[3]
            i5=self.diceNum[5]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
    def move(self,d):
        if (d == "E"):
            i0=self.diceNum[3]
            i1=self.diceNum[1] 
            i2=self.diceNum[0] 
            i3=self.diceNum[5]
            i4=self.diceNum[4]
            i5=self.diceNum[2]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "W"):
            i0=self.diceNum[2]
            i1=self.diceNum[1] 
            i2=self.diceNum[5] 
            i3=self.diceNum[0]
            i4=self.diceNum[4]
            i5=self.diceNum[3]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "S"):
            i0=self.diceNum[4]
            i1=self.diceNum[0] 
            i2=self.diceNum[2] 
            i3=self.diceNum[3]
            i4=self.diceNum[5]
            i5=self.diceNum[1]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
        if (d == "N"):
            i0=self.diceNum[1]
            i1=self.diceNum[5] 
            i2=self.diceNum[2] 
            i3=self.diceNum[3]
            i4=self.diceNum[0]
            i5=self.diceNum[4]
            self.diceNum =[i0,i1,i2,i3,i4,i5]
 
l = list(map(int,input().split()))
dice = Dice()
dice.setNum(l)

n = int(input())
while (n > 0):
    n = n -1
    (t,f) = map(int,input().split())
    dice.moveToTopnum(t)
    for i in range(0,3):
        if ( dice.getFrontPos() == f):
            break
        dice.yokoRotate()
    print (dice.getRightPos())

Submission status: Accepted

Solution code:
d=list(map(int,input().split()))
q=int(input())

def Check(Dice,P):
    if P == [3,1] or P ==[4,3] or P ==[2,4] or P ==[1,2]:
        a=0
    elif P == [2,0] or P ==[5,2] or P ==[3,5] or P ==[0,3]:
        a=1
    elif P == [0,1] or P ==[1,5] or P ==[5,4] or P ==[4,0]:
        a=2
    elif P == [1,0] or P ==[5,1] or P ==[4,5] or P ==[0,4]:
        a=3
    elif P == [0,2] or P ==[2,5] or P ==[5,3] or P ==[3,0]:
        a=4
    elif P == [1,3] or P ==[3,4] or P ==[4,2] or P ==[2,1]:
        a=5
    return a

for i in range(q):
    l1=list(map(int,input().split()))
    l2=[d.index(l1[0]),d.index(l1[1])]
    print(d[Check(d,l2)])

Submission status: Accepted

Solution code:
dice = list(map(int, input().split()))
q = int(input())

anslist = []

for _ in range(q):
    up, front = map(int, input().split())
    if up == dice[1]:
        slist = "N"
    elif up == dice[2]:
        slist = "W"
    elif up == dice[3]:
        slist = "E"
    elif up == dice[4]:
        slist = "S"
    elif up == dice[5]:
        slist = "NN"
    else:
        slist = ""

    for s in slist:
        if s == "N":
            next = list("263415")
        elif s == "E":
            next = list("421653")
        elif s == "S":
            next = list("513462")
        else:
            next = list("326154")

        tmp = [0] * 6
        for i, n in enumerate(next):
            tmp[i] = dice[int(n)-1]

        dice = tmp

    if front == dice[1]:
        ans = dice[2]
    elif front == dice[2]:
        ans = dice[4]
    elif front == dice[3]:
        ans = dice[1]
    elif front == dice[4]:
        ans = dice[3]

    anslist.append(ans)

for ans in anslist:
    print(ans)

Submission status: Accepted

Solution code:
d=list(map(int,input().split()))
for i in range(int(input())):
	o=list(map(int,input().split()))
	if o[0]==d[0]:
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[1]:
		d[0],d[1],d[4],d[5]=d[1],d[5],d[0],d[4]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[2]:
		d[0],d[2],d[3],d[5]=d[2],d[5],d[0],d[3]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
		
	elif o[0]==d[3]:
		d[0],d[2],d[3],d[5]=d[3],d[0],d[5],d[2]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
		
	elif o[0]==d[4]:
		d[0],d[1],d[4],d[5]=d[4],d[0],d[5],d[1]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])
			
	elif o[0]==d[5]:
		d[0],d[1],d[4],d[5]=d[5],d[4],d[1],d[0]
		if o[1]==d[1]:
			print(d[2])
		elif o[1]==d[2]:
			print(d[4])
		elif o[1]==d[4]:
			print(d[3])
		elif o[1]==d[3]:
			print(d[1])

Submission status: Accepted

Solution code:
def swap(dice,i,j,k,l):#diceの目をi→ｊ→ｋ→ｌ→iの順に入れ替える
    x=dice[l]
    dice[l]=dice[k]
    dice[k]=dice[j]
    dice[j]=dice[i]
    dice[i]=x
    return dice

def Sroll(dice):
    dice=swap(dice,0,1,5,4)
    return dice

def Eroll(dice):
    dice=swap(dice,0,2,5,3)
    return dice

def Wroll(dice):
    dice=swap(dice,0,3,5,2)
    return dice

def Nroll(dice):
    dice=swap(dice,0,4,5,1)
    return dice




dice=list(map(int,input().split()))
n=int(input())
for i in range(0,n):
    top,fro=map(int,input().split())
    for j in range(0,8):
        if(fro==dice[1]):
            break
        if(j==3):
            dice=Eroll(dice)
        dice=Sroll(dice)
    while(top!=dice[0]):
        dice=Eroll(dice)
    print(dice[2])

Submission status: Accepted

Solution code:
class dice:
    def __init__(self, d, t, s):
        self.d = d
        self.t = t
        self.s = s
    def top(self):
        from collections import OrderedDict
        p = OrderedDict()
        for k, v in zip(["T", "N", "S", "E", "W", "B"], [self.d[0], self.d[4], self.d[1], self.d[2], self.d[3], self.d[5]]):
            p[k] = v
        import random
        while True:
            c = random.choice(["N", "S", "E", "W"])
            t, n, s, e, w, b = p["T"], p["N"], p["S"], p["E"], p["W"], p["B"]
            
            if t == self.t and s == self.s:
                print(e)
                break
            if c == "N":
                
                p["N"], p["T"], p["S"], p["B"]= t, s, b, n
            if c == "S":
                p["N"], p["T"], p["S"], p["B"]= b, n, t, s
            if c == "E":
                p["E"], p["T"], p["W"], p["B"]= t, w, b, e
            if c == "W":
                p["E"], p["T"], p["W"], p["B"]= b, e, t, w
        
        

d = list(map(int, input().split()))        
for i in range(int(input())):
    t, s = map(int, input().split())
    dice1 = dice(d, t, s)
    dice1.top()

Submission status: Accepted

Solution code:
from enum import Enum
import sys
import math

# 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.pattern = 'NNNNENNNENNNWNNNWNNNENNN'
        
    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 match(self, top, front):
        for i in range(6):
            self.work[i] = self.number[i]
        
        ret = 0
        
        for i in range(len(self.pattern)):
            self.roll(self.pattern[i])
            if top == self.number[0] and front == self.number[1]:
                ret = self.number[2]
                break
                
    def get_Right(self):
        return self.number[2]        
        
dice = Dice()
table = list(map(int, input().split()))
dice.setNumber(table[0], table[1], table[2], table[3], table[4], table[5])

top_fro = []
q = int(input())
for i in range(q):
    top_fro.append(list(map(int, input().split())))

for i in range(q):
    dice.match(top_fro[i][0], top_fro[i][1])
    print("%d"%(dice.get_Right()))

Submission status: Accepted

Solution code:
class Dice :
    def __init__(self, pane):
        self.pane = pane
    
    def roll_W(self):
        temp = self.pane[0]
        self.pane[0] = self.pane[2]
        self.pane[2] = self.pane[5]
        self.pane[5] = self.pane[3]
        self.pane[3] = temp

    def roll_S(self):
        temp = self.pane[0]
        self.pane[0] = self.pane[4]
        self.pane[4] = self.pane[5]
        self.pane[5] = self.pane[1]
        self.pane[1] = temp
    
dice = Dice(input().split())
n = int(input())
result = []

for i in range(0, n):
    str_in = input().split()
    t = str_in[0]
    f = str_in[1]
    while True:
        if (dice.pane[0] == f or dice.pane[4] == f):
            dice.roll_S()
        if (dice.pane[0] == t and dice.pane[1] == f):
            break
        dice.roll_W()

    result.append(dice.pane[2])

for i in result:
    print(i)

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:
class Dice:
    def __init__(self, pip):
        self.pip = pip
    
    def move(self, dir):
        if str(dir) == 'E':
            self.pip[0], self.pip[2], self.pip[3], self.pip[5] = \
                self.pip[3], self.pip[0], self.pip[5], self.pip[2]
        elif str(dir) == 'W':
            self.pip[0], self.pip[2], self.pip[3], self.pip[5] = \
                self.pip[2], self.pip[5], self.pip[0], self.pip[3]
        elif str(dir) == 'N':
            self.pip[0], self.pip[1], self.pip[4], self.pip[5] = \
                self.pip[1], self.pip[5], self.pip[0], self.pip[4]
        elif str(dir) == 'S':
            self.pip[0], self.pip[1], self.pip[4], self.pip[5] = \
                self.pip[4], self.pip[0], self.pip[5], self.pip[1]

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

for _ in range(n):
    t, f = map(int, input().split())
    for j in "EEENEEENEEESEEESEEENEEEN":
        if d.pip[0] == t and d.pip[1] == f:
            break
        d.move(j)
    print(d.pip[2])

Submission status: Accepted

Solution code:
class Dice:
  def __init__(self, top=1, front=2, left=4, right=3, back=5, bottom=6):
    self.d = {"TOP":top, "FRONT":front, "LEFT":left, "RIGHT":right, "BACK":back, "BOTTOM":bottom}

  def roll_x(self):
    self._roll("TOP", "FRONT", "BOTTOM", "BACK")
  
  def roll_y(self):
    self._roll("FRONT", "LEFT", "BACK", "RIGHT")
  
  def roll_z(self):
    self._roll("TOP", "LEFT", "BOTTOM", "RIGHT")
  
  def _roll(self, a, b, c, d):
    self.d[a], self.d[b], self.d[c], self.d[d] = \
    self.d[b], self.d[c], self.d[d], self.d[a]

  def all_rolls(self):
      ret = []
      for i in range(6):
        if i%2:
          self.roll_x()
        else:
          self.roll_y()
        for j in range(4):
          self.roll_z()
          t = self.d.copy()
          ret.append(t)
      return ret

d = list(map(int, input().split()))
dd = Dice(d[0], d[1], d[3], d[2], d[4], d[5])
l = dd.all_rolls()
q = int(input())
for i in range(q):
  t, f = list(map(int, input().split()))
  for ele in l:
    if ele["TOP"]==t and ele["FRONT"]==f:
      print(ele["RIGHT"])
      break

Submission status: Accepted

Solution code:
class Dice:
	# Label: 1=Up, 2=South, 3=East, 4=West, 5=North, 6=Down
	def __init__(self, nums):
		self.cU = nums[0]
		self.cS = nums[1]
		self.cE = nums[2]
		self.cW = nums[3]
		self.cN = nums[4]
		self.cD = nums[5]
	def goN(self):
		pU,pS,pN,pD = self.cU,self.cS,self.cN,self.cD
		self.cU,self.cS,self.cN,self.cD = pS,pD,pU,pN
	def goE(self):
		pU,pE,pW,pD = self.cU,self.cE,self.cW,self.cD
		self.cU,self.cE,self.cW,self.cD = pW,pU,pD,pE
	def goW(self):
		pU,pE,pW,pD = self.cU,self.cE,self.cW,self.cD
		self.cU,self.cE,self.cW,self.cD = pE,pD,pU,pW
	def goS(self):
		pU,pS,pN,pD = self.cU,self.cS,self.cN,self.cD
		self.cU,self.cS,self.cN,self.cD = pN,pU,pD,pS
	def toU(self, num):
		if self.cD == num:
			for _ in range(2): self.goN()
		elif self.cN == num: self.goS()
		elif self.cE == num: self.goW()
		elif self.cW == num: self.goE()
		elif self.cS == num: self.goN()
	def right(self, num):
		if self.cN == num: return self.cW
		elif self.cE == num: return self.cN
		elif self.cW == num: return self.cS
		elif self.cS == num: return self.cE
		else: raise # ????????????
nums = [int(i) for i in input().split(' ')]
di = Dice(nums)
q = int(input())
for _ in range(q):
	u,f = map(int, tuple(input().split(' ')))
	di.toU(u)
	print(di.right(f))

Submission status: Runtime Error

Solution code:
class Dice(object):
 
    def __init__(self, d):

        self.rows = [d[0], d[4], d[5], d[1]]
        self.cols = [d[0], d[2], d[5], d[3]]
 
    def __move_next(self, x, y):
        temp = y.pop(0)
        y.append(temp)
        x[0] = y[0]
        x[2] = y[2]
 
    def __move_prev(self, x, y):
        temp = y.pop(3)
        y.insert(0, temp)
        x[0] = y[0]
        x[2] = y[2]
 
    def execute(self, c):
        for i in self.c:
            self.__move(i, self.rows, self.cols)
 
    def __move(self, com, x, y):
        if com == "N":
            self.__move_prev(y, x)
        elif com == "S":
            self.__move_next(y, x)
        elif com == "E":
            self.__move_prev(x, y)
        elif com == "W":
            self.__move_next(x, y)
 
    def print_top(self):
        print(self.rows[0])
    
    def execute2(self, a, b):

        while self.rows[0] == b:
           self.__move_next(self.rows, self.cols)
        
        while self.rows[0] == a and self.rows[1] == b:
            self.__move_next(self.cols, self.rows)
    
    def print_right_side(self):
        print(self.cols[4])
        

d = map(int, input().split(" "))
c = input() 
dice = Dice2(d)

for i in range(c):
    a, b = map(int, input().split(" "))
    dice.execute2(a,b)
    dice.print_right_side()

Submission status: Runtime Error

Solution code:
from typing import List


class Dice:

    sur = [i for i in range(6)]

    def __init__(self, s: List[int]):
        self.s = s  # surfaceリスト

        # indexが面の番号。リストの値は、それに接する面の番号。
        self.sur[0] = [1, 2, 4, 3]
        self.sur[1] = [0, 3, 5, 2]
        self.sur[2] = [1, 5, 4, 0]
        self.sur[3] = [1, 0, 4, 5]
        self.sur[4] = [0, 2, 5, 3]
        self.sur[5] = [1, 3, 4, 2]

    def get_right_face_num(self, upper_num: int, front_num: int) -> int:
        for i, value in enumerate(self.sur[upper_num]):
            if value == front_num:
                return self.sur[upper_num][(i + 1) % 4]
        else:
            raise ValueError('get_right_faceの来てはいけない箇所')

    def get_value(self, face_num) -> int:
        return self.s[face_num]

    def get_face_num(self, value) -> int:
        for i in range(len(self.s)):
            if value == self.s[i]:
                return i
        else:
            raise ValueError('get_sur_indexの来てはいけない箇所')


# 提出用
data = [int(i) for i in input().split()]
N = int(input())
dice = Dice(data)
for _i in range(N):
    upper_value, front_value = map(int, input().split())
    upper_num = dice.get_face_num(upper_value)
    front_num = dice.get_face_num(front_value)
    ans_num = dice.get_right_face_num(upper_num, front_num)
    print(dice.get_value(ans_num))

# 動作確認用
with open('input_itp1_11_a_1.txt', mode='r', encoding='utf8') as fin:
    data = [int(i) for i in next(fin).split()]
    N = int(next(fin))
    dice = Dice(data)
    for _i in range(N):
        upper_value, front_value = map(int, next(fin).split())
        upper_num = dice.get_face_num(upper_value)
        front_num = dice.get_face_num(front_value)
        ans_num = dice.get_right_face_num(upper_num, front_num)
        print(dice.get_value(ans_num))

Submission status: Runtime Error

Solution code:
import random


class Cube:
    def __init__(self, u, s, e, w, n, d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d

    def rotate(self, dic):
        if dic == "N":
            tmp = self.u
            self.u = self.s
            self.s = self.d
            self.d = self.n
            self.n = tmp
        elif dic == "E":
            tmp = self.u
            self.u = self.w
            self.w = self.d
            self.d = self.e
            self.e = tmp
        elif dic == "W":
            tmp = self.u
            self.u = self.e
            self.e = self.d
            self.d = self.w
            self.w = tmp
        else:
            tmp = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = tmp


def main():
    u, s, e, w, n, d = map(int, input().split())
    cube = Cube(u, s, e, w, n, d)
    q = int(input())
    for i in range(q):
        upper, front = map(int, input().split())
        while True:
            cube.rotate(random.choice("NWES"))
            if upper == cube.u and front == cube.s:
                print(cube.e)
                break


if __name__ == '__main__':
    main(

Submission status: Runtime Error

Solution code:
#include<iostream>
using namespace std;

int main(){
    int n, a, b;
    int c[6]={};
    for (int i=0; i<6; i++) {
        cin >> c[i];
    }
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> a >> b;
        if (a==c[0]) {
            if (b==c[1]) {
                cout << c[2] << endl;
            } else if (b==c[2]) {
                cout << c[4] << endl;
            } else if (b==c[3]) {
                cout << c[1] << endl;
            } else if (b==c[4]) {
                cout << c[3] << endl;
            }
        } else if (a==c[1]) {
            if (b==c[0]) {
                cout << c[3] << endl;
            } else if (b==c[2]) {
                cout << c[0] << endl;
            } else if (b==c[3]) {
                cout << c[5] << endl;
            } else if (b==c[5]) {
                cout << c[2] << endl;
            }
        } else if (a==c[2]) {
            if (b==c[0]) {
                cout << c[1] << endl;
            } else if (b==c[1]) {
                cout << c[5] << endl;
            } else if (b==c[4]) {
                cout << c[0] << endl;
            } else if (b==c[5]) {
                cout << c[4] << endl;
            }
        } else if (a==c[3]) {
            if (b==c[0]) {
                cout << c[4] << endl;
            } else if (b==c[1]) {
                cout << c[0] << endl;
            } else if (b==c[4]) {
                cout << c[5] << endl;
            } else if (b==c[5]) {
                cout << c[1] << endl;
            }
        } else if (a==c[4]) {
            if (b==c[0]) {
                cout << c[2] << endl;
            } else if (b==c[2]) {
                cout << c[5] << endl;
            } else if (b==c[3]) {
                cout << c[0] << endl;
            } else if (b==c[5]) {
                cout << c[3] << endl;
            }
        } else if (a==c[5]) {
            if (b==c[1]) {
                cout << c[3] << endl;
            } else if (b==c[2]) {
                cout << c[1] << endl;
            } else if (b==c[3]) {
                cout << c[4] << endl;
            } else if (b==c[4]) {
                cout << c[2] << endl;
            }
        } 
    }
    return 0;
}

Submission status: Runtime Error

Solution code:
mask = [range(6), (1, 5, 2, 3, 0, 4), (2, 1, 5, 0, 4,3),(3, 1, 0, 5, 4, 2), (4, 0, 2, 3, 5, 1)]
mask += [mask[1][i] for i in mask[1]]

TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM = mask[0]

def set_top(dice, top):
    return [dice[i] for i in mask[top]]

def set_front(dice, front):
    twist = (0, 3, 1, 4, 2, 5)
    while dice[FRONT] != front:
        dice = [dice[i] for i in twist]
    return dice
        
label = input().split()
q = int(input())
for _ in range(q):
    top, front = map(label.index, input().split())
    dice = set_top(mask[0], top)
    dice = set_front(dice, front)
    print(label[dice[RIGHT]])