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:
inp = [i for i in input().split()]
n = int(input())
array = [[i for i in input().split()] for i in range(n)]
for i2 in range(n):
    fuck =""
    for i in range(1,33):
        if (i<=20 and i%5==0) or i==22 or i==27 or i==28:
            s = "N"
        else:
            s = "E"
        if s == "E":
            a = []
            a.append(inp[3])
            a.append(inp[1])
            a.append(inp[0])
            a.append(inp[5])
            a.append(inp[4])
            a.append(inp[2])
            inp = a
            if inp[0] == array[i2][0] and inp[1] == array[i2][1] and inp[2] != fuck:
                print(inp[2])
                fuck = inp[2]
        elif s == "N":
            a = []
            a.append(inp[1])
            a.append(inp[5])
            a.append(inp[2])
            a.append(inp[3])
            a.append(inp[0])
            a.append(inp[4])
            inp = a
            if inp[0] == array[i2][0] and inp[1] == array[i2][1] and inp[2] != fuck:
                print(inp[2])
                fuck = inp[2]

Submission status: Accepted

Solution code:
import random
class Dice:
    def __init__(self, usr_input):
        self.eyes = list(map(int, usr_input.split()))
        self.pos = {label:eye for label, eye in zip(range(1, 7), self.eyes)}
        #self.com = list(command)

    def roll(self,c):
        self.eyes = [n for n in self.pos.values()]
        if c == 'E':
            self.pos[1] = self.eyes[3]
            self.pos[3] = self.eyes[0]
            self.pos[4] = self.eyes[5]
            self.pos[6] = self.eyes[2]
        elif c == 'N':
            self.pos[1] = self.eyes[1]
            self.pos[2] = self.eyes[5]
            self.pos[5] = self.eyes[0]
            self.pos[6] = self.eyes[4]
        elif c == 'S':
            self.pos[1] = self.eyes[4]
            self.pos[2] = self.eyes[0]
            self.pos[5] = self.eyes[5]
            self.pos[6] = self.eyes[1]
        elif c == 'W':
            self.pos[1] = self.eyes[2]
            self.pos[3] = self.eyes[5]
            self.pos[4] = self.eyes[0]
            self.pos[6] = self.eyes[3]

    def right_print(self, top, front):
        while self.pos[1] != top or self.pos[2] != front:
            c = random.choice(['N', 'E', 'W', 'S'])
            self.roll(c)

        print(self.pos.get(3))

if __name__ == '__main__':
    dice1 = Dice(input())
    #dice1.roll()
    num = int(input())
    for i in range(num):
        question = list(map(int, input().split()))
        dice1.right_print(question[0], question[1])

Submission status: Accepted

Solution code:
import copy
class Dice2:
    def __init__(self, nums):
        self.nums = nums
        self.dic = \
                {(1,2):3, (1,3):5, (1,4):2, (1,5):4, (2,3):1, (2,4):6, (2,6):3, (3,5):1, (3,6):5, (4,5):6, (4,6):2, (5,6):4}
    def output_right(self, x):
        x[0] = self.nums.index(x[0]) + 1
        x[1] = self.nums.index(x[1]) + 1
        nums = self.nums
        y = copy.deepcopy(x)
        x.sort()
        key = tuple(x)
        if tuple(y)==key:
            return nums[self.dic[key]-1]
        else:
            return nums[6-self.dic[key]]

dice = Dice2(list(map(int, input().split())))
q = int(input())
for i in range(q):
    print(dice.output_right(list(map(int, input().split()))))

Submission status: Accepted

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

import sys
import os
import math


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

def dice_roll(c):
    copy = dice[:]
    if c == 'E':
        dice[2] = copy[0]
        dice[5] = copy[2]
        dice[3] = copy[5]
        dice[0] = copy[3]
    elif c == 'N':
        dice[0] = copy[1]
        dice[4] = copy[0]
        dice[5] = copy[4]
        dice[1] = copy[5]
        pass
    elif c == 'S':
        dice[1] = copy[0]
        dice[0] = copy[4]
        dice[4] = copy[5]
        dice[5] = copy[1]
    elif c == 'W':
        dice[0] = copy[2]
        dice[2] = copy[5]
        dice[5] = copy[3]
        dice[3] = copy[0]
    # Y???????????¢
    elif c == 'R':
        dice[2] = copy[1]
        dice[4] = copy[2]
        dice[3] = copy[4]
        dice[1] = copy[3]

for i in range(q):
    up, front = list(map(int, input().split()))

    # up???????????????
    num = 0
    while dice[0] != up:
        if num % 2 == 0:
            dice_roll('E')
        else:
            dice_roll('N')
        num += 1

    # front???????????????
    while dice[1] != front:
        dice_roll('R')

    print(dice[2])

Submission status: Accepted

Solution code:
class dice_2:
    def __init__(self,label):
        self.spots = label

    def result(self,top,front):
        if self.spots.index(top)+1 == 1:
            self.order=[2,3,5,4,2]
        elif self.spots.index(top)+1 == 2:
            self.order=[6,3,1,4,6]
        elif self.spots.index(top)+1 == 3:
            self.order=[2,6,5,1,2]
        elif self.spots.index(top)+1 == 4:
            self.order=[2,1,5,6,2]
        elif self.spots.index(top)+1 == 5:
            self.order=[1,3,6,4,1]
        elif self.spots.index(top)+1 == 6:
            self.order=[2,4,5,3,2]

        for i in range(4):
            if self.order[i] == self.spots.index(front)+1:
                self.right = self.order[i+1]

    def output(self):
        print(self.spots[self.right-1])

label = list(map(int,input().split()))
dice = dice_2(label)
for i in range(int(input())):
    top,front = map(int,(input().split()))
    dice.result(top,front)
    dice.output()

Submission status: Accepted

Solution code:
class Dice:
    """????????????????????????"""
    def __init__(self, s):
        self.s1 = s[0]
        self.s2 = s[1]
        self.s3 = s[2]
        self.s4 = s[3]
        self.s5 = s[4]
        self.s6 = s[5]

    def roll(self, direction):          # ???????????????????§??????¢????????¢
        temp = self.s1
        if direction == "E":
            self.s1 = self.s4
            self.s4 = self.s6
            self.s6 = self.s3
            self.s3 = temp
        elif direction == "N":
            self.s1 = self.s2
            self.s2 = self.s6
            self.s6 = self.s5
            self.s5 = temp
        elif direction == "S":
            self.s1 = self.s5
            self.s5 = self.s6
            self.s6 = self.s2
            self.s2 = temp
        elif direction == "W":
            self.s1 = self.s3
            self.s3 = self.s6
            self.s6 = self.s4
            self.s4 = temp

    def setposition(self, a, b):
        while 1:
            if self.s1 == a:
                break
            self.roll("N")
            if self.s1 == a:
                break
            self.roll("E")
        while 1:
            if self.s2 == b:
                break
            self.roll("N")
            self.roll("E")
            self.roll("S")


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

for _ in range(q):
    a, b = map(int, input().split())
    dice1.setposition(a, b)
    print(dice1.s3)

Submission status: Accepted

Solution code:
#ITP1_11-B Dice 2
rep=[
    [1,2,4,3,1],
    [2,0,3,5,2],
    [0,1,5,4,0],
    [0,4,5,1,0],
    [0,2,5,3,0],
    [1,3,4,2,1]
]
d = input().split(" ")
q = int(input())
dic={}
for i in range(6):
    dic.update({d[i]:i})
for i in range(q):
    a,b = input().split(" ")
    for j in range(4):
        top=dic[a]
        front = dic[b]
        if(d[rep[top][j]]==d[front]):
            print(d[rep[top][j+1]])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.up = 1
        self.under = 6
        self.N = 5
        self.W = 4
        self.E = 3
        self.S = 2

    def roll(self, d):#d => direction
        if d == "N":
            tmp = self.up
            self.up = self.S
            self.S = self.under
            self.under = self.N
            self.N = tmp
        elif d == "W":
            tmp = self.up
            self.up = self.E
            self.E = self.under
            self.under = self.W
            self.W = tmp
        elif d == "E":
            tmp = self.up
            self.up = self.W
            self.W = self.under
            self.under = self.E
            self.E = tmp
        elif d == "S":
            tmp = self.up
            self.up = self.N
            self.N = self.under
            self.under = self.S
            self.S = tmp

    def getUpward(self):
        return self.up

    def setRoll(self,up,under,N,W,E,S):
        self.up = up
        self.under = under
        self.N = N
        self.W = W
        self.E = E
        self.S = S

    def rotate(self):#crockwise
        tmp = self.S
        self.S = self.E
        self.E = self.N
        self.N = self.W
        self.W = tmp

    def getE(self, up, S):
        '''
        
        :param up:num 
        :param S: num
        :return: num
        '''
        count = 0
        while True:
            if self.up == up:
                break
            if count != 3:
                self.roll("N")
                count += 1
            else:
                self.roll("E")
        while True:
            if self.S == S:
                break
            self.rotate()
        return self.E



myd = Dice()
myd.up, myd.S, myd.E, myd.W, myd.N, myd.under = map(int,input().split())
n = int(input())
for i in range(n):
    up, S = map(int,input().split())
    print(myd.getE(up,S))

Submission status: Accepted

Solution code:
def main():
    labels = list(map(int, input().split(' ')))

    n = int(input())

    for i in range(n):
        dice = Dice(labels)
        t, f = map(int, input().split(' '))

        for j in range(8):
            if j%4 == 0:
                dice.toE()

            if f == dice.front:
                break

            dice.toN()

        for j in range(4):
            if t == dice.top:
                break
            dice.toE()

        print(dice.right)

class Dice:
    def __init__(self, labels):
        self.labels = labels

        self._tb = (0, 5)
        self._fb = (1, 4)
        self._lr = (3, 2)

    def toN(self):
        tb = self._tb
        self._tb = self._fb
        self._fb = tuple(reversed(tb))

        return self

    def toS(self):
        tb = self._tb
        self._tb = tuple(reversed(self._fb))
        self._fb = tb

        return self

    def toW(self):
        tb = self._tb
        self._tb = tuple(reversed(self._lr))
        self._lr = tb

        return self

    def toE(self):
        tb = self._tb
        self._tb = self._lr
        self._lr = tuple(reversed(tb))

        return self

    def get_top(self):
        return self.labels[self._tb[0]]

    def get_front(self):
        return self.labels[self._fb[0]]

    def get_right(self):
        return self.labels[self._lr[1]]

    top   = property(get_top)
    front = property(get_front)
    right = property(get_right)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def roll(inst):
    global u, s, e, w, n, d
    if inst == 'N':
        u, s, n, d = s, d, u, n
    elif inst == 'E':
        u, e, w, d = w, u, d, e
    elif inst == 'S':
        u, s, n, d = n, u, d, s
    elif inst == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = map(int, input().split())
q = int(input())
for i in range(q):
    u1, s1 = map(int, input().split())
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,t,f,r,l,ba,bo):
        self.t = t
        self.f = f
        self.r = r
        self.l = l
        self.ba = ba
        self.bo = bo

s = input().split(" ")
dice = Dice(s[0],s[1],s[2],s[3],s[4],s[5])
n = int(input())
for i in range(n):
    t = input().split(" ")
    if t[0] == dice.t:
        if t[1] == dice.f:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.ba)
        elif t[1] == dice.ba:
            print(dice.l)
        else:
            print(dice.f)
    elif t[0] == dice.f:
        if t[1] == dice.bo:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.t)
        elif t[1] == dice.t:
            print(dice.l)
        else:
            print(dice.bo)
    elif t[0] == dice.r:
        if t[1] == dice.bo:
            print(dice.ba)
        elif t[1] == dice.t:
            print(dice.f)
        elif t[1] == dice.f:
            print(dice.bo)
        else:
            print(dice.t)
    elif t[0] == dice.l:
        if t[1] == dice.bo:
            print(dice.f)
        elif t[1] == dice.t:
            print(dice.ba)
        elif t[1] == dice.f:
            print(dice.t)
        else:
            print(dice.bo)
    elif t[0] == dice.ba:
        if t[1] == dice.bo:
            print(dice.l)
        elif t[1] == dice.t:
            print(dice.r)
        elif t[1] == dice.r:
            print(dice.bo)
        else:
            print(dice.t)
    elif t[0] == dice.bo:
        if t[1] == dice.f:
            print(dice.l)
        elif t[1] == dice.l:
            print(dice.ba)
        elif t[1] == dice.r:
            print(dice.f)
        else:
            print(dice.r)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.face = {12:3, 13:5, 14:2, 15:4, 21:4, 23:1, 24:6, 26:3, 31:2, 32:6, 35:1, 36:5, 41:5, 42:1, 45:6, 46:2, 51:3, 53:6, 54:1, 56:4, 62:4, 63:2, 64:5, 65:3}
    
    def right_face(self, face):
        return self.face[face]

d = Dice()
dice = [dice for dice in input().split()]
q = int(input())
for i in range(q):
    face_up, face_front = input().split()
    face_up = dice.index(face_up)
    face_front = dice.index(face_front)
    face = str(face_up+1) + str(face_front+1)
    face = int(face)
    n = d.right_face(face)
    print(dice[n-1])

Submission status: Accepted

Solution code:
def roll(i):
    global u, s, e, w, n, d
    if i == 'N':
        u, s, n, d = s, d, u, n
    elif i == 'E':
        u, e, w, d = w, u, d, e
    elif i == 'S':
        u, s, n, d = n, u, d, s
    elif i == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = input().split()
q = int(input())
for i in range(q):
    u1, s1 = input().split()
    if u1 == u:
        pass
    elif u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Submission status: Accepted

Solution code:
def roll(i):
    global u, s, e, w, n, d
    if i == 'N':
        u, s, n, d = s, d, u, n
    elif i == 'E':
        u, e, w, d = w, u, d, e
    elif i == 'S':
        u, s, n, d = n, u, d, s
    elif i == 'W':
        u, e, w, d = e, d, u, w

u, s, e, w, n, d = input().split()
q = int(input())
for i in range(q):
    u1, s1 = input().split()
    if u1 == s:
        roll('N')
    elif u1 == e:
        roll('W')
    elif u1 == w:
        roll('E')
    elif u1 == n:
        roll('S')
    elif u1 == d:
        roll('N')
        roll('N')
    if s1 == s:
        print(e)
    elif s1 == e:
        print(n)
    elif s1 == n:
        print(w)
    elif s1 == w:
        print(s)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.front = '1'
        self.back = '6'
        self.top = '5'
        self.bottom = '2'
        self. right = '3'
        self.left = '4'
    def turnRight(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self.top
    def turnLeft(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self.top
    def turnFront(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self.top
    def turnBack(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self.top
 
mydice = Dice()
mydice.top, mydice.front, mydice.right, mydice.left, mydice.back, mydice.bottom = input().split()
 
n = int(input())
for q in range(n):
    top, front = input().split()
 
    for rotate in range(3):
        mydice.turnFront()
        if mydice.top == front:
            break
        mydice.turnRight()
        if mydice.top == front:
            break
    mydice.turnFront()
    while top != mydice.top:
        mydice.turnRight()
    print(mydice.right)

Submission status: Accepted

Solution code:
x=[int(i)for i in input().split()]
y=[[int(0)for i in range(4)]for i in range(3)]
y[0][1]=x[0]
y[1][1]=x[1]
y[1][2]=x[2]
y[1][0]=x[3]
y[1][3]=x[4]
y[2][1]=x[5]
m=int(input())
for i in range(m):
    r,l= [int(i) for i in input().split()]
    for j in range(3):
        if y[0][1]==r:
            break
        y[0][1],y[1][1]=y[1][1],y[0][1]
        y[1][1],y[2][1]=y[2][1],y[1][1]
        y[2][1],y[1][3]=y[1][3],y[2][1]
        
    if y[0][1]==r:
        for j in range(4):
            if y[1][1]==l:
                break
            y[1][1],y[1][2]=y[1][2],y[1][1]
            y[1][2],y[1][3]=y[1][3],y[1][2]
            y[1][3],y[1][0]=y[1][0],y[1][3]
            if y[1][1]==l:
                break
    else:
        for j in range(4):
            if y[0][1]==r:
                break
            y[0][1],y[1][2]=y[1][2],y[0][1]
            y[1][2],y[2][1]=y[2][1],y[1][2]
            y[2][1],y[1][0]=y[1][0],y[2][1]
            if y[0][1]==r:
                break
        for j in range(4):
            if y[1][1]==l:
                break
            y[1][1],y[1][2]=y[1][2],y[1][1]
            y[1][2],y[1][3]=y[1][3],y[1][2]
            y[1][3],y[1][0]=y[1][0],y[1][3]
            if y[1][1]==l:
                break
    print(y[1][2])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????? I

?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°????????????????????????????????????
   +--+
 ???|???|
+--+--+--+--+
|???|???|???|???|
+--+--+--+--+
 ???|???|
   +--+

"""

class Dice:
    T = 0
    S = 1
    E = 2
    W = 3
    N = 4
    B = 5

    """ ????????????????????? """
    def __init__(self): # ?????????????????????
        self.side = [1, 2, 3, 4, 5, 6]
        self.top = 0

    def setDice(self, arr):  # ??¢??????????????????
        self.side = arr[0:6]

    def Turn(self, dir): # ?????????????????¢??????
        s = self.side

        if dir == "N":     # ????????¢??????
            #    Top 0     South 1   East 2    West 3    North 4   Bottom 5
            t = [s[Dice.S],s[Dice.B],s[Dice.E],s[Dice.W],s[Dice.T],s[Dice.N]]
        elif dir == "S": # ????????¢??????
            t = [s[Dice.N],s[Dice.T],s[Dice.E],s[Dice.W],s[Dice.B],s[Dice.S]]
        elif dir == "E": # ??±?????¢??????
            t = [s[Dice.W],s[Dice.S],s[Dice.T],s[Dice.B],s[Dice.N],s[Dice.E]]
        elif dir == "W": # ?\??????¢??????
            t = [s[Dice.E],s[Dice.S],s[Dice.B],s[Dice.T],s[Dice.N],s[Dice.W]]
        elif dir == "L": # ???????????¢
            t = [s[Dice.T],s[Dice.W],s[Dice.S],s[Dice.N],s[Dice.E],s[Dice.B]]
        elif dir == "R": # ???????????¢
            t = [s[Dice.T],s[Dice.E],s[Dice.N],s[Dice.S],s[Dice.W],s[Dice.B]]

        self.side = t
#        print("{0} T0:{1} S1:{2} W2:{3} E3:{4} N4:{5} B5:{6} ".format(dir, self.side[0],self.side[1], self.side[2],self.side[3], self.side[4],self.side[5]))

    def Search(self, tval, sval, dir): # ?????¢??¨?????¢???????????????????????????????????¢???????????????
    
        if self.side[Dice.E] == tval:
            self.Turn("W")
        elif self.side[Dice.W] == tval:
            self.Turn("E")
        elif self.side[Dice.N] == tval:
            self.Turn("S")
        elif self.side[Dice.S] == tval:
            self.Turn("N")
        elif self.side[Dice.B] == tval:
            self.Turn("N")
            self.Turn("N")

        for i in range(3): # ?????¢?????????????????????????????§????????????
            if self.side[Dice.S] == sval:
                break
            self.Turn("L")
            
        return self.side[dir]

dnum = list(map(int,input().strip().split()))
d = Dice()
d.setDice(dnum)
cnum = int(input().strip())
for c in range(cnum):
    cmd = list(map(int,input().strip().split()))
    val = d.Search(cmd[0], cmd[1], Dice.E)
    print(val)

Submission status: Accepted

Solution code:
def CP(a,b):
	x = a[1]*b[2]-a[2]*b[1]
	y = a[2]*b[0]-a[0]*b[2]
	z = a[0]*b[1]-a[1]*b[0]
	return (x,y,z)

minus = lambda d:tuple(-i for i in d)

class dice:
	def __init__(self,data):
		names = [(1,0,0),(0,0,1),(0,1,0),(0,-1,0),(0,0,-1),(-1,0,0)]
		self.faces = {n:d for n,d in zip(names,data)}
		self.top = (1,0,0)
		self.north = (0,0,-1)
	
	def turn(self,direction):
		if direction == 'N':
			self.top,self.north = minus(self.north),self.top
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'W':
			self.top = CP(self.top,self.north)
		elif direction == 'E':
			[self.turn('W') for i in range(3)]
	
	def get_name(self,value):
		for n,v in self.faces.items():
			if v == value:
				return n


if __name__ == '__main__':
	data = [int(i) for i in input().split()]
	d = dice(data)
	q = int(input())
	for i in range(q):
		a,b = [int(s) for s in input().split()]
		a_name = d.get_name(a)
		b_name = d.get_name(b)
		c_name = CP(b_name,a_name)
		print(d.faces[c_name])

Submission status: Accepted

Solution code:
x1,x2,x3,x4,x5,x6 = map(int,(input().split()))
n = int(input())
T=[]
for k in range(n):
    i,j = map(int, (input().split()))
    T.append([i,j])
L=[x1,x2,x3,x4,x5,x6]

for k in T:
    while L[0] != k[0]:
        L = [L[4], L[0], L[2], L[3], L[5], L[1]]
        if L[0] == k[0]:
            pass
        else:
            L = [L[3], L[1], L[0], L[5], L[4], L[2]]

    while L[1] != k[1]:
        L = [L[0], L[3], L[1], L[4], L[2], L[5]]
        if L[1] == k[1]:
            pass
        else:
            L = [L[0], L[3], L[1], L[4], L[2], L[5]]
    print(L[2])

Submission status: Accepted

Solution code:
class dice2():
    def __init__(self,men1,men2,men3,men4,men5,men6):
        self.men=[men1,men2,men3,men4,men5,men6]
        
    def find_num_rside(self,top,front):
        t=self.men.index(top)+1
        f=self.men.index(front)+1
        if [t,f] in [[1,2],[2,6],[6,5],[5,1]]:
            return self.men[3-1]#3
        elif  [t,f] in [[5,6],[6,2],[2,1],[1,5]]:
            return self.men[4-1]#4
        elif [t,f] in [[4,2],[2,3],[3,5],[5,4]]:
            return self.men[1-1]#1
        elif  [t,f] in [[5,3],[3,2],[2,4],[4,5]]:
            return self.men[6-1]#6
        elif  [t,f] in [[1,3],[3,6],[6,4],[4,1]]:
            return self.men[5-1]#5
        elif  [t,f] in [[4,6],[6,3],[3,1],[1,4]]:
            return self.men[2-1]#2
        
mn=list(map(int,input().split()))
q=int(input())

d=dice2(*mn)

for i in range(q):
    top, front=list(map(int,input().split()))
    ans=d.find_num_rside(top, front)
    print(ans)

Submission status: Accepted

Solution code:
class Dice:
    __slots__ = ['n_list', 'pair_list']

    def __init__(self, n_tup):
        self.n_list = list(n_tup)
        self.pair_list = [self.return_dice_round_pair(self.n_list, 2, 3, 5, 4),
                          self.return_dice_round_pair(self.n_list, 1, 4, 6, 3),
                          self.return_dice_round_pair(self.n_list, 1, 2, 6, 5),
                          self.return_dice_round_pair(self.n_list, 1, 5, 6, 2),
                          self.return_dice_round_pair(self.n_list, 1, 3, 6, 4),
                          self.return_dice_round_pair(self.n_list, 2, 4, 5, 3)
                          ]

    @staticmethod
    def roll_switch(n_list, from_idx_list, to_idx_list):
        n_list[from_idx_list[0] - 1], \
        n_list[from_idx_list[1] - 1], \
        n_list[from_idx_list[2] - 1], \
        n_list[from_idx_list[3] - 1] \
            = n_list[to_idx_list[0] - 1], \
              n_list[to_idx_list[1] - 1], \
              n_list[to_idx_list[2] - 1], \
              n_list[to_idx_list[3] - 1]
        return n_list

    def roll(self, direction):
        if direction == "N":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [2, 6, 5, 1])

        elif direction == "E":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [4, 1, 3, 6])

        if direction == "S":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [5, 1, 2, 6])

        if direction == "W":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [3, 6, 4, 1])

    @staticmethod
    def return_dice_round_pair(n_list, a, b, c, d):
        return [(n_list[a-1], n_list[b-1]),
                (n_list[b-1], n_list[c-1]),
                (n_list[c-1], n_list[d-1]),
                (n_list[d-1], n_list[a-1])]

    def return_right_num(self, top, forward):
        for i in range(6):
            if (top, forward) in self.pair_list[i]:
                return self.n_list[i]


dice = Dice([int(x) for x in input().split()])
cmd_cnt = int(input())

for i in range(cmd_cnt):
    top, forward = (int(x) for x in input().split())
    print(dice.return_right_num(top, forward))

Submission status: Accepted

Solution code:
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

die = list(map(int, input().split()))
n = int(input())
for i in range(n):
    a, b = list(map(int, input().split()))
    if die[0] == a:
        pass
    elif die[1] == a:
        die = roll(die, "N")
    elif die[2] == a:
        die = roll(die, "W")
    elif die[3] == a:
        die = roll(die, "E")
    elif die[4] == a:
        die = roll(die, "S")
    elif die[5] == a:
        die = roll(roll(die, "E"), "E")
    while True:
        if die[1] == b:
            break
        die = roll(die, "R")
    print(die[2])

Submission status: Accepted

Solution code:
# your code goes here
#dice2
def np(dl,nu):
    l=0
    while dl[l]!=nu:
        l+=1
    return l
def si(n1,n2):
    if n1==1:
        if n2==2:
            return 3
        elif n2==3:
            return 5
        elif n2==4:
            return 2
        else:
            return 4
    elif n1==2:
        if n2==1:
            return 4
        elif n2==3:
            return 1
        elif n2==4:
            return 6
        else:
            return 3
    elif n1==3:
        if n2==2:
            return 6
        elif n2==5:
            return 1
        elif n2==1:
            return 2
        else:
            return 5
    elif n1==4:
        if n2==1:
            return 5
        elif n2==2:
            return 1
        elif n2==5:
            return 6
        else:
            return 2
    elif n1==5:
        if n2==1:
            return 3
        elif n2==3:
            return 6
        elif n2==4:
            return 1
        else:
            return 4
    elif n1==6:
        if n2==3:
            return 2
        elif n2==2:
            return 4
        elif n2==5:
            return 3
        else:
            return 5
D=[int(i) for i in input().split()]
q=int(input())
for i in range(q):
    n=[int(i) for i in input().split()]
 #   print(n)
    p=[]
    for j in range(2):
        p.append(np(D,n[j]))
  #  print(p)
    print(D[si(p[0]+1,p[1]+1)-1])

Submission status: Accepted

Solution code:
# Aizu Problem ITP_1_11_B: Dice II
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def do_roll(dice, roll):
    d1, d2, d3, d4, d5, d6 = dice
    if roll == 'E':
        return [d4, d2, d1, d6, d5, d3]
    elif roll == 'W':
        return [d3, d2, d6, d1, d5, d4]
    elif roll == 'N':
        return [d2, d6, d3, d4, d1, d5]
    elif roll == 'S':
        return [d5, d1, d3, d4, d6, d2]
    else:
        assert False, "We should never reach this point!"
    

right_pos = {(1, 2): 3, (1, 4): 2, (1, 5): 4, (1, 3): 5,
             (2, 3): 1, (2, 1): 4, (2, 4): 6, (2, 6): 3,
             (3, 1): 2, (3, 2): 6, (3, 6): 5, (3, 5): 1,
             (4, 1): 5, (4, 2): 1, (4, 6): 2, (4, 5): 6,
             (5, 1): 3, (5, 4): 1, (5, 6): 4, (5, 3): 6,
             (6, 2): 4, (6, 3): 2, (6, 5): 3, (6, 4): 5}
def get_right(dice, top, front):
    p_top = dice.index(top) + 1
    p_front = dice.index(front)  + 1
    return dice[right_pos[(p_top, p_front)] - 1]
    """
    for k in range(4):
        dice = do_roll(dice, 'S')
        if dice[:2] == [top, front]:
            return dice[2]
    for j in range(4):
        dice = do_roll(dice, 'E')
        for k in range(4):
            dice = do_roll(dice, 'S')
            if dice[:2] == [top, front]:
                return dice[2]
    for j in range(4):
        dice = do_roll(dice, 'W')
        for k in range(4):
            dice = do_roll(dice, 'S')
            if dice[:2] == [top, front]:
                return dice[2]
    """
    
dice = [int(_) for _ in input().split()]
Q = int(input())
for q in range(Q):
    top, front = [int(_) for _ in input().split()]
    print(get_right(dice[:], top, front))