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:
dice = list(map(int, input().split()))
q = int(input())

def dicing(lis, inst):
    if inst == 'N':
        ans_lis = [lis[1],lis[5],lis[2],lis[3],lis[0],lis[4]]
    if inst == 'W':
        ans_lis = [lis[2],lis[1],lis[5],lis[0],lis[4],lis[3]]
    if inst == 'S':
        ans_lis = [lis[4],lis[0],lis[2],lis[3],lis[5],lis[1]]
    if inst == 'E':
        ans_lis = [lis[3],lis[1],lis[0],lis[5],lis[4],lis[2]]
    
    return(ans_lis)

def turn_right(lis):
    inst = list('ENW')
    for i in range(len(inst)):
        lis = dicing(lis, inst[i])
    return(lis)

def find_above(lis, num):
    inst1 = list('NNNEWW')
    for i in range(len(inst1)):
        if lis[0] == num:
            break
        
        lis = dicing(lis, inst1[i])
        if lis[0] == num:
            break
    return(lis)

for i in range(q):
    question = list(map(int, input().split()))
    lis = [1,2,3,4,5,6]
    lis = find_above(lis, dice.index(question[0]) + 1)
    for j in range(4):
        lis = turn_right(lis)
        if dice[lis[1]- 1] == question[1]:
            break

    print(dice[lis[2]-1])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, e1, e2, e3, e4, e5, e6):
        self.eye1 = e1
        self.eye2 = e2
        self.eye3 = e3
        self.eye4 = e4
        self.eye5 = e5
        self.eye6 = e6
    
    def roll_e(self):
        tmp_eye   = self.eye3
        self.eye3 = self.eye1
        self.eye1 = self.eye4
        self.eye4 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_n(self):
        tmp_eye   = self.eye5
        self.eye5 = self.eye1
        self.eye1 = self.eye2
        self.eye2 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_s(self):
        tmp_eye   = self.eye2
        self.eye2 = self.eye1
        self.eye1 = self.eye5
        self.eye5 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_w(self):
        tmp_eye   = self.eye4
        self.eye4 = self.eye1
        self.eye1 = self.eye3
        self.eye3 = self.eye6
        self.eye6 = tmp_eye
    
    def roll_h(self):
        tmp_eye   = self.eye2
        self.eye2 = self.eye3
        self.eye3 = self.eye5
        self.eye5 = self.eye4
        self.eye4 = tmp_eye
    
    def get_eye1(self):
        return self.eye1
    
    def get_eye2(self):
        return self.eye2

    def get_eye3(self):
        return self.eye3

e1, e2, e3, e4 ,e5, e6 = map(int,input().split())
dice1 = Dice(e1, e2, e3, e4 ,e5, e6)
roll = int(input())
for i in range(roll):
    t1, t2 = map(int,input().split())
    while t1 != dice1.get_eye1():
        dice1.roll_e()
        if t1 != dice1.get_eye1():
            dice1.roll_n()
        else:
            break
    while t2 != dice1.get_eye2():
        dice1.roll_h()
    print(dice1.get_eye3())

Submission status: Accepted

Solution code:
from collections import deque

class Dice():
    # def __init__(self, sequence):
    #     self.v = deque([seq[5], seq[4], seq[0], seq[1]])
    #     self.h = deque([seq[3], seq[0], seq[2], seq[5]])

    def __init__(self, seq):
        self.v = deque([seq[5], seq[4], seq[0], seq[1]])
        self.h = deque([seq[3], seq[0], seq[2], seq[5]])

    def lotate(self, direction):

        if   direction == "E":
            self.h.rotate()
            self.v[2] = self.h[1]
            self.v[0] = self.h[3]

        elif direction == "W":
            self.h.rotate(-1)
            self.v[2] = self.h[1]
            self.v[0] = self.h[3]

        elif direction == "N":
            self.v.rotate(-1)
            self.h[1] = self.v[2]
            self.h[3] = self.v[0]

        elif direction == "S":
            self.v.rotate()
            self.h[1] = self.v[2]
            self.h[3] = self.v[0]

        else:
            pass

    def disp_top_face(self):
        return self.v[2]

    def disp_front_face(self):
        return self.v[3]

    def disp_back_face(self):
        return self.v[1]

    def disp_right_face(self):
        return self.h[2]

    def disp_left_face(self):
        return self.h[0]

    def disp_bottom_face(self):
        return self.h[3]

if __name__ == "__main__":

    param = input().split(" ")

    seq = [int(a) for a in param]
    d = Dice(seq)

    n = int(input())

    for _ in range(n):

        param = input().split()
        top = int(param[0])
        front = int(param[1])

        flag = 1

        while(flag):

            for _ in range(4):
                d.lotate("S")

                if d.disp_front_face() == front :
                    flag = 0
                    break

            if flag != 0:
                d.lotate("E")


        flag = 1

        while(flag):

            for _ in range(4):
                d.lotate("E")

                if d.disp_top_face() == top :
                    flag = 0
                    break

        print(d.disp_right_face())

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self, dice_labels):
        self.top = dice_labels[0]
        self.south = dice_labels[1]
        self.east = dice_labels[2]
        self.west = dice_labels[3]
        self.north = dice_labels[4]
        self.bottom = dice_labels[5]

    def roll(self, str_direction):
        if str_direction == 'E':
            self.__roll_to_east()
        elif str_direction == 'W':
            self.__roll_to_west()
        elif str_direction == 'S':
            self.__roll_to_south()
        elif str_direction == 'N':
            self.__roll_to_north()

    def turn(self, str_direction):
        if str_direction == 'L':
            self.__turn_left()
        elif str_direction == 'R':
            self.__turn_right()

    def __roll_to_east(self):
        self.top, self.east, self.west, self.bottom = (self.west, self.top, self.bottom, self.east)

    def __roll_to_west(self):
        self.top, self.east, self.west, self.bottom = (self.east, self.bottom, self.top, self.west)

    def __roll_to_south(self):
        self.top, self.south, self.north, self.bottom = (self.north, self.top, self.bottom, self.south)

    def __roll_to_north(self):
        self.top, self.south, self.north, self.bottom = (self.south, self.bottom, self.top, self.north)

    def __turn_right(self):
        self.south, self.east, self.west, self.north = (self.east, self.north, self.south, self.west)

    def __turn_left(self):
        self.south, self.east, self.west, self.north = (self.west, self.south, self.north, self.east)

dice_labels = input().split(' ')
num_question = int(input())
dice = Dice(dice_labels)

for i in range(0, num_question):
    target_top_label, target_for_label = tuple(input().split(' '))
    for j in range(0, 2):
        for k in range(0, 4):
            if dice.top != target_top_label:
                dice.roll('N')
        dice.turn('R')

    for j in range(0, 4):
        if dice.south != target_for_label:
            dice.turn('R')

    print(dice.east)

Submission status: Accepted

Solution code:
S = [
    (1, 2, 4, 3), # 0
    (0, 3, 5, 2), # 1
    (0, 1, 5, 4), # 2
    (0, 4, 5, 1), # 3
    (0, 2, 5, 3), # 4
    (1, 3, 4, 2), # 5
]

*L, = map(int, input().split())
q = int(input())
for i in range(q):
    a, b = map(int, input().split())
    for i in range(6):
        if L[i] == a:
            for k in range(4):
                if L[S[i][k]] == b:
                    print(L[S[i][k-3]])

Submission status: Accepted

Solution code:
def lotate(dic, dire):
    if dire == 'N':
        x,y,z,w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['back'], dic['bottom'], dic['front'], dic['up'] = x,y,z,w
    elif dire == 'S':
        x, y, z, w = dic['up'], dic['back'], dic['bottom'], dic['front']
        dic['front'], dic['up'], dic['back'], dic['bottom'] = x, y, z, w
    elif dire == 'W':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['left'], dic['bottom'], dic['right'], dic['up'] = x, y, z, w
    elif dire == 'E':
        x, y, z, w = dic['up'], dic['left'], dic['bottom'], dic['right']
        dic['right'], dic['up'], dic['left'], dic['bottom'] = x, y, z, w
    return dic


a,b,c,d,e,f = map(int, input().split())
n = int(input())
for _ in range(n):
    dic = {'up': a, 'front': b, 'right': c, 'left': d, 'back': e, 'bottom': f}
    x, y = map(int, input().split())
    s = 'EEENEEENEEESEEESEEENEEEN'
    for i in s:
        if dic['up'] == x and dic['front'] == y:
            print(dic['right'])
            break
        dic = lotate(dic, i)

Submission status: Accepted

Solution code:
import random


class Dice(object):

    def __init__(self, *args):
        self.faces = [None]
        self.faces.extend(args)
        self.top = self.faces[1]
        self.up = self.faces[5]
        self.down = self.faces[2]
        self.left = self.faces[4]
        self.right = self.faces[3]

    def roll(self, direction):
        if direction == "N":
            self.top, self.up, self.down = self.down, self.top, self.faces[7 - self.faces.index(self.top)]
        elif direction == "S":
            self.top, self.up, self.down = self.up, self.faces[7 - self.faces.index(self.top)], self.top
        elif direction == "W":
            self.top, self.left, self.right = self.right, self.top, self.faces[7 - self.faces.index(self.top)]
        elif direction == "E":
            self.top, self.left, self.right = self.left, self.faces[7 - self.faces.index(self.top)], self.top
        else:
            raise ValueError("{} is not valid direction.".format(direction))


f = [int(i) for i in input().split()]
dice = Dice(*f)

n = int(input())
for i in range(n):
    top, down = [int(i) for i in input().split()]
    while dice.top != top:
        dice.roll(random.choice("NSWE"))
    if dice.down == down:
        print(dice.right)
    elif dice.right == down:
        print(dice.up)
    elif dice.up == down:
        print(dice.left)
    elif dice.left == down:
        print(dice.down)
    else:
        raise ValueError()

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self, up, back, right, left, front, down):
        self.up = up
        self.down = down
        self.right = right
        self.left = left
        self.front = front
        self.back = back
    
    def north(self):
        temp = self.up
        self.up = self.back
        self.back = self.down
        self.down = self.front
        self.front = temp
    
    def south(self):
        temp = self.up
        self.up = self.front
        self.front = self.down
        self.down = self.back
        self.back = temp
        
    def east(self):
        temp = self.up
        self.up = self.left
        self.left = self.down
        self.down = self.right
        self.right = temp
    
    def west(self):
        temp = self.up
        self.up = self.right
        self.right = self.down
        self.down = self.left
        self.left = temp
    
    def counterclockwise(self):
        temp = self.back
        self.back = self.left
        self.left = self.front
        self.front = self.right
        self.right = temp
    
    def clockwise(self):
        temp = self.back
        self.back = self.right
        self.right = self.front
        self.front = self.left
        self.left = temp
        
    def operate(self, op):
        if op == "N":
            self.north()
        elif op == "S":
            self.south()
        elif op == "E":
            self.east()
        elif op == "W":
            self.west()
        
    def fix_up(self, up):
        if self.down == up:
            self.north()
            self.north()
        elif self.right == up:
            self.west()
        elif self.left == up:
            self.east()
        elif self.front == up:
            self.south()
        elif self.back == up:
            self.north()

    def fix_back_with_fixed_up(self, back):
        if self.right == back:
            self.clockwise()
        elif self.left == back:
            self.counterclockwise()
        elif self.front == back:
            self.clockwise()
            self.clockwise()

up, back, right, left, front, down = map(int, input().split())
dice = Dice(up, back, right, left, front, down)

question_n = int(input())
for _ in range(question_n):
    q_up, q_back = map(int, input().split())
    dice.fix_up(q_up)
    dice.fix_back_with_fixed_up(q_back)
    print(dice.right)

Submission status: Accepted

Solution code:
def right(a,b,a1,a2,a3,a4,a5,a6):
    b1=a6
    b2=a5
    b3=a4
    b4=a3
    b5=a2
    b6=a1
  
    if a>b:
        tmp_a=a
        a=b
        b=tmp_a
        
    if [a,b]==[a1,a2]:
        right_side=[a3,b3]
    elif [a,b]==[a1,a3]:
        right_side=[a5,b5]
    elif [a,b]==[a1,a4]:
        right_side=[a2,b2]
    elif [a,b]==[a1, a5]:
        right_side=[a4,b4]
    elif [a,b]==[a2, a3]:
        right_side=[a1,b1]
    elif [a, b]==[a2, a4]:
        right_side=[a6,b6]
    elif [a,b]==[a2,a6]:
        right_side=[a3,b3]
    elif [a,b]==[a3, a5]:
        right_side=[a1,b1]
    elif [a,b]==[a3, a6]:
        right_side=[a5,b5]
    elif [a, b]==[a4, a5]:
        right_side=[a6,b6]
    elif [a,b]==[a4,a6]:
        right_side=[a2,b2]
    elif [a,b]==[a5,a6]:
        right_side=[a4,b4]
  
    return right_side
    

initial=list( map(int,input().split()))
num_of_q=int(input())

for i in range(0, num_of_q):
    a=list(map(int, input().split()))
    flag=0
    if a[0]>a[1]:
        flag=1
    answer=right(*a,*initial)
    print(answer[flag])

Submission status: Accepted

Solution code:
class Dice():
    top=1
    n=2
    e=4
    w=3
    s=5
    dice=[1,2,3,4,5,6]

    top_front_right=[[-1,3,5,2,4,-1],[4,-1,1,6,-1,3],[2,6,-1,-1,1,5],
                     [5,1,-1,-1,6,2],[3,-1,6,1,-1,4],[-1,4,2,5,3,-1]]

    def __init__(self,num):
        self.dice=num

    def roll(self,direction):
        if(direction=="N"):
            self.top,self.n,self.s = self.n,7-self.top,self.top
        elif(direction=="E"):
            self.top,self.e,self.w = self.e,7-self.top,self.top
        elif(direction=="W"):
            self.top,self.e,self.w = self.w,self.top,7-self.top
        elif(direction=="S"):
            self.top,self.n,self.s = self.s,self.top,7-self.top




d=list(map(int,input().split()))
dice=Dice(d)
n=int(input())
for i in range(n):
    top,front=map(int,input().split())
    print(d[dice.top_front_right[d.index(top)][d.index(front)]-1])

Submission status: Accepted

Solution code:
class Dice:
    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 rot(self,way):
        if way == 0:
            c = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = c
        if way == 1:
            c = self.e
            self.e = self.n
            self.n = self.w
            self.w = self.s
            self.s = c
        if way == 2:
            c = self.w
            self.w = self.d
            self.d = self.e
            self.e = self.u
            self.u = c
u,s,e,w,n,d = map(int,input().split())
dice = Dice(u,s,e,w,n,d)
questions = int(input())
for q in range(questions):
    top, south = map(int,input().split())
    flag = 0
    count = 0
    while True:
        for v in range(5):
            if dice.u == top and dice.s == south:
                print(dice.e)
                flag = 1
                break
            else:
                dice.rot(0)
        if flag:
            break
        elif count == 5:
            dice.rot(2)
            count = 0
        else:
            dice.rot(1)
            count += 1

Submission status: Accepted

Solution code:
s = list(map(int, input().split()))
q = int(input())
# 上面をnとして時計回りにサイコロを回したときの正面のindexの一覧[a_1,a_2,a_3,a_4]
# 正面がa_nのとき右面はa_n+1
dm = {0: [1, 2, 4, 3, 1],
      1: [0, 3, 5, 2, 0],
      2: [0, 1, 5, 4, 0],
      3: [0, 4, 5, 1, 0],
      4: [0, 2, 5, 3, 0],
      5: [1, 3, 4, 2, 1]}

for i in range(q):
    t, f = map(lambda x: s.index(int(x)), input().split())
    print(s[dm[t][dm[t].index(f)+1]])

Submission status: Accepted

Solution code:
class rightnumber():
    def __init__(self):
        self.top = [[0, 0, 0, 0], [4, 2, 3, 5], [4, 6, 3, 1], [1, 2, 6, 5], [1, 5, 6, 2], [1, 3, 6, 4], [4, 5, 3, 2]]
        self.front =[[0, 0, 0, 0], [2, 3, 5, 4], [6, 3, 1, 4], [2, 6, 5, 1], [5, 6, 2, 1], [3, 6, 4, 1], [5, 3, 2, 4]]
inp = list(map(int, input().split()))
q = int(input())
rightnumbers = rightnumber()
top= [0]*q
front = [0]*q
key1 = [0]*q
key2 = [0]*q
for n in range(q):
    top[n], front[n] = map(int, input().split())
    for k in range(6):
        if top[n] == inp[k]:
            key1[n] = k;
        if front[n] == inp[k]:
            key2[n] = k
for n in range(q):
    for i in range(1, 7):
        for j in range(0, 4):
            if rightnumbers.top[i][j] == key1[n]+1 and rightnumbers.front[i][j] == key2[n]+1:
                print(inp[i-1])

Submission status: Accepted

Solution code:
rot_set = [
    (1,2,4,3,1), #0
    (0,3,5,2,0), #1
    (0,1,5,4,0), #2
    (0,4,5,1,0), #3
    (0,2,5,3,0), #4
    (1,3,4,2,1)  #5
]

dice_int = list(map(int, input().split()))
q = int(input())
right_face = []

for _ in range(q):
    a, b = map(int, input().split())
    a_idx = dice_int.index(a)
    b_idx = dice_int.index(b)

    for i in range(6):
        for j in range(4):
            if (a_idx, b_idx) == (rot_set[i][j], rot_set[i][j+1]):
                right_face.append(dice_int[i])

for i in range(q):
    print(right_face[i])

Submission status: Accepted

Solution code:
import random

class Dice():
    def __init__(self):
        self.face = [i for i in range(6)]
        self.memo = [i for i in range(6)]

    def setface(self,initface):
        for i in range(6):
            self.face[i] = initface[i]

    def roll(self,direction):
        for i in range(6):
            self.memo[i] = self.face[i]
        index = {'E':(3,1,0,5,4,2),'N':(1,5,2,3,0,4),'S':(4,0,2,3,5,1),'W':(2,1,5,0,4,3)}
        for i in range(6):
            self.face[i] = self.memo[index[direction][i]]

    def top(self):
        return self.face[0]

dice = Dice()
initface = list(map(int,input().split()))
dice.setface(initface)
q = int(input())
for _ in range(q):
    u,f = map(int,input().split())
    while True:
        dice.roll(['E','N','S','W'][random.randint(0,3)])
        if dice.face[0]==u and dice.face[1]==f:
            print(dice.face[2])
            break

Submission status: Accepted

Solution code:
class dice:
    def __init__(self, f):
        assert(len(f) == 6)
        self.face = f
        self.N = [1, 5, 2, 3, 0, 4]
        self.S = [4, 0, 2, 3, 5, 1]
        self.E = [3, 1, 0, 5, 4, 2]
        self.W = [2, 1, 5, 0, 4, 3]
        self.R = [0, 2, 4, 1, 3, 5]

    def rotate(self, dir):
        rot = self.__getattribute__(dir)
        old = self.face
        new = [-1 for _ in range(6)]
        for i in range(6):
            new[i] = old[rot[i]]
        self.face = new


F = list(map(int, input().split()))
D = dice(F)
m = int(input())
for _ in range(m):
    a, b = map(int, input().split())
    for _ in range(4):
        if a == D.face[0]:
            break
        else:
            D.rotate('N')
    if a != D.face[0]:
        for _ in range(4):
            if a == D.face[0]:
                break
            else:
                D.rotate('E')
    assert(a == D.face[0])
    for _ in range(4):
        if b == D.face[1]:
            break
        else:
            D.rotate('R')
    assert(b == D.face[1])
    print(D.face[2])

Submission status: Accepted

Solution code:
class Dice():

    def __init__(self, d):
        self.d = d
        self.north = [2, 6, 3, 4, 1, 5]
        self.south = [5, 1, 3, 4, 6, 2]
        self.east = [4, 2, 1, 6, 5, 3]
        self.west = [3, 2, 6, 1, 5, 4]
        self.label = {(d[0], d[1]): d[2]}

    def roll(self, direction):
        if direction == 'S':
            self.d = [ self.d[self.south[i]-1] for i in range(6) ]
        elif direction == 'N':
            self.d = [ self.d[self.north[i]-1] for i in range(6) ]
        elif direction == 'E':
            self.d = [ self.d[self.east[i]-1] for i in range(6) ]
        elif direction == 'W':
            self.d = [ self.d[self.west[i]-1] for i in range(6) ]

    def rollAll(self):
        for i in range(4):
            for i in range(4):
                self.roll('E')
                self.label[(self.d[0], self.d[1])] = self.d[2]
            self.roll('S')
        self.roll('E')
        self.roll('S')
        for i in range(4):
            self.roll('E')
            self.label[(self.d[0], self.d[1])] = self.d[2]
        self.roll('S')
        self.roll('S')
        for i in range(4):
            self.roll('E')
            self.label[(self.d[0], self.d[1])] = self.d[2]

    def getLabel(self):
        return self.label

    def getDice(self):
        return self.d

s = [ int(s) for s in input().split() ]
n = int(input())
dice = Dice(s)
dice.rollAll()
l = dice.getLabel()

for i in range(n):
    a,b = [ int(s) for s in input().split() ]
    print(l[(a,b)])

Submission status: Accepted

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

diceB = [[1,2,3,5,4,2],[2,1,4,6,3,1],[3,1,2,6,5,1],[4,1,5,6,2,1],[5,1,3,6,4,1],[6,2,4,5,3,2]]

q = int(input())

for i in range(q):
  quest = list(map(int, input().split()))
  ans = 0
  for j in range(6):
    if quest[0] == diceA[j]:
      for k in range(6):
        if quest[1] == diceA[k]:
          for l in range(1,5):
            if diceB[j][l] == k+1:
              ans = diceB[j][l+1]
  print(diceA[ans-1])

Submission status: Accepted

Solution code:
import sys

# 立方体で数字が自由なさいころ
class Dice:
    def __init__(self, top, bottom, front, rear, right, left):
        self.top = top
        self.front = front
        self.right = right
        self.bottom = bottom
        self.rear = rear
        self.left = left

    def rotate_front(self):
        t, b, f, r = self.top, self.bottom, self.front, self.rear
        self.top = r
        self.bottom = f
        self.front = t
        self.rear = b

    def rotate_rear(self):
        t, b, f, r = self.top, self.bottom, self.front, self.rear
        self.top = f
        self.bottom = r
        self.front = b
        self.rear = t

    def rotate_right(self):
        t, b, r, l = self.top, self.bottom, self.right, self.left
        self.top = l
        self.bottom = r
        self.right = t
        self.left = b

    def rotate_left(self):
        t, b, r, l = self.top, self.bottom, self.right, self.left
        self.top = r
        self.bottom = l
        self.right = b
        self.left = t

    def get_tfr(self):
        return [self.top, self.front, self.right]

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    top, front, right, left, rear, bottom = map(int, input().split())
    d = Dice(top, bottom, front, rear, right, left)
    tftor = {}

    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r

    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r

    d.rotate_right()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    d.rotate_front()
    d.rotate_front()
    t, f, r = d.get_tfr()
    tftor[(t, f)] = r
    for _ in range(3):
        d.rotate_right()
        t, f, r = d.get_tfr()
        tftor[(t, f)] = r
    # print(tftor)

    q = int(input())
    for _ in range(q):
        t, f = map(int, input().split())
        print(tftor[(t, f)])

main()

Submission status: Accepted

Solution code:
arr = input().split()

n = {
    0 : [(3, 1), (4, 3), (2, 4), (1, 2)],
    1 : [(0, 3), (3, 5), (5, 2), (2, 0)],
    2 : [(0, 1), (4, 0), (5, 4), (1, 5)],
    3 : [(0, 4), (4, 5), (5, 1), (1, 0)],
    4 : [(0, 2), (2, 5), (5, 3), (3, 0)],
    5 : [(1, 3), (3, 4), (4, 2), (2, 1)],
}

q = int(input())
for _ in range(q):
    a, b = input().split()
    
    for i in range(6):
        if (arr.index(a), arr.index(b)) in n[i]:
            print(arr[i])

Submission status: Accepted

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

def E(ls):
    ls=[ls[3],ls[1],ls[0],ls[5],ls[4],ls[2]]
    return ls

def N(ls):
    ls=[ls[1],ls[5],ls[2],ls[3],ls[0],ls[4]]
    return ls

def R(ls):
    ls=[ls[0],ls[2],ls[4],ls[1],ls[3],ls[5]]
    return ls

for i in range(q):
    num0,num1=list(map(int,input().split()))
    if num.index(num0) in [0,2,3,5]:
        while num[0]!=num0:
            num=E(num)
        while num[1]!=num1:
            num=R(num)
        print(num[2])
    else:
        while num[0]!=num0:
            num=N(num)
        while num[1]!=num1:
            num=R(num)
        print(num[2])

Submission status: Accepted

Solution code:
def rotate(arr,i):
    if i=='W':
        arr=[arr[k-1] for k in [3,2,6,1,5,4]]
    if i=='E':
        arr=[arr[k-1] for k in [4,2,1,6,5,3]]
    if i=='N':
        arr=[arr[k-1] for k in [2,6,3,4,1,5]]
    if i=='S':
        arr=[arr[k-1] for k in [5,1,3,4,6,2]]
    return arr
arr=input().split()

deck=[]
for i in 'W'*4:
    arr=rotate(arr,i)
    for k in 'NNNN':
        arr=rotate(arr,k)
        deck.append(arr)
arr=rotate(arr,'N')
arr=rotate(arr,'W')
for k in 'NNNN':
    arr=rotate(arr,k)
    deck.append(arr)
arr=rotate(arr,'W')
arr=rotate(arr,'W')
for k in 'NNNN':
    arr=rotate(arr,k)
    deck.append(arr)
n=int(input())
for i in range(n):
    query=input().split()
    for v in deck:
        if v[0]==query[0] and v[1]==query[1]:
            print(v[2])
            break

Submission status: Accepted

Solution code:
class Dice:
    
    def __init__(self, U, S, E, W, N, D):
        self.u, self.d = U, D
        self.e, self.w = E, W
        self.s, self.n = S, N       
    
    def roll(self, commmand):
        if command == 'E':
            self.e, self.u, self.w, self.d = self.u, self.w, self.d, self.e
        elif command == 'W':
            self.e, self.u, self.w, self.d = self.d, self.e, self.u, self.w
        elif command == 'S':
            self.s, self.u, self.n, self.d = self.u, self.n, self.d, self.s
        else:
            self.s, self.u, self.n, self.d = self.d, self.s, self.u, self.n

    def rightside(self, upside, forward):
        self.structure = {self.u:{self.w: self.s, self.s: self.e, self.e: self.n, self.n: self.w},
                          self.d:{self.n: self.e, self.e: self.s, self.s: self.w, self.w: self.n},
                          self.e:{self.u: self.s, self.s: self.d, self.d: self.n, self.n: self.u},
                          self.s:{self.u: self.w, self.w: self.d, self.d: self.e, self.e: self.u},
                          self.w:{self.u: self.n, self.n: self.d, self.d: self.s, self.s: self.u},
                          self.n:{self.u: self.e, self.e: self.d, self.d: self.w, self.w: self.u}}
        
        print(self.structure[upside][forward])

U, S, E, W, N, D = [int(i) for i in input().split()]
turns = int(input())
dice1 = Dice(U, S, E, W, N, D)
for i in range(turns):
    upside, forward = [int(k) for k in input().split()]
    dice1.rightside(upside, forward)

Submission status: Accepted

Solution code:
apex = ((1,2,3),
        (1,4,2),
        (1,5,4),
        (1,3,5),

        (2,1,4),
        (2,3,1),
        (2,6,3),
        (2,4,6),

        (3,1,2),
        (3,5,1),
        (3,6,5),
        (3,2,6),

        (4,1,5),
        (4,2,1),
        (4,6,2),
        (4,5,6),

        (5,3,6),
        (5,1,3),
        (5,4,1),
        (5,6,4),

        (6,3,2),
        (6,5,3),
        (6,4,5),
        (6,2,4)
        )

dice = {}
label = []
label[:] = map(int,input().split())
#print('label: {}'.format(label))

for i in range(0,6):
    dice[label[i]] = i+1
#print('dice: {}'.format(dice))


q = int(input())

for _ in range(0,q):
    numbers = []
    numbers[:] = map(int,input().split())
    right = 0
    for t in apex:
        if (t[0] == dice[numbers[0]] and t[1] == dice[numbers[1]]):
            right = t[2]
            #print('{} {} {}'.format(t[0],t[1],t[2]))
            break
    for key, n in dice.items():
        if (n == right):
            print(key)
            break

Submission status: Accepted

Solution code:
S = [
    (1, 2, 4, 3), # 0
    (0, 3, 5, 2), # 1
    (0, 1, 5, 4), # 2
    (0, 4, 5, 1), # 3
    (0, 2, 5, 3), # 4
    (1, 3, 4, 2), # 5
]

*L, = map(int, input().split())
q = int(input())
for i in range(q):
    a, b = map(int, input().split())
    for i in range(6):
        if L[i] == a:
            for k in range(4):
                if L[S[i][k]] == b:
                    print(L[S[i][k-3]])

Submission status: Accepted

Solution code:
import random

class Dice:
    def __init__(self):
        self.u=1
        self.w=2
        self.s=3
        self.e=4
        self.n=5
        self.d=6
        self.dic={"W":0,"S":1,"E":2,"N":3}
    
    def __init__(self,u,w,s,e,n,d):
        self.u=u
        self.w=w
        self.s=s
        self.e=e
        self.n=n
        self.d=d
        self.dic={"W":0,"S":1,"E":2,"N":3}

    def rot(self,way):
        if isinstance(way,str):
            way=self.dic[way]

        if way==0:
            c=self.u
            self.u=self.e
            self.e=self.d
            self.d=self.w
            self.w=c
        elif way==1:
            c=self.u
            self.u=self.n
            self.n=self.d
            self.d=self.s
            self.s=c
        elif way==2:
            c=self.u
            self.u=self.w
            self.w=self.d
            self.d=self.e
            self.e=c
        else :
            c=self.u
            self.u=self.s
            self.s=self.d
            self.d=self.n
            self.n=c


lst=["W","S","E","N"]
u,s,e,w,n,d=map(int,input().split())
N=int(input())
dice=Dice(u,w,s,e,n,d)
for i in range(N):
    top,front=map(int,input().split())
    while True:
        if dice.u==top and dice.w==front:
            break
        else:
            dice.rot(random.choice(lst))
    print(dice.s)