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.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'
 
    def set(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 kaiten(self,loc):
        for i in range(6):
            self.work[i] = self.number[i]
 
        if loc == 'E':
            self.set(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
        elif loc == 'N':
            self.set(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
        elif loc == 'S':
            self.set(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
        elif loc == 'W':
            self.set(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])
 
 
    def tansaku(self,top_num,front_num):
 
        self.save = [i for i in range(6)]
        for i in range(len(self.number)):
            self.save[i] = self.number[i]
 
        ret = -1;
 
        for i in range(len(self.order)):
            self.kaiten(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[i]
 
        return ret;
 
 
 
 
dice = Dice()
table = list(map(int,input().split()))
dice.set(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.tansaku(top_num, front_num)))

Submission status: Accepted

Solution code:
class Dice():

    patterns = {"N":[1,5,2,3,0,4],"S":[4,0,2,3,5,1],"W":[2,1,5,0,4,3],"E":[3,1,0,5,4,2]}

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

    #0->上、1->正面、2->右面、3->左面、4->裏面、5->下面
    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,command,int):
        for i in command:
            self.number = [self.number[j] for j in self.patterns[i]]
        return self.number[int]


dice = Dice()
nums = list(map(int,input().split()))
dice.setNumber(nums[0],nums[1],nums[2],nums[3],nums[4],nums[5]) #サイコロの初期値
q_num = int(input()) #質問の回数

command1 = ["S","S","S","S","E","E","E"] #topを一致させるための回転
command2 = ["S","E","N","S","E","N","S","E","N","S","E","N"] #frontを一致させるための回転


for i in range(q_num): #質問の数だけ回答を繰り返す
    right = nums[2]
    top,front = map(int,input().split())
    for j in command1:
        if top == dice.roll(j,0):
            for k in command2:
                if front == dice.roll(k,1):
                    right = dice.number[2]
    print(right)

Submission status: Accepted

Solution code:
a = list(map(int, input().split()))
s = ['402351', '152304', '310542', '215043']   
for _ in range(int(input())):
    b, c = map(int, input().split())
    t = []
    t.append(a)
    i = 0; j = 0
    while True:
        if t[i][0] == b and t[i][1] == c:
            print(t[i][2])
            break
        if i == len(t)-1: # add if it finishes searching all path
            for h in range(len(s)):
                t.append([ t[j][int(k)] for k in list(s[h]) ])
            j+=1
        i+=1

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, p):
        self.y = [p[0], p[1], p[5], p[4]]
        self.x = [p[0], p[2], p[5], p[3]]
        self.z = [p[1], p[2], p[4], p[3]]
    def roll(self, c):
        if c == 'y':
            self.y.append(self.y.pop(0))
            self.x[0] = self.y[0]
            self.x[2] = self.y[2]
            self.z[0] = self.y[1]
            self.z[2] = self.y[3]
        elif c == 'x':
            self.x.append(self.x.pop(0))
            self.y[0] = self.x[0]
            self.y[2] = self.x[2]
            self.z[1] = self.x[1]
            self.z[3] = self.x[3]
        elif c == 'z':
            self.z.append(self.z.pop(0))
            self.x[1] = self.z[1]
            self.x[3] = self.z[3]
            self.y[1] = self.z[0]
            self.y[3] = self.z[2]
    def num(self, n):
        n = n - 1;
        pos_y = [0, 1, 5, 4]
        pos_x = [0, 2, 5, 3]
        if n in pos_y:
            return self.y[pos_y.index(n)]
        else:
            return self.x[pos_x.index(n)]

p = [ int(n) for n in input().split()]

q = int(input())
qq = []
for i in range(0, q):
    qq.append([int(n) for n in input().split()])


for (a, b) in qq:
    
    dice = Dice(p)

    
    for _ in range(0, 4):
        if dice.num(1) == a:
            break;
        dice.roll('x')
    for _ in range(0, 4):
        if dice.num(1) == a:
            break;
        dice.roll('y')
   
    for _ in range(0, 4):
        if dice.num(2) == b:
            break;
        dice.roll('z')
    print(dice.num(3))

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self, ls = [0 for i in range(6)]):
        self.ls = ls
        
    def now(self):
        return ls
    
    def S(self):
        ls = self.ls
        n_ls = [0 for i in range(6)]
        n_ls[0] = ls[4]
        n_ls[1] = ls[0]
        n_ls[2] = ls[2]
        n_ls[3] = ls[3]
        n_ls[4] = ls[5]
        n_ls[5] = ls[1]
        
        return Dice(n_ls)
    
    def N(self):
        ls = self.S().S().S().ls
        return Dice(ls)
        
    def E(self):
        ls = self.ls
        n_ls = [0 for i in range(6)]
        n_ls[0] = ls[3]
        n_ls[1] = ls[1]
        n_ls[2] = ls[0]
        n_ls[3] = ls[5]
        n_ls[4] = ls[4]
        n_ls[5] = ls[2]
        return Dice(n_ls)
    
    
    def W(self):
        ls = self.E().E().E().ls
        return Dice(ls)
        
        
        
    def R_rot(self):
        return self.E().S().W()
        
    def rot(self, num):
        for _ in range(num):
            self = self.R_rot()
        return self
    
ls = list(map(int, input().split()))
q = int(input())

a = Dice(ls)
#print(a.ls)
me = [a, a.N(), a.W(), a.E(), a.S(), a.S().S()]
#print([i.ls for i in me])

for _ in range(q):
    n, m = map(int, input().split())



    B= Dice()
    for i in me:
        if i.ls[0] == n:
            B = i
#print(B.ls)
    Rot = [B.rot(i) for i in range(4)]
    for i in Rot:
        if i.ls[1] == m:
            print(i.ls[2])

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
q = int(input())
l = [[3,5,2,4],[4,1,6,3],[2,6,1,5],[5,1,6,2],[3,6,1,4],[4,2,5,3]]
for i in range(q):
    a, b = map(int,input().split())
    for j in range(6):
        if a == n[j]:
            c = j
        if b == n[j]:
            d = j
    e = d
    if c < d:
        e -= 1
    if c + d > 5:
        e -= 1
    print(n[l[c][e]-1])

Submission status: Accepted

Solution code:
import random
class Dice:
    def __init__(self,surfaceNum):
        self.surface = surfaceNum
        
    def east(self):
        self.surface[0], self.surface[2], self.surface[5], self.surface[3] = self.surface[3], self.surface[0], self.surface[2], self.surface[5]
    def west(self):
        self.surface[0], self.surface[2], self.surface[5], self.surface[3] = self.surface[2], self.surface[5], self.surface[3], self.surface[0]
    def south(self):
        self.surface[0], self.surface[1], self.surface[5], self.surface[4] = self.surface[4], self.surface[0], self.surface[1], self.surface[5]
    def north(self):
        self.surface[0], self.surface[1], self.surface[5], self.surface[4] = self.surface[1], self.surface[5], self.surface[4], self.surface[0]
        
    def printTopSurface(self):
        print(self.surface[0])  
    def printRight(self):
        print(self.surface[2])

surfaceNum = [int(i) for i in input().split()]
commandNum = int(input())
rollCommandList = ["E", "W", "S", "N"]
Dice = Dice(surfaceNum)
rightNum = [0]*commandNum

for i in range(commandNum):
    topFront = [int(data) for data in input().split()]
    while Dice.surface[0] != topFront[0] or Dice.surface[1] != topFront[1]:
        rollCommand = random.choice(rollCommandList)
        if rollCommand == "E":
            Dice.east()
        if rollCommand == "W":
            Dice.west()
        if rollCommand == "S":
            Dice.south()
        if rollCommand == "N":
            Dice.north()
    rightNum[i] = Dice.surface[2]
for i in range(commandNum):
    print(rightNum[i])

Submission status: Accepted

Solution code:
dice = [int(_) for _ in input().split()]
num = int(input())
dire = {"N": [2,6,3,4,1,5],"S":[5,1,3,4,6,2],
      "E":[4,2,1,6,5,3],"W":[3,2,6,1,5,4]}
z_dire = [1,4,2,5,3,6]

for i in range(num):
    dis = [int(_) for _ in input().split()]
    count = 0
    while dice[0] != dis[0]:
        em = dice[:]
        for j in range(len(dice)):
            if count % 2 == 0:
                em[j] = dice[dire["N"][j]-1]
            else:
                em[j] = dice[dire["E"][j]-1]
        else:
            del dice[:]
            dice = em[:]
        count += 1

    while dice[1] != dis[1]:
        em = dice[:]
        for j in range(len(dice)):
            em[j] = dice[z_dire[j]-1]
        else:
            del dice[:]
            dice = em[:]
    print(dice[2])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, p):
        self.y = [p[0], p[1], p[5], p[4]]
        self.x = [p[0], p[2], p[5], p[3]]
        self.z = [p[1], p[2], p[4], p[3]]
    def roll(self, c):
        if c == 'y':
            self.y.append(self.y.pop(0))
            self.x[0] = self.y[0]
            self.x[2] = self.y[2]
            self.z[0] = self.y[1]
            self.z[2] = self.y[3]
        elif c == 'x':
            self.x.append(self.x.pop(0))
            self.y[0] = self.x[0]
            self.y[2] = self.x[2]
            self.z[1] = self.x[1]
            self.z[3] = self.x[3]
        elif c == 'z':
            self.z.append(self.z.pop(0))
            self.x[1] = self.z[1]
            self.x[3] = self.z[3]
            self.y[1] = self.z[0]
            self.y[3] = self.z[2]
    def num(self, n):
        n = n - 1;
        pos_y = [0, 1, 5, 4]
        pos_x = [0, 2, 5, 3]
        if n in pos_y:
            return self.y[pos_y.index(n)]
        else:
            return self.x[pos_x.index(n)]

p = [ int(n) for n in input().split()]
q = int(input())
qq = []
for i in range(0, q):
    qq.append([int(n) for n in input().split()])

for (a, b) in qq:
    dice = Dice(p)
    for _ in range(0, 4):
        if dice.num(1) == a:
            break;
        dice.roll('x')
    for _ in range(0, 4):
        if dice.num(1) == a:
            break;
        dice.roll('y')
    for _ in range(0, 4):
        if dice.num(2) == b:
            break;
        dice.roll('z')
    print(dice.num(3))

Submission status: Accepted

Solution code:
num = list(map(int, input().split()))
q = int(input())
anslist = []

for i in range(q):
    top, front = map(int, input().split())
    if top == num[0]:
        order = ''
    elif top == num[1]:
        order = 'N'
    elif top == num[2]:
        order = 'W'
    elif top == num[3]:
        order = 'E'
    elif top == num[4]:
        order = 'S'
    elif top == num[5]:
        order = 'NN'


    for o in order:
        if o == 'N':
            next = '263415'
        elif o == 'W':
            next = '326154'
        elif o == 'S':
            next = '513462'
        else:
            next = '421653'

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

        num = temp

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

    anslist.append(ans)

for ans in anslist:
    print(ans)

Submission status: Accepted

Solution code:
dice_ = [int(i) for i in input().split()]
dice = {
    (0,0,1):dice_[0],
    (0,-1,0):dice_[1],
    (1,0,0):dice_[2],
    (-1,0,0):dice_[3],
    (0,1,0):dice_[4],
    (0,0,-1):dice_[5]
}
rdice = {
    dice_[0]:(0,0,1),
    dice_[1]:(0,-1,0),
    dice_[2]:(1,0,0),
    dice_[3]:(-1,0,0),
    dice_[4]:(0,1,0),
    dice_[5]:(0,0,-1)
}

def cross(a,b):
	return(a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0])
n = int(input())
for _ in range(n):
	a_,b_ = [int(i) for i in input().split()]
	a = rdice[a_]
	b = rdice[b_]
	print(dice[cross(a,b)])

Submission status: Accepted

Solution code:
# coding: utf-8
class Dice:
    
    def __init__(self):
        self.data = {"bottom":5, "west": 3, "north" : 4,
                "east": 2, "south" : 1,"top" : 0}
    
    def moveOne(self, direction):
        if direction == "W":
            temp = self.data["bottom"]
            self.data["bottom"] = self.data["west"]
            self.data["west"] = self.data["top"]
            self.data["top"] = self.data["east"]
            self.data["east"] = temp
        elif direction == "E":
            temp = self.data["bottom"]
            self.data["bottom"] = self.data["east"]
            self.data["east"] = self.data["top"]
            self.data["top"] = self.data["west"]
            self.data["west"] = temp
        elif direction == "N":
            temp = self.data["bottom"]
            self.data["bottom"] = self.data["north"]
            self.data["north"] = self.data["top"]
            self.data["top"] = self.data["south"]
            self.data["south"] = temp
        elif direction == "S":
            temp = self.data["bottom"]
            self.data["bottom"] = self.data["south"]
            self.data["south"] = self.data["top"]
            self.data["top"] = self.data["north"]
            self.data["north"] = temp

    def movePart(self, directions):
        for direction in directions:
            self.moveOne(direction)
            
    def setDiceII(self, a, b):
        if self.data["east"] == a:
            self.moveOne("W")
        elif self.data["north"] == a:
            self.moveOne("S")
        elif self.data["west"] == a:
            self.moveOne("E")
        elif self.data["south"] == a:
            self.moveOne("N")
        elif self.data["bottom"] == a:
            self.movePart("WW")

        if self.data["north"] == b:
            self.rotateRight()
            self.rotateRight()
        elif self.data["east"] == b:
            self.rotateRight()
        elif self.data["west"] == b:
            self.rotateLeft()
            
        # self.printDice()
    
    def rotateRight(self):
        temp = self.data["east"]
        self.data["east"] = self.data["north"]
        self.data["north"] = self.data["west"]
        self.data["west"] = self.data["south"]
        self.data["south"] = temp
    
    def rotateLeft(self):
        temp = self.data["west"]
        self.data["west"] = self.data["north"]
        self.data["north"] = self.data["east"]
        self.data["east"] = self.data["south"]
        self.data["south"] = temp
      
    def getSurface(self):
        return self.data["top"]
    
    def getEast(self):
        return self.data["east"]
        
    def printDice(self):
        print(self.data["north"])
        print(str(self.data["west"]) + " " + str(self.data["bottom"]) + " " + str(self.data["east"]) )
        print(self.data["south"])
        print(self.data["top"])


num = list(map(int,input().rstrip().split()))
n = int(input().rstrip())
dice = Dice()
for i in range(n):
    top, south = map(int,input().rstrip().split())
    dice.setDiceII(num.index(top),num.index(south))
    print(num[dice.getEast()])

Submission status: Accepted

Solution code:
nums = list(map(int, input().split()))
cases = int(input())


class die:
    face = [0, 0, 0, 0, 0, 0]

    def __init__(self, nums):
        self.face = [nums[f] for f in [0, 3, 1, 2, 4, 5]]

    def roll(self, dirs):

        for di in dirs:
            c_face = list(self.face)
            if di == 'S':
                self.face = [c_face[f] for f in [4, 1, 0, 3, 5, 2]]
            elif di == 'N':
                self.face = [c_face[f] for f in [2, 1, 5, 3, 0, 4]]
            elif di == 'W':
                self.face = [c_face[f] for f in [3, 0, 2, 5, 4, 1]]
            elif di == 'E':
                self.face = [c_face[f] for f in [1, 5, 2, 0, 4, 3]]
            # print(di, self.face)

        return list(self.face)


def four_roll(d, t, f):
    for r in 'SSSS':
        d.roll(r)
        if d.face[0] == t and d.face[2] == f:
            print(d.face[3])
            return True


for _ in range(cases):
    t, f = map(int, input().split())
    for md in (' ', 'W', 'E', 'NW', 'NE', 'WW'):
        d = die(nums)
        d.roll(md)
        four_roll(d, t, f)

Submission status: Accepted

Solution code:
import math

class Dice:
    def __init__(self, top, south, east, west, north, bottom):
        self.top = top
        self.south = south
        self.east = east
        self.west = west
        self.north = north
        self.bottom = bottom

    def roll_east(self):
        tmp = self.top
        self.top = self.west
        self.west = self.bottom
        self.bottom = self.east
        self.east = tmp

    def roll_north(self):
        tmp = self.top
        self.top = self.south
        self.south = self.bottom
        self.bottom = self.north
        self.north = tmp

    def roll_south(self):
        tmp = self.top
        self.top = self.north
        self.north = self.bottom
        self.bottom = self.south
        self.south = tmp

    def roll_west(self):
        tmp = self.top
        self.top = self.east
        self.east = self.bottom
        self.bottom = self.west
        self.west = tmp

    def spin_left(self):
        tmp = self.north
        self.north = self.east
        self.east = self.south
        self.south = self.west
        self.west = tmp

    def check(self, south):
        for x in range(4):
            if south == self.south:
                print(self.east)
                return
            self.spin_left()

    def top_check(self, top, south):
        if top == self.top:
            self.check(south)
        elif top == self.south:
            self.roll_north()
            self.check(south)
        elif top == self.east:
            self.roll_west()
            self.check(south)
        elif top == self.west:
            self.roll_east()
            self.check(south)
        elif top == self.north:
            self.roll_south()
            self.check(south)
        elif top == self.bottom:
            self.roll_south()
            self.roll_south()
            self.check(south)


a, b, c, d, e, f = map(int, input().split())
dice = Dice(a, b, c, d, e, f)
num = int(input())
for x in range(num):
    top, south = map(int, input().split())
    dice.top_check(top, south)

Submission status: Accepted

Solution code:
v = list(map(int, input().split()))
v.insert(0,-1)
n = int(input())
case = [[],
        [4,2,3,5], #1
        [6,3,1,4], #2
        [6,5,1,2], #3
        [6,2,1,5], #4
        [6,4,1,3], #5
        [5,3,2,4]] #6

for _ in range(n):
    topval, frontval = map(int, input().split())
    topkey, frontkey = v.index(topval), v.index(frontval)
    for i in range(4):
        if case[topkey][i] == frontkey:
            if i != 3:
                rightkey = case[topkey][i+1]
            else:
                rightkey = case[topkey][0]
    print(v[rightkey])

Submission status: Accepted

Solution code:
class Dice:
        def __init__(self, strList):
                self.label = strList[:]
                self.label[0] = int(self.label[0])
                self.label[1] = int(self.label[1])
                self.label[2] = int(self.label[2])
                self.label[3] = int(self.label[3])
                self.label[4] = int(self.label[4])
                self.label[5] = int(self.label[5])
        def reset(self, strList):
                self.label = strList[:]
                self.label[0] = int(self.label[0])
                self.label[1] = int(self.label[1])
                self.label[2] = int(self.label[2])
                self.label[3] = int(self.label[3])
                self.label[4] = int(self.label[4])
                self.label[5] = int(self.label[5])
        def rollE(self):
                temp = self.label[3]
                self.label[3] = self.label[5]
                self.label[5] = self.label[2]
                self.label[2] = self.label[0]
                self.label[0] = temp
        def rollN(self):
                temp = self.label[0]
                self.label[0] = self.label[1]
                self.label[1] = self.label[5]
                self.label[5] = self.label[4]
                self.label[4] = temp
        def rollA(self):
                temp = self.label[3]
                self.label[3] = self.label[4]
                self.label[4] = self.label[2]
                self.label[2] = self.label[1]
                self.label[1] = temp
        def printDice(self):
                print(self.label)
        def check(self, x, y):
                if x == self.label[0] and y == self.label[1]:
                        return True
                else:
                        return False

strDice = input()
diceList = strDice.split()
dice = Dice(diceList)

num = int(input())

for i in range(num):
        list = input().split()
        a = int(list[0])
        b = int(list[1])
        dice.reset(diceList)
        for j in range(6):
                for k in range(4):
                        dice.rollA()
                        if dice.check(a, b):
                                print(dice.label[2])
                if j % 2 == 0:
                        dice.rollE()
                else:
                        dice.rollN()

Submission status: Accepted

Solution code:
class dice:
    def __init__(self, first, second, third, fourth, fifth, sixth):
        self.up = first
        self.down = sixth
        self.S = second
        self.E = third
        self.N = fifth
        self.W = fourth
    def spinleft(self):
        temp = self.S
        self.S = self.W
        self.W = self.N
        self.N = self.E
        self.E = temp
    def movetoaim(self, u, S):
        # if upside != u, move dice to make upside become u
        if u == self.down:
            self = self.move("E")
            self = self.move("E")
        elif u != self.up:
            if u == self.S:
                self = self.move("N")
            elif u == self.E:
                self = self.move("W")
            elif u == self.N:
                self = self.move("S")
            else:
                self = self.move("E")
        # if South != S, spin to match them
        if S == self.W:
            self.spinleft()
        elif S == self.N:
            self.spinleft()
            self.spinleft()
        elif S == self.E:
            for i in range(3):
                self.spinleft()
        return self
        
    def move(self, direction):
        if direction == "S":
            temp = self.up
            self.up = self.N
            self.N = self.down
            self.down = self.S
            self.S = temp 
            return self
        elif direction == "E":
            temp = self.up
            self.up = self.W
            self.W = self.down
            self.down = self.E
            self.E = temp
            return self
        elif direction == "N":
            temp = self.up
            self.up = self.S
            self.S = self.down
            self.down = self.N
            self.N = temp
            return self
        else:
            temp = self.up
            self.up = self.E
            self.E = self.down
            self.down = self.W
            self.W = temp
            return self

nums = list(map(int,input().split()))
dice1 = dice(nums[0], nums[1], nums[2], nums[3], nums[4], nums[5])
n = int(input())
for i in range(n):
    aim = list(map(int,input().split()))
    dice1 = dice1.movetoaim(aim[0], aim[1])
    print(str(dice1.E))

Submission status: Accepted

Solution code:
import random

def rotateDice(direction: str, dice: dict) -> dict:
    if direction == 'S':
        dice['S'], dice['F'], dice['N'], dice['B'] = dice['F'], dice['N'], dice['B'], dice['S']
    elif direction == 'E':
        dice['E'], dice['F'], dice['W'], dice['B'] = dice['F'], dice['W'], dice['B'], dice['E']
    elif direction == 'W':
        dice['W'], dice['F'], dice['E'], dice['B'] = dice['F'], dice['E'], dice['B'], dice['W']
    elif direction == 'N':
        dice['N'], dice['F'], dice['S'], dice['B'] = dice['F'], dice['S'], dice['B'], dice['N']
    return dice


dice_list = list(map(int, input().split()))
dice = {'FSEWNB'[i]: dice_list[i] for i in range(6)}
for i in range(int(input())):
    f, s = map(int, input().split())
    while True:
        dice = rotateDice(random.choice(['S', 'E', 'W', 'N']), dice)
        if dice['F'] == f and dice['S'] == s:
            print(dice['E'])
            break

Submission status: Accepted

Solution code:

dice = input().split()
q = int(input())
for x in range(q):
    n, m = input().split()
    # 上面を合わせる
    d1 = {dice[0]: "123456", dice[1]: "263415", dice[2]: "326154", dice[3]: "421653", dice[4]: "513462",
          dice[5]: "653421"}
    dice = [dice[int(i) - 1] for i in d1[n]]
    # 側面を合わせる
    d2 = {dice[1]: "123456", dice[2]: "135246", dice[3]: "142536", dice[4]: "154326"}
    dice = [dice[int(i) - 1] for i in d2[m]]
    print(dice[2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, faces):
        self.top = faces[0]
        self.front = faces[1]
        self.right = faces[2]
        self.left = faces[3]
        self.back = faces[4]
        self.bottom = faces[5]

    def rotate(self, command):
        pre_top = self.top
        if command == "N":
            self.top = self.front
            self.front = self.bottom
            self.bottom = self.back
            self.back = pre_top
        elif command == "E":
            self.top = self.left
            self.left = self.bottom
            self.bottom = self.right
            self.right = pre_top
        elif command == "W":
            self.top = self.right
            self.right = self.bottom
            self.bottom = self.left
            self.left = pre_top
        elif command == "S":
            self.top = self.back
            self.back = self.bottom
            self.bottom = self.front
            self.front = pre_top
        elif command == "R":
            pre_front = self.front
            self.front = self.left
            self.left = self.back
            self.back = self.right
            self.right = pre_front


def main() -> None:
    dice_values = list(map(int, input().split()))
    dice_x = Dice(dice_values)

    q = int(input())
    for i in range(q):
        top, front = map(int, input().split())
        for j in range(6):
            if top == dice_x.top:
                break
            dice_x.rotate("N")
        for j in range(6):
            if top == dice_x.top:
                break
            dice_x.rotate("E")
        while front != dice_x.front:
            dice_x.rotate("R")
        print(dice_x.right)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
dice = input().split()
div1 = [[0, 1, 5, 4], [0, 3, 5, 2], [1, 3, 4, 2]]
div2 = [[2, 3], [1, 4], [5, 0]]
for i in range(int(input())):
    x = 0
    a, b = [dice.index(j) for j in input().split()]
    for j, k in zip(div1, div2):
        if a in j and b in j:
            if j.index(a) < j.index(b):
                x = k[0]
            else:
                x = k[1]
            if abs(j.index(a) - j.index(b)) == 3:
                x = 5 - x
    print(dice[x])

Submission status: Accepted

Solution code:
a, b, c, d, e, f = map(int, input().split())

num = int(input())

def dice1(x):
    if x == b:
        return c
    elif x == c:
        return e
    elif x == d:
        return b
    else:
        return d

def dice2(x):
    if x == a:
        return d
    elif x == c:
        return a
    elif x == d:
        return f
    else:
        return c

def dice3(x):
    if x == a:
        return b
    elif x == b:
        return f
    elif x == e:
        return a
    else:
        return e

def dice4(x):
    if x == a:
        return e
    elif x == b:
        return a
    elif x == e:
        return f
    else:
        return b

def dice5(x):
    if x == a:
        return c
    elif x == c:
        return f
    elif x == d:
        return a
    else:
        return d

def dice6(x):
    if x == b:
        return d
    elif x == c:
        return b
    elif x == d:
        return e
    else:
        return c

for i in range(num):
    j, k = map(int, input().split())
    if j == a:
        print(dice1(k))
    elif j == b:
        print(dice2(k))
    elif j == c:
        print(dice3(k))
    elif j == d:
        print(dice4(k))
    elif j == e:
        print(dice5(k))
    else:
        print(dice6(k))

Submission status: Accepted

Solution code:
labels = input().split()
q = int(input())
dices = [[1, 3, 4, 2], 
         [0, 2, 5, 3], 
         [0, 4, 5, 1], 
         [0, 1, 5, 4], 
         [0, 3, 5, 2], 
         [1, 2, 4, 3]]

for i in range(q):
    n = [labels.index(s) for s in input().split()]
    b = (dices[n[1]].index(n[0]) + 1) % 4
    a = dices[n[1]][b]
    print(labels[a])

Submission status: Accepted

Solution code:
def f(d):
    s = {'E':"310542",'S':"402351",'R':"031425"}
    a, b = input().split()
    for c in "SE":
        for i in range(4):
            d = [d[int(i)]for i in s[c]]
            for j in range(4):
                d = [d[int(i)]for i in s['R']]
                if d[0] == a and d[1] == b:
                    print(d[2])
                    return

d = input().split()
for n in range(int(input())): f(d)