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 = input().split()
dice.insert(4, dice.pop(3))
right = []


def rightSearch(up, front):
    index_up = dice.index(up)
    index_front = dice.index(front)
    if 1 <= index_up <= 4 and 1 <= index_front <= 4:
        if index_up == 4 and index_front == 1:
            return dice[0]
        if index_up == 1 and index_front == 4:
            return dice[5]
        if index_up < index_front:
            return dice[0]
        else:
            return dice[5]
    elif (index_front == 0 or index_front == 5) and (index_up != 0 or index_up != 5):
        if index_front == 0:
            if index_up == 1:
                return dice[4]
            else:
                return dice[index_up - 1]
        else:
            if index_up == 4:
                return dice[1]
            else:
                return dice[index_up + 1]
    else:
        if index_up == 0:
            if index_front == 4:
                return dice[1]
            else:
                return dice[index_front + 1]
        else:
            if index_front == 1:
                return dice[4]
            else:
                return dice[index_front - 1]


number = int(input())
for i in range(number):
    up, front = input().split()
    right.append(rightSearch(up, front))

for r in right:
    print(r)

Submission status: Accepted

Solution code:
# サイコロ        初期状態
#    [1]           N
# [4][2][3][5]   W[1]E
#    [6]           S
#
# 上記の順番でラベルに対する整数が与えられる
# l1 l2 l3 l4 l5 l6
# 「上面[1] 前面[2]」が与えられて右側の面[3]を答える

class Dice:
    def __init__(self, l1, l2, l3, l4, l5, l6):
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3
        self.l4 = l4
        self.l5 = l5
        self.l6 = l6
        self.tmp = 0
    def move_n(self):
        self.tmp = self.l5
        self.l5 = self.l1
        self.l1 = self.l2
        self.l2 = self.l6
        self.l6 = self.tmp
    def move_s(self):
        self.tmp = self.l2
        self.l2 = self.l1
        self.l1 = self.l5
        self.l5 = self.l6
        self.l6 = self.tmp
    def move_e(self):
        self.tmp = self.l3
        self.l3 = self.l1
        self.l1 = self.l4
        self.l4 = self.l6
        self.l6 = self.tmp
    def move_w(self):
        self.tmp = self.l4
        self.l4 = self.l1
        self.l1 = self.l3
        self.l3 = self.l6
        self.l6 = self.tmp
    def show(self):
        return self.l1, self.l2, self.l3, self.l4, self.l5, self.l6

d1 = Dice(*map(int, input().split()))
n = int(input())

for _ in range(n):
    ue, mae = map(int, input().split())
    ne_switch = 0
    while d1.l2 != mae:
        if ne_switch < 3:
            d1.move_n()
            ne_switch+=1
        elif 3 <= ne_switch :
            d1.move_e()
            ne_switch+=1
        if ne_switch == 6: ne_switch = 0
    while d1.l1 != ue:
        d1.move_e()
    print(d1.l3)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

class Dice:

    def __init__(self,dicelist):
        self.dice_list = dicelist

    def roll(self, direction):
        work = list(self.dice_list)
        if (direction == 'N'):
            self.dice_list[0] = work[1]
            self.dice_list[1] = work[5]
            self.dice_list[2] = work[2]
            self.dice_list[3] = work[3]
            self.dice_list[4] = work[0]
            self.dice_list[5] = work[4]
        elif (direction == 'E'):
            self.dice_list[0] = work[3]
            self.dice_list[1] = work[1]
            self.dice_list[2] = work[0]
            self.dice_list[3] = work[5]
            self.dice_list[4] = work[4]
            self.dice_list[5] = work[2]
        elif (direction == 'S'):
            self.dice_list[0] = work[4]
            self.dice_list[1] = work[0]
            self.dice_list[2] = work[2]
            self.dice_list[3] = work[3]
            self.dice_list[4] = work[5]
            self.dice_list[5] = work[1]
        elif (direction == 'W'):
            self.dice_list[0] = work[2]
            self.dice_list[1] = work[1]
            self.dice_list[2] = work[5]
            self.dice_list[3] = work[0]
            self.dice_list[4] = work[4]
            self.dice_list[5] = work[3]

    def roll_until_index0top(self,num0):
        if self.dice_list[0] == num0:
            return
        commands = ['E', 'E', 'E', 'N', 'N', 'N']
        for c in commands:
            self.roll(c)
            if self.dice_list[0] == num0:
                return
        else:
            print('error')

    def get_index_by_number(self, num):
        for i in range(len((self.dice_list))):
            if self.dice_list[i] == num:
                return i
        else:
            return -1

    def get_index2_number(self, num1):
        index1 = self.get_index_by_number(num1)
        if index1 == 1:
            return self.dice_list[2]
        elif (index1 == 2):
            return self.dice_list[4]
        elif (index1 == 3):
            return self.dice_list[1]
        elif (index1 == 4):
            return self.dice_list[3]
        else:
            print('error 2')

    def getTop(self):
        return self.dice_list[0]


dice_list = list(map(int, stdin.readline().rstrip().split()))
q = int(stdin.readline().rstrip())
dice = Dice(dice_list)

for i in range(q):
    num0_num1 = list(map(int, stdin.readline().rstrip().split()))
    dice.roll_until_index0top(num0_num1[0])
    print(dice.get_index2_number(num0_num1[1]))

Submission status: Accepted

Solution code:
import copy
import random

D = list(map(int, input().split()))
q = int(input())

for x in range(q):
    y, z = map(int, input().split())
    while True:
        r = random.randint(0,3)
        if r == 0:
            Dt = copy.copy(D)
            temp = Dt[0]
            Dt[0] = Dt[4]
            Dt[4] = Dt[5]
            Dt[5] = Dt[1]
            Dt[1] = temp
        elif r== 1:
            Dt = copy.copy(D)
            temp = Dt[0]
            Dt[0] = Dt[2]
            Dt[2] = Dt[5]
            Dt[5] = Dt[3]
            Dt[3] = temp
        elif r== 2:
            Dt = copy.copy(D)
            temp = Dt[0]
            Dt[0] = Dt[3]
            Dt[3] = Dt[5]
            Dt[5] = Dt[2]
            Dt[2] = temp
        elif r == 3:
            Dt = copy.copy(D)
            temp = Dt[0]
            Dt[0] = Dt[1]
            Dt[1] = Dt[5]
            Dt[5] = Dt[4]
            Dt[4] = temp

        D = copy.copy(Dt)
        if Dt[0] == y and Dt[1] == z:
            print(Dt[2])
            break

Submission status: Accepted

Solution code:
from enum import IntEnum

class Face(IntEnum):
    One=1
    Two=2
    Three=3
    Four=4
    Five=5
    Six=6


class Dice:
    def __init__(self):
        self.top=Face.One
        self.bottom=Face.Six
        self.back=Face.Two
        self.front=Face.Five
        self.left=Face.Four
        self.right=Face.Three
    
    def roll(self,command):
        if command=="E":
            #back-frontを軸として右回転
            temp=self.top
            self.top=self.left
            self.left=self.bottom
            self.bottom=self.right
            self.right=temp
            
        elif command=="N":
            #right-leftを固定して前転
            temp=self.top
            self.top=self.back
            self.back=self.bottom
            self.bottom=self.front
            self.front=temp
            
        elif command=="S":
            #right-leftを固定して後転
            temp=self.top
            self.top=self.front
            self.front=self.bottom
            self.bottom=self.back
            self.back=temp
            
        elif command=="W":
            #back-frontを軸として左回転
            temp=self.top
            self.top=self.right
            self.right=self.bottom
            self.bottom=self.left
            self.left=temp
      
def num_pattern_match(num_list,pattern):
    for i,x in enumerate(num_list):
        is_match=True
        for j,y in enumerate(pattern):
            if num_list[(i+j) % len(num_list)] == pattern[j]:
                pass
            else:
                is_match=False
        if is_match:
            return True
            
    return False      

def build_dice(top,back):
    #2つの面からさいころの状態を初期化する
    dice=Dice()
    dice.top=top
    dice.back=back
    
    dice.bottom=Face(7-int(dice.top))
    dice.front=Face(7-int(dice.back))

    #left-rightはどう計算する?
    #難しく考えないで確実にできる簡単な方法を実行する
    exist_dice_list=[int(dice.top),
    int(dice.bottom),
    int(dice.front),
    int(dice.back)]
    
    left_face_num=set([i+1 for i in range(6)]) - set(exist_dice_list)
    
    #top->front->bottom->backの面のリストを作成する
    #泥臭いプログラミングできるかどうかにかかっている
    face_list=[int(dice.top),int(dice.front),int(dice.bottom),int(dice.back)]

    #泥臭くても手を動かせば完了するときはさっさと手を動かして完了させる
    if 1 in left_face_num:
        #1,5が残されているパターン
        pattern=[2,3,5,4]
        if num_pattern_match(face_list,pattern):
            #top->front->bottom->backに走査したとき、2354に一致するとき
            #左に1が来る
            dice.right=Face.Six
            dice.left=Face.One
        else:
            dice.right=Face.One
            dice.left=Face.Six
            
    elif 2 in left_face_num:
        pattern=[1,4,6,3]
        if num_pattern_match(face_list,pattern):
            dice.right=Face.Five
            dice.left=Face.Two
        else:
            dice.right=Face.Two
            dice.left=Face.Five
            
    elif 3 in left_face_num:
        pattern=[2,6,5,1]
        if num_pattern_match(face_list,pattern):
            dice.right=Face.Four
            dice.left=Face.Three
        else:
            dice.right=Face.Three
            dice.left=Face.Four           
        
    return dice

def run():
    dice_score_ls=list(map(int,input().split()))
    
    dice_score_dict={i+1:score for i,score in enumerate(dice_score_ls)}
    d_inv={v:k for k,v in dice_score_dict.items()}
    
    query_num=int(input())

    for i in range(query_num):
        top,back=tuple(map(int,input().split()))
        top=d_inv[top]
        back=d_inv[back]
        
        dice=build_dice(top,back)
        
        # print(int(dice.right))
        print(dice_score_dict[int(dice.right)])
        


run()

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, numbers):
        self.dice = {"上" :numbers[0],
                     "下":numbers[5], 
                     "前":numbers[1],
                     "後":numbers[4],
                     "左":numbers[3],
                     "右":numbers[2]
                    }
    
    def order(self, order_seq):
        for _, order in enumerate(order_seq):
            if order == "N":
                self.north()
            elif order == "E":
                self.east()
            elif order == "W":
                self.west()
            elif order == "S":
                self.south()
        
        return self.dice["上"]

    def north(self):
        U, D, F, B = [self.dice["上"], self.dice["下"], self.dice["前"], self.dice["後"]]
        self.dice["上"] = F
        self.dice["前"] = D
        self.dice["下"] = B
        self.dice["後"] = U

    def east(self):
        U, D, L, R = [self.dice["上"], self.dice["下"], self.dice["左"], self.dice["右"]]
        self.dice["上"] = L
        self.dice["下"] = R
        self.dice["左"] = D
        self.dice["右"] = U

    def west(self):
        U, D, L, R = [self.dice["上"], self.dice["下"], self.dice["左"], self.dice["右"]]
        self.dice["上"] = R
        self.dice["下"] = L
        self.dice["左"] = U
        self.dice["右"] = D

    def south(self):
        U, D, F, B = [self.dice["上"], self.dice["下"], self.dice["前"], self.dice["後"]]
        self.dice["上"] = B
        self.dice["前"] = U
        self.dice["下"] = F
        self.dice["後"] = D   

    def set_position(self, n, position_seq):
        self.ans = []
        for i in range(n):
            self.up = position_seq[i][0]
            self.front = position_seq[i][1]
            
            self.search()
            self.west()
            self.search()
            self.west()
            self.search()
            self.north()
            self.search()
            self.west()
            self.search()
            self.west()
            self.search()
        
        return self.ans
    
    def search(self):
        # print("goal:now", self.up, ":", self.dice["上"])
        if self.dice["上"] == self.up:
            for _ in range(4):
                # print("goal:now|up ", self.front, ":", self.dice["前"], "|", self.dice["上"], "→", self.dice["右"])
                if self.dice["前"] == self.front:
                    break
                self.rotate()
            self.ans.append(self.dice["右"])

    def rotate(self):
        F, R, B, L = [self.dice["前"], self.dice["右"], self.dice["後"], self.dice["左"]]
        self.dice["前"] = R
        self.dice["右"] = B
        self.dice["後"] = L
        self.dice["左"] = F
        
numbers = list(map(int, input().split()))
n = int(input())
pos_seq = []
for _ in range(n):
    pos_seq.append(list(map(int, input().split())))

dice = Dice(numbers)
ans = dice.set_position(n, position_seq = pos_seq)
for i in range(n):
    print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = list(map(int, input().split()))
class Dice:
    def __init__(self, num):
        self.state = {}
        for i in range(1,6+1):
            self.state[i] = num[i-1]

        self.reversed_state = {}
        for key,val in zip(self.state.keys(), self.state.values()):
            self.reversed_state[val] = key
        
    def search_side(self, x,y):
        a = self.reversed_state[x]
        b = self.reversed_state[y]
        if str(a)+str(b) in '12651':
            side = self.state[3]
        elif str(b)+str(a) in '12651':
            side = self.state[4]
        elif str(a)+str(b) in '13641':
            side = self.state[5]
        elif str(b)+str(a) in '13641':
            side = self.state[2]
        elif str(a)+str(b) in '23542':
            side = self.state[1]
        elif str(b)+str(a) in '23542':
            side = self.state[6]
        return side
        
dice = Dice(num)
q = int(input())
for _ in range(q):
    x,y = map(int, input().split())
    ans = dice.search_side(x,y)
    print(ans)

Submission status: Accepted

Solution code:
a = [int(i) for i in input().split()]
n = int(input())

class Dise:
    def __init__(self,a):
        self.dise = [0 for i in range(len(a))]
        for i in range(len(a)):
            self.dise[i] = a[i]

    def roll(self,dir):
        for self.pop_str in dir:
            if self.pop_str == "N":
                self.dise[0],self.dise[1],self.dise[4],self.dise[5] = self.dise[1],self.dise[5],self.dise[0],self.dise[4]
            elif self.pop_str == "S":
                self.dise[0],self.dise[1],self.dise[4],self.dise[5] = self.dise[4],self.dise[0],self.dise[5],self.dise[1]
            elif self.pop_str == "E":
                self.dise[0],self.dise[2],self.dise[3],self.dise[5] = self.dise[3],self.dise[0],self.dise[5],self.dise[2]
            elif self.pop_str == "W":
                self.dise[0],self.dise[2],self.dise[3],self.dise[5] = self.dise[2],self.dise[5],self.dise[0],self.dise[3]

    def N(self):
        self.dise[0],self.dise[1],self.dise[4],self.dise[5] = self.dise[1],self.dise[5],self.dise[0],self.dise[4]

    def S(self):
        self.dise[0],self.dise[1],self.dise[4],self.dise[5] = self.dise[4],self.dise[0],self.dise[5],self.dise[1]

    def E(self):
        self.dise[0],self.dise[2],self.dise[3],self.dise[5] = self.dise[3],self.dise[0],self.dise[5],self.dise[2]

    def W(self):
        self.dise[0],self.dise[2],self.dise[3],self.dise[5] = self.dise[2],self.dise[5],self.dise[0],self.dise[3]

    def R(self):
        self.dise[1],self.dise[2],self.dise[3],self.dise[4] = self.dise[3],self.dise[1],self.dise[4],self.dise[2]

    def L(self):
        self.dise[1],self.dise[2],self.dise[3],self.dise[4] = self.dise[2],self.dise[4],self.dise[1],self.dise[3]

    #確認用
    def output(self):
        print(self.dise[2])


for i in range(n):
    X = Dise(a)
    count = 0
    top,front = [int(i) for i in input().split()]

    while X.dise[0] != top and X.dise[1] != front:
        X.N()
        count += 1
        if count >= 4:
            X.E()
            count += 1
            if count >= 8:
                X.R()

    if X.dise[0] == top:
        while X.dise[1] != front:
            X.R()
    elif X.dise[1] == front:
        while X.dise[0] != top:
            X.E()
    X.output()

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, nums):
        from collections import deque
        self.ns = deque([nums[4], nums[0], nums[1]])
        self.we = deque([nums[3], nums[0], nums[2]])
        self.bk = deque([nums[5]])

    def __str__(self):
        return str({"ns": self.ns, "we": self.we, "bk": self.bk})

    def operation(self, o):
        if o == "N":
            self.n()
        elif o == "S":
            self.s()
        elif o == "W":
            self.w()
        elif o == "E":
            self.e()
        else:
            raise BaseException("invalid", o)

    def n(self):
        self.ns.append(self.bk.popleft())
        self.bk.append(self.ns.popleft())
        self.we[1] = self.ns[1]

    def s(self):
        self.ns.appendleft(self.bk.pop())
        self.bk.appendleft(self.ns.pop())
        self.we[1] = self.ns[1]

    def w(self):
        self.we.append(self.bk.popleft())
        self.bk.append(self.we.popleft())
        self.ns[1] = self.we[1]

    def e(self):
        self.we.appendleft(self.bk.pop())
        self.bk.appendleft(self.we.pop())
        self.ns[1] = self.we[1]

    def rotate_right(self):
        tmp = self.ns
        tmp.reverse()
        self.ns = self.we
        self.we = tmp

    def fit_top_s(self, top, s):
        while self.get_top() != top:
            if top in self.ns:
                self.n()
            elif top in self.we:
                self.w()
            else:
                self.n()
                self.n()
        while self.get_s() != s:
            self.rotate_right()

    def get_top(self):
        return self.ns[1]

    def get_s(self):
        return self.ns[2]

    def get_e(self):
        return self.we[2]


def resolve():
    nums = [int(i) for i in input().split()]
    dc = Dice(nums)
    q = int(input())
    for _ in range(q):
        top, s = [int(i) for i in input().split()]
        dc.fit_top_s(top, s)
        print(dc.get_e())


resolve()

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, top, front, right, left, rear, bottom):
        self.top = top
        self.front = front
        self.right = right
        self.left = left
        self.rear = rear
        self.bottom = bottom
    def print(self):
        '''print top of dice'''
        print(self.top)
    def north(self):
        self.top, self.front, self.bottom, self.rear = \
            self.front, self.bottom, self.rear, self.top
    def south(self):
        self.top, self.front, self.bottom, self.rear = \
            self.rear, self.top, self.front, self.bottom
    def east(self):
        self.top, self.right, self.bottom, self.left = \
            self.left, self.top, self.right, self.bottom
    def west(self):
        self.top, self.right, self.bottom, self.left = \
            self.right, self.bottom, self.left, self.top
    def find_right(self, top, front):
        if top == self.top:
            pass
        elif top == self.front:
            self.north()
        elif top == self.rear:
            self.south()
        elif top == self.right:
            self.west()
        elif top == self.left:
            self.east()
        elif top == self.bottom:
            self.north()
            self.north()
        else:
            return -1
        if front == self.front:
            return self.right
        elif front == self.right:
            return self.rear
        elif front == self.rear:
            return self.left
        elif front == self.left:
            return self.front
        else:
            return -1

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

for i in range(q):
    top, front = map(int, input().split())
    right = dice.find_right(top, front)
    print(right)

Submission status: Accepted

Solution code:
class Dice():
    directions = "EEENEEENEEESEEESEEENEEEN"

    def __init__(self, lst):
        self.top = lst[0]
        self.front = lst[1]
        self.right = lst[2]
        self.left = lst[3]
        self.back = lst[4]
        self.bottom = lst[5]

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

    def fit_top_front(self, num_top, num_front):
        for direction in Dice.directions:
            if self.top == num_top and self.front == num_front:
                break
            self.rotate(direction)


def main():
    dice_nums = list(map(int, input().split()))
    dice = Dice(dice_nums)

    num_questions = int(input())

    for _ in range(num_questions):
        nums = list(map(int, input().split()))
        dice.fit_top_front(nums[0], nums[1])
        print(dice.right)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

do = [(2,3,5,4), (6,3,1,4), (2,6,5,1), (2,1,5,6), (1,3,6,4), (2,4,5,3)]

def check(top, front):
    top_index=dice.index(top)
    front_index=dice.index(front)+1
    tmp=do[top_index]
    tmp_index=tmp.index(front_index)
   # print(top_index,front_index,tmp,tmp_index) 
    i = 0
    if tmp_index == 3:
        i = tmp[0]
    else:
        i = tmp[tmp_index+1]
    print(dice[i-1])


for i in range(q):
    top,front=input().split()
    check(top,front)

Submission status: Accepted

Solution code:
class Dice:
    
    def __init__(self, nums):
        self.nums = list(nums)
        
    @property
    def top(self):
        return self.nums[0]
        
    def _shift(self, *pairs):
        for pair in pairs:
            num = self.nums[pair[0]]
            self.nums[pair[0]] = self.nums[pair[1]]
            self.nums[pair[1]] = num
            
    def go(self, opes:str):
        for ope in opes:
            if ope == "N":
                self.go_north()
            elif ope == "E":
                self.go_east()
            elif ope == "W":
                self.go_west()
            else:
                self.go_south()
                
    def go_top(self, num):
        i = self.nums.index(num)
        
        if i == 1:
            self.go_north()
        elif i == 2:
            self.go_west()
        elif i == 3:
            self.go_east()
        elif i == 4:
            self.go_south()
        elif i == 5:
            self.go_north()
            self.go_north()
            
    def go_ahead(self, num):
        i = self.nums.index(num)
        
        if i == 2:
            self.go_cw()
        elif i == 4:
            self.go_cw()
            self.go_cw()
        elif i == 3:
            self.go_ccw()
            
        
    def go_north(self):
        self._shift((0, 1), (1, 5), (5, 4))
        
    def go_east(self):
        self._shift((0, 3), (3, 5), (5, 2))
        
    def go_west(self):
        self._shift((0, 2), (2, 5), (5, 3))
        
    def go_south(self):
        self._shift((0, 4), (4, 5), (5, 1))
        
    def go_ccw(self):
        self._shift((3, 4), (4, 2), (2, 1))
        
    def go_cw(self):
        self._shift((3, 1), (1, 2), (2, 4))
        

dice = Dice(map(int, input().split()))

for _ in range(int(input())):
    top, ahead = map(int, input().split())
    dice.go_top(top)
    dice.go_ahead(ahead)
    print(dice.nums[2])

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self):
        self.nums = list(range(6))
        self.work = list(range(6))

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

    def rotate(self, cmd):
        for i in range(6):
            # deepcopy
            self.work[i] = self.nums[i]

        if cmd == 'N':
            self.set_nums(self.work[1], self.work[5], self.work[2], self.work[3], self.work[0], self.work[4])
        elif cmd == 'S':
            self.set_nums(self.work[4], self.work[0], self.work[2], self.work[3], self.work[5], self.work[1]),
        elif cmd == 'E':
            self.set_nums(self.work[3], self.work[1], self.work[0], self.work[5], self.work[4], self.work[2])
        else:
            self.set_nums(self.work[2], self.work[1], self.work[5], self.work[0], self.work[4], self.work[3])

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

    def right(self, top, front):
        # サイコロの面IDを返す
        if top == 1:
            return [3,5,2,4][front-2]
        elif top == 6:
            return [4,2,5,3][front-2]
        elif top == 2:
            # 0が返る場合は不正なサイコロ
            return [4,0,1,6,0,3][front-1]
        elif top == 5:
            # 0が返る場合は不正なサイコロ
            return [3,0,6,1,0,4][front-1]
        elif top == 3:
            # 0が返る場合は不正なサイコロ
            return [2,6,0,0,1,5][front-1]
        elif top == 4:
            # 0が返る場合は不正なサイコロ
            return [5,1,0,0,6,2][front-1]
        else:
            pass

nums = [int(arg) for arg in input().split()]
d = Dice()
d.set_nums(*nums)
for _ in range(int(input())):
    top, front = [int(arg) for arg in input().split()]
    right_id = d.right(nums.index(top)+1, nums.index(front)+1)
    print(nums[right_id-1])

Submission status: Accepted

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

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


arrinput = list(map(int, input().split()))
dirinput = 'WWWWSWWWWSWWWWSWWWWWSWWWWSSWWWW'
dicresult = []

dice = diceClass(arrinput)
for s in dirinput:
	dice.throw(s)
	if dicresult.count([[dice.t, dice.s], dice.e]) == 0:
		dicresult.append([[dice.t, dice.s], dice.e])

q = int(input())
for i in range(0, q):
	a, b = map(int, input().split())
	for j in dicresult:
		if a == j[0][0] and b == j[0][1]:
			print(j[1])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,d1,d2,d3,d4,d5,d6):
        self.dice = [d1,d2,d3,d4,d5,d6]
    def turn(self,dir):
        if dir == 'S':
            self.dice = [self.dice[4],self.dice[0],self.dice[2],self.dice[3],self.dice[5],self.dice[1]]
        if dir == 'N':
            self.dice = [self.dice[1],self.dice[5],self.dice[2],self.dice[3],self.dice[0],self.dice[4]]
        if dir == 'W':
            self.dice = [self.dice[2],self.dice[1],self.dice[5],self.dice[0],self.dice[4],self.dice[3]]
        if dir == 'E':
            self.dice = [self.dice[3],self.dice[1],self.dice[0],self.dice[5],self.dice[4],self.dice[2]]


def check(m):
    for b in range(4):
        m.turn('N')
        if m.dice[0] == a[0] and m.dice[1] == a[1]:
            return(m.dice[2])

dice_num = input().split()
q = int(input())
conditions = []
for a in range(q):
    temp = input().split()
    conditions.append(temp)

init_Dice = Dice(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])

kaitou = []
for a in conditions:
    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue

    init_Dice.turn('W')
    init_Dice.turn('N')
    init_Dice.turn('E')

    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue
    init_Dice.turn('E')
    if check(init_Dice):
        print(check(init_Dice))
        continue