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:
def f(d, D):
    i = 0
    while i < 6:
        if d == D[i]:
            return i
        else:
            i += 1

Q0 = [1,2,4,3,1]
Q1 = [0,3,5,2,0]
Q2 = [0,1,5,4,0]
ans = ''
D = list(map(int, input().split(' ')))
n = int(input())
i = 0
while i < n:
    d0, d1 = map(int, input().split(' '))
    L0 = f(d0, D)
    L1 = f(d1, D)
    if L0 == 0:
        q = 0
        while q < 4:
            if L1 == Q0[q]:
                ans += str(D[Q0[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 1:
        q = 0
        while q < 4:
            if L1 == Q1[q]:
                ans += str(D[Q1[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 2:
        q = 0
        while q < 4:
            if L1 == Q2[q]:
                ans += str(D[Q2[q + 1]]) + '\n'
                break
            q += 1
    elif L0 == 3:
        q = 0
        while q < 4:
            if L1 == Q2[4-q]:
                ans += str(D[Q2[4-q-1]]) + '\n'
                break
            q += 1
    elif L0 == 4:
        q = 0
        while q < 4:
            if L1 == Q1[4-q]:
                ans += str(D[Q1[4-q-1]]) + '\n'
                break
            q += 1
    elif L0 == 5:
        q = 0
        while q < 4:
            if L1 == Q0[4-q]:
                ans += str(D[Q0[4-q-1]]) + '\n'
                break
            q += 1
    i += 1
if ans != '':
    print(ans[:-1])

Submission status: Accepted

Solution code:
class Dice:
    """サイコロを操作するクラス"""
    def __init__(self, num):
        self.num = num
    def roll_north(self):
        tmp = self.num[:]
        self.num[0] = tmp[1]
        self.num[1] = tmp[5]
        self.num[4] = tmp[0]
        self.num[5] = tmp[4]
    def roll_south(self):
        tmp = self.num[:]
        self.num[0] = tmp[4]
        self.num[1] = tmp[0]
        self.num[4] = tmp[5]
        self.num[5] = tmp[1]
    def roll_east(self):
        tmp = self.num[:]
        self.num[0] = tmp[3]
        self.num[2] = tmp[0]
        self.num[3] = tmp[5]
        self.num[5] = tmp[2]
    def roll_west(self):
        tmp = self.num[:]
        self.num[0] = tmp[2]
        self.num[2] = tmp[5]
        self.num[3] = tmp[0]
        self.num[5] = tmp[3]
    def roll_right(self):
        tmp = self.num[:]
        self.num[1] = tmp[3]
        self.num[2] = tmp[1]
        self.num[3] = tmp[4]
        self.num[4] = tmp[2]
    def print_dice(self):
        print(self.num)
    def print_top(self):
        print(self.num[0])

num = list(map(int, input().split()))
dice = Dice(num)
for i in range(int(input())):
    top, front = map(int, input().split())
    while not(top == dice.num[0] or front == dice.num[1]):
        dice.roll_north()
    while top != dice.num[0]:
        dice.roll_east()
    while front != dice.num[1]:
        dice.roll_right()
    print(dice.num[2])

Submission status: Accepted

Solution code:
from enum import IntEnum


class Position(IntEnum):
    TOP = 0
    FORESIDE = 1
    RIGHTSIDE = 2
    BACKSIDE = 3
    LEFTSIDE = 4
    BOTTOM = 5


class Dice:
    def __init__(self, v1, v2, v3, v4, v5, v6):
        self.values = {v1: [v1, v2, v3, v5, v4, v6],
                       v2: [v2, v6, v3, v1, v4, v5],
                       v3: [v3, v6, v5, v1, v2, v4],
                       v4: [v4, v6, v2, v1, v5, v3],
                       v5: [v5, v6, v4, v1, v3, v2],
                       v6: [v6, v5, v3, v2, v4, v1]}
        self.set_top(v1)

    def set_top(self, v):
        if v not in self.values:
            raise ValueError()

        self.top = v
        self.status = self.values[v]

    def rotate(self):
        (self.status[Position.FORESIDE], self.status[Position.RIGHTSIDE],
         self.status[Position.BACKSIDE], self.status[Position.LEFTSIDE]) \
         = (self.status[Position.LEFTSIDE], self.status[Position.FORESIDE],
            self.status[Position.RIGHTSIDE], self.status[Position.BACKSIDE])

    def __getitem__(self, side):
        return self.status[side]


def run():
    sides = [int(v) for v in input().split()]
    dice = Dice(*sides)
    cnt = int(input())

    for i in range(cnt):
        v1, v2 = [int(v) for v in input().split()]
        dice.set_top(v1)
        for j in range(4):
            if v2 == dice[Position.FORESIDE]:
                break
            dice.rotate()
        else:
            raise ValueError()

        print('{}'.format(dice[Position.RIGHTSIDE]))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
d = input().split()
p = [(-1, 2, 4, 1, 3, -1), (3, -1, 0, 5, -1, 2), (1, 5, -1, -1, 0, 4),
(4, 0, -1, -1, 5, 1), (2, -1, 5, 0, -1, 3), (-1, 3, 1, 4, 2, -1)]
for _ in range(int(input())):
  t, f = [d.index(s) for s in input().split()]
  print(d[p[t][f]])

Submission status: Accepted

Solution code:
class Dice2():
    def front(self,up,front,dice):
        #index == 5
        if(up==dice[2] and front==dice[1])or(up==dice[1] and front==dice[3])or(up==dice[3] and front==dice[4])or(up==dice[4] and front==dice[2]):
            return dice[5]
        #index == 4
        elif(up==dice[0] and front==dice[2])or(up==dice[2] and front==dice[5])or(up==dice[5] and front==dice[3])or(up==dice[3] and front==dice[0]):
            return dice[4]
        #index == 3
        elif(front==dice[5] and up==dice[4])or(front==dice[4] and up==dice[0])or(front==dice[0] and up==dice[1])or(front==dice[1] and up==dice[5]):
            return dice[3]
        #index == 2
        elif(up==dice[5] and front==dice[4])or(up==dice[4] and front==dice[0])or(up==dice[0] and front==dice[1])or(up==dice[1] and front==dice[5]):
            return dice[2]
        #index == 1
        elif(front==dice[0] and up==dice[2])or(front==dice[2] and up==dice[5])or(front==dice[5] and up==dice[3])or(front==dice[3] and up==dice[0]):
            return dice[1]
        #index == 0
        else:
            return dice[0]

dice = [int(i) for i in input().split()]
p = int(input())
dc2 = Dice2()
ans = []
for i in range(p):
    up,front = map(int,input().split())
    ans.append(dc2.front(up,front,dice))
for i in range(p):
    print(ans[i])

Submission status: Accepted

Solution code:
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
 
import random
u,s,e,w,n,d=map(int,input().split())
dice=Dice(u,w,s,e,n,d)
q=int(input())
for i in range(q):
    a,b=map(int,input().split())
    while True:
        dice.rot(random.randint(0,3))
        if(dice.u==a and dice.s==b):
            print(dice.e)
            break

Submission status: Accepted

Solution code:
class Dice:
    TOP, SOUTH, EAST, WEST, NORTH, BOTTOM = 0, 1, 2, 3, 4, 5
    def __init__(self, d):
        self.dice = d
        self.top = d[0]
        self.south = d[1]
        self.east = d[2]
        self.west = d[3]
        self.north = d[4]
        self.bottom = d[5]

    def set_top_front(self, top, front):
        for i, n in enumerate(self.dice):
            if n == top:
                if i == Dice.TOP: pass
                elif i == Dice.SOUTH: self.move("N")
                elif i == Dice.EAST: self.move("W")
                elif i == Dice.WEST: self.move("E")
                elif i == Dice.NORTH: self.move("S")
                elif i == Dice.BOTTOM:
                    self.move("S")
                    self.move("S")

                break
        
        for i, n in enumerate(self.dice):
            if n == front:
                if i == Dice.TOP: print("error.")
                elif i == Dice.SOUTH: pass
                elif i == Dice.EAST: self.move("R")
                elif i == Dice.WEST: self.move("L")
                elif i == Dice.NORTH:
                    self.move("R")
                    self.move("R")
                elif i == Dice.BOTTOM: print("error.")

                break
            
        
    def move(self, d):
        if d == "N":
            self.move_north()
        elif d == "W":
            self.move_west()
        elif d == "S":
            self.move_south()
        elif d == "E":
            self.move_east()
        elif d == "R":
            self.move_right()
        elif d == "L":
            self.move_left()

        self.dice = [
            self.top,
            self.south,
            self.east,
            self.west,
            self.north,
            self.bottom,
        ]

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

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

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

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

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

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

if __name__ == "__main__":
    import re

    nums = [int(n) for n in re.split(r"\s+", input().strip())]
    dice = Dice(nums)

    n = int(input().strip())
    for i in range(n):
        top, front = [int(n) for n in re.split(r"\s+", input().strip())]
        dice.set_top_front(top, front)

        print(dice.east)

Submission status: Accepted

Solution code:
def north(list):
    List = []
    List.append(list[1])
    List.append(list[5])
    List.append(list[2])
    List.append(list[3])
    List.append(list[0])
    List.append(list[4])
    return List

def west(list):
    List = []
    List.append(list[2])
    List.append(list[1])
    List.append(list[5])
    List.append(list[0])
    List.append(list[4])
    List.append(list[3])
    return List
    
def south(list):
    List = []
    List.append(list[4])
    List.append(list[0])
    List.append(list[2])
    List.append(list[3])
    List.append(list[5])
    List.append(list[1])
    return List

def east(list):
    List = []
    List.append(list[3])
    List.append(list[1])
    List.append(list[0])
    List.append(list[5])
    List.append(list[4])
    List.append(list[2])
    return List
dice = list(map(int, input().split()))
D = [dice]
for i in range(0, 3):
    D.append(north(D[i]))
D.append(south(west(D[0])))
D.append(south(east(D[0])))
for i in range(6):
    for j in range(3):
        if j == 0:
            D.append(east(D[i + j]))
        else:
            D.append(east(D[5 + 3 * i + j]))

x = int(input())
for i in range(x):
    a, b = map(int, input().split())
    for i in range(len(D)):
        if a == D[i][0] and b == D[i][1]:
            print(D[i][2])
            break

Submission status: Accepted

Solution code:
#! python3
# dice_i.py

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

    def rotate(self, ope):
        if ope == 'S':
            self.top, self.north, self.bottom, self.south = self.north, self.bottom, self.south, self.top
        elif ope == 'N':
            self.top, self.south, self.bottom, self.north = self.south, self.bottom, self.north, self.top
        elif ope == 'E':
            self.top, self.west, self.bottom, self.east = self.west, self.bottom, self.east, self.top
        elif ope == 'W':
            self.top, self.east, self.bottom, self.west = self.east, self.bottom, self.west, self.top
        elif ope == 'R': # clockwise
            self.south, self.east, self.north, self.west = self.east, self.north, self.west, self.south
        elif ope == 'L': # reversed clockwise
            self.south, self.east, self.north, self.west = self.west, self.north, self.east, self.south

dc = dice([int(x) for x in input().split(' ')])
q = int(input())
top_souths = [[int(x) for x in input().split(' ')] for i in range(q)]

for top, south in top_souths:
    if dc.top != top:
        opes = ['S', 'E', 'S', 'E', 'S']
        for op in opes:
            dc.rotate(op)
            if dc.top == top: break
    for i in range(4):
        if dc.south == south:
            break
        dc.rotate('R')
    print(dc.east)

Submission status: Accepted

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

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

for n in range(int(input())):
    top, foward = map(int, input().split())
    south_cnt = 0

    while True:
        #print(dice.labels[0], dice.labels[1], dice.labels[2], dice.labels[4], dice.labels[3])
        if foward == dice.labels[1] and top == dice.labels[0]:
            break
        if foward == dice.labels[1]:
            dice.east()
        elif top == dice.labels[0]:
            dice.south()
            dice.east()
            dice.north()
        else:
            if south_cnt == 3:
                dice.east()
            else:
                dice.south()
                south_cnt += 1
    print(dice.labels[2])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


class Dice:
    def __init__(self, n):
        self.upper    = n[0]
        self.backward = n[1]
        self.right    = n[2]
        self.left     = n[3]
        self.ahead    = n[4]
        self.bottom   = n[5]

    def roll_north(self):
        self.upper, self.ahead, self.bottom, self.backward = self.backward, self.upper, self.ahead, self.bottom

    def roll_south(self):
        self.upper, self.ahead, self.bottom, self.backward = self.ahead, self.bottom, self.backward, self.upper

    def roll_east(self):
        self.upper, self.right, self.bottom, self.left = self.left, self.upper, self.right, self.bottom

    def roll_west(self):
        self.upper, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.upper

    def roll_side(self):
        self.ahead, self.right, self.backward, self.left = self.left, self.ahead, self.right, self.backward


dice_info = Dice(list(map(int, input().split())))
q = int(input())

for i in range(q):
    upper, ahead = map(int, input().split())

    if upper is dice_info.left:
        dice_info.roll_east()
        
    elif upper is dice_info.right:
        dice_info.roll_west()

    while not (upper == dice_info.upper or ahead == dice_info.ahead):
        dice_info.roll_north()

    while upper != dice_info.upper:
        dice_info.roll_east()

    while ahead != dice_info.ahead:
        dice_info.roll_side()

    print(dice_info.left)

Submission status: Accepted

Solution code:
class Saikoro:

    def sai(self,one,two,three,four,five,six):
        self.s1=int(one)
        self.s2=int(two)
        self.s3=int(three)
        self.s4=int(four)
        self.s5=int(five)
        self.s6=int(six)

    def turnE(self):
        e=[self.s4,self.s2,self.s1,self.s6,self.s5,self.s3]
        self.s1=e[0]
        self.s2=e[1]
        self.s3=e[2]
        self.s4=e[3]
        self.s5=e[4]
        self.s6=e[5]

    def turnN(self):
        n=[self.s2,self.s6,self.s3,self.s4,self.s1,self.s5]
        self.s1=n[0]
        self.s2=n[1]
        self.s3=n[2]
        self.s4=n[3]
        self.s5=n[4]
        self.s6=n[5]


l=input().split()

sai1=Saikoro()
sai1.sai(l[0],l[1],l[2],l[3],l[4],l[5])

q=int(input())

i=0
Ans=[]
while i<q:
    m=input().split()
    ichi=int(m[0])
    ni=int(m[1])
    sai1.sai(l[0],l[1],l[2],l[3],l[4],l[5])
    p=0
    while p<4:
        if ichi==int(l[0]) and ni==int(l[2]) or ichi==int(l[2]) and ni==int(l[5]) or ichi==int(l[5]) and ni==int(l[3]) or ichi==int(l[3]) and ni==int(l[0]) :
            Ans.append(int(l[4]))
            break
        elif ichi==int(l[5]) and ni==int(l[2]) or ichi==int(l[2]) and ni==int(l[0]) or ichi==int(l[0]) and ni==int(l[3]) or ichi==int(l[3]) and ni==int(l[5]) :
            Ans.append(int(l[1]))
            break
        w=0
        sai1.turnE()
        while w<4:
            sai1.turnN()
            if sai1.s1==ichi and sai1.s2==ni:
                Ans.append(sai1.s3)
                break
            else:
                pass
            w+=1
        p+=1
    i+=1

for A in Ans:
    print(A)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.t = t
        self.f = f
        self.r = r
        self.l = l
        self.b = b
        self.u = u
        self.a=[t,f,r,l,b,u]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d):
        self.a=[self.a[i] for i in self.direction[d]]
        self.t = self.a[0]
        self.f = self.a[1]
        self.r = self.a[2]
        self.l = self.a[3]
        self.b = self.a[4]
        self.u = self.a[5]
t,f,r,l,b,u=map(int,input().split())
dice=Dice(t,f,r,l,b,u)
n=int(input())
s='SSSEWW'
yw='YYY'
for j in range(n):
    t,f=map(int,input().split())
    for d in s:
        if dice.t==t:
            break
        dice.roll(d)
    for t in yw:
        if dice.f==f:
            break
        dice.roll(t)
    print(dice.r)

Submission status: Accepted

Solution code:
# AOJ ITP1_11_B

# サイコロ。

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

class dice:
    def __init__(self):
        self.data = [0 for i in range(8)]
        a = intinput()
        for i in range(1, 7): self.data[i] = a[i - 1]  # 1, 2, 3, 4, 5, 6.
        for i in range(6): self.data[0] += a[i]  # 各位の和を0に。
        self.data[7] = a[0] * a[5] + a[1] * a[4] + a[2] * a[3]  # 対面同士の積の和。
        # dataの1, 2, 3, 4, 5, 6というindexが位置を示す。
        # 回転するとそれらの位置にある数が変化するイメージ。

    def d_read(self, a):
        # aはたとえば5, 1, 2, 6のような配列
        b = []
        for i in range(len(a)): b.append(self.data[a[i]])
        return b

    def d_write(self, b, c):
        # cはたとえば1, 2 ,6, 5のような配列で、そこにbの中身を落とす。
        if len(b) != len(c): return     # 要求と異なる。
        for i in range(len(b)): self.data[c[i]] = b[i]

    def roll(self, direction):
        # directionが'S', 'E', 'W', 'N'.
        # 'S', 'N'では3と4が不変。 'E'と'W'では2と5が不変。
        if direction == ' ': return
        if direction == 'S':
            self.d_write(self.d_read([5, 1, 2, 6]), [1, 2, 6, 5])
            
        elif direction == 'N':
            self.d_write(self.d_read([2, 6, 5, 1]), [1, 2, 6, 5])
            
        elif direction == 'W':
            self.d_write(self.d_read([3, 6, 4, 1]), [1, 3, 6, 4])
            
        elif direction == 'E':
            self.d_write(self.d_read([4, 1, 3, 6]), [1, 3, 6, 4])

    def get_label(self, i):  # i番の面の数を返す。
        return self.data[i]

    def get_index(self, n):  # nが書かれた面のindexを返す。
        for i in range(1, 7):
            if self.data[i] == n: return i
        return 0  # 見つからない場合。

def main():
    d = dice()
    q = int(input())
    command = ["", ' ', 'N', 'W', 'E', 'S', 'S']
    get_ans = [-1, -1, 3, 5, 2, 4]
    for i in range(q):
        x = intinput()
        a = x[0]; b = x[1]
        # aが上面、bが前面。答えるのは右側面。aとbは整数(indexではない).
        upper = d.get_index(a)
        d.roll(command[upper])  # aが上面に来るように回転。
        if upper == 6: d.roll('S')  # 6の場合はもう一回。
        front = d.get_index(b)   # 前面にくる数のindex.
        print(d.get_label(get_ans[front]))  # 答えを出力。

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.side = {"top": 0, "front": 0, "right": 0, "left": 0, "back": 0, "bottom": 0}

    # サイコロを東西南北、どちらか一方に転がした時、それぞれの面の変化
    def roll(self, direction):
        self.direction = direction
        if self.direction == "N":
            w = self.side["top"]
            self.side["top"] = self.side["front"]
            self.side["front"] = self.side["bottom"]
            self.side["bottom"] = self.side["back"]
            self.side["back"] = w
        elif self.direction == "S":
            w = self.side["top"]
            self.side["top"] = self.side["back"]
            self.side["back"] = self.side["bottom"]
            self.side["bottom"] = self.side["front"]
            self.side["front"] = w
        elif self.direction == "E":
            w = self.side["top"]
            self.side["top"] = self.side["left"]
            self.side["left"] = self.side["bottom"]
            self.side["bottom"] = self.side["right"]
            self.side["right"] = w
        elif self.direction == "W":
            w = self.side["top"]
            self.side["top"] = self.side["right"]
            self.side["right"] = self.side["bottom"]
            self.side["bottom"] = self.side["left"]
            self.side["left"] = w

    # サイコロの面の位置関係
    def position_relation(self, top, front):
        # 北に転がした場合
        if top == "top" and front == "front"\
        or top == "front" and front == "bottom"\
        or top == "bottom" and front == "back"\
        or top == "back" and front == "top":
            self.right = self.side["right"]
        # 北に転がした時の"top"と"front"を入れ替えた場合 
        if top == "top" and front == "back"\
        or top == "back" and front == "bottom"\
        or top == "bottom" and front == "front"\
        or top == "front" and front == "top":
            self.right = self.side["left"]
        # 東に転がした場合
        if top == "top" and front == "right"\
        or top == "right" and front == "bottom"\
        or top == "bottom" and front == "left"\
        or top == "left" and front == "top":
            self.right = self.side["back"]
        # 東に転がした時の"top"と"front"を入れ替えた場合
        if top == "top" and front == "left"\
        or top == "left" and front == "bottom"\
        or top == "bottom" and front == "right"\
        or top == "right" and front == "top":
            self.right = self.side["front"]
        # 西に転がした場合
        if top == "front" and front == "right"\
        or top == "right" and front == "back"\
        or top == "back" and front == "left"\
        or top == "left" and front == "front":
            self.right = self.side["top"]
        # 西に転がした時の"top"と"front"を入れ替えた場合
        if top == "front" and front == "left"\
        or top == "left" and front == "back"\
        or top == "back" and front == "right"\
        or top == "right" and front == "front":
            self.right = self.side["bottom"]

dice = Dice()
for s, n in zip(dice.side, input().split()):
    dice.side[s] = int(n)

q = int(input())
for i in range(q):
    t, f = map(int, input().split())
    for k, v in dice.side.items():
        if t == v:
            top = k
        if f == v:
            front = k
    dice.position_relation(top, front)
    print(dice.right)

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self, numbers):
        self.top = numbers[0]
        self.s = numbers[1]
        self.e = numbers[2]
        self.w = numbers[3]
        self.n = numbers[4]
        self.bot = numbers[5]
    def rot(self, dir):
        top, s, e, w, n, bot = (self.top, self.s, self.e, self.w, self.n, self.bot)
        if dir == 'N':
            self.top = s
            self.s = bot
            self.e = e
            self.w = w
            self.n = top
            self.bot = n
        elif dir == 'S':
            self.top = n
            self.s = top
            self.e = e
            self.w = w
            self.n = bot
            self.bot = s
        elif dir == 'E':
            self.top = w
            self.s = s
            self.e = top
            self.w = bot
            self.n = n
            self.bot = e
        elif dir == 'W':
            self.top = e
            self.s = s
            self.e = bot
            self.w = top
            self.n = n
            self.bot = w
    def getTop(self):
        return self.top
    def rotMany(self, command):
        for dir in command:
            self.rot(dir)



li = list(map(int, input().split()))
D = Dice(li)
n = int(input())
for _ in range(n):
    t, s = map(int, input().split())
    for cmd in ["N","N","N","E","EE",""]:
        if D.top == t:
            break
        else:
            D.rotMany(cmd)
    for cmd in ["SEN", "SEN", "SEN", ""]:
        if D.s == s:
            break
        else:
            D.rotMany(cmd)
    print(D.e)

Submission status: Accepted

Solution code:
class Dice:
     
    def __init__(self,ls):
        self.a = ls[1-1] # top
        self.b = ls[2-1] # front
        self.c = ls[3-1] # right
        self.d = ls[4-1] # left
        self.e = ls[5-1] # back
        self.f = ls[6-1] # bottom
     
    def E(self):
        self.a, self.c, self.f, self.d = self.d, self.a, self.c, self.f
    def W(self):
        self.a, self.c, self.f, self.d = self.c, self.f, self.d, self.a
    def S(self):
        self.a, self.b, self.f, self.e = self.e, self.a, self.b, self.f
    def N(self):
        self.a, self.b, self.f, self.e = self.b, self.f, self.e, self.a   
                         
import random
ls = map(int,raw_input().split())
q = int(raw_input())
for i in range(q):
    dice = Dice(ls)
    a,b = map(int,raw_input().split())
    while dice.a != a or dice.b != b:
        i = random.choice([1,2,3,4])
        if i == 1: dice.E()
        elif i == 2: dice.W()
        elif i == 3 : dice.S()
        elif i == 4 : dice.N()
    print dice.c

Submission status: Accepted

Solution code:
class Dice(object):

    def __init__(self, n):
        self.n = n
        self.pos = ['12431', '03520', '01540', '04510', '02530', '13421']

    def face(self, x1, x2):
        s = '{0}{1}'.format(self.n.index(x1),self.n.index(x2))
        for i, p in enumerate(self.pos):
            if s in p:
                return self.n[i]
dice = Dice([int(i) for i in input().split()])
n = int(input())
for _ in range(n):
    x1, x2 = map(int, input().split())
    print(dice.face(x1, x2))

Submission status: Accepted

Solution code:

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

	def roll(self, drct):
		t_ = self.t
		s_ = self.s
		e_ = self.e
		w_ = self.w
		n_ = self.n
		b_ = self.b
		if drct == 'S':
			self.t = n_
			self.s = t_
			self.n = b_
			self.b = s_
		elif drct == 'N':
			self.t = s_
			self.s = b_
			self.n = t_
			self.b = n_
		elif drct == 'E':
			self.t = w_
			self.e = t_
			self.w = b_
			self.b = e_
		elif drct == 'W':
			self.t = e_
			self.e = b_
			self.w = t_
			self.b = w_
		else:
			return
	
	def set_top(self, face):
		if self.t == face:
			return
		elif self.s == face:
			self.roll('N')
		elif self.e == face:
			self.roll('W')
		elif self.w == face:
			self.roll('E')
		elif self.n == face:
			self.roll('S')
		else:
			self.roll('N')
			self.roll('N')
			
	def get_right_face(self, f):
		if self.s == f:
			return self.e
		elif self.e == f:
			return self.n
		elif self.w == f:
			return self.s
		else:
			return self.w
	
faces = list(map(int, input().split()))
dice = Dice(faces)

n = int(input())
for i in range(n):
	t, f = map(int, input().split())
	dice.set_top(t)
	print(dice.get_right_face(f))

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, s1, s2, s3, s4, s5, s6):
        self.s1 = s1 #このクラスが持つ「s1」変数に引数を格納 以下も同様
        self.s2 = s2
        self.s3 = s3
        self.s4 = s4
        self.s5 = s5
        self.s6 = s6
    def east(self):
        prev_s6 = self.s6
        self.s6 = self.s3
        self.s3 = self.s1
        self.s1 = self.s4
        self.s4 = prev_s6
    def west(self):
        prev_s6 = self.s6
        self.s6 = self.s4
        self.s4 = self.s1
        self.s1 = self.s3
        self.s3 = prev_s6
    def north(self):
        prev_s6 = self.s6
        self.s6 = self.s5
        self.s5 = self.s1
        self.s1 = self.s2
        self.s2 = prev_s6
    def south(self):
        prev_s6 = self.s6
        self.s6 = self.s2
        self.s2 = self.s1
        self.s1 = self.s5
        self.s5 = prev_s6
    def rotate(self):
        prev_s2 = self.s2
        self.s2 = self.s4
        self.s4 = self.s5
        self.s5 = self.s3
        self.s3 = prev_s2
    def top(self):
        return self.s1
    def front(self):
        return self.s2
    def right(self):
        return self.s3

s1, s2, s3, s4, s5, s6 = map(int, input().split())
dice = Dice(s1, s2, s3, s4, s5, s6)
q = int(input())
for i in range(q):
    t, f = map(int, input().split())
    flag = False
    for j in range(6):
        if j % 2 == 0:
            dice.north()
        else:
            dice.west()
        
        for k in range(4):
            dice.rotate()
            if dice.top() == t and dice.front() == f:
                flag = True
                break
        if flag:
            break
    print(dice.right())