Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

  You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

 In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

  In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

  For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6



Sample input:
1 2 3 4 5 6
3
6 5
1 3
3 2


Sample output:
3
5
6

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self,rot,Face):
        self.rot= rot
        self.Face = Face
    def Rotate(rot,Face):
        r=len(rot)
        intFace=[int(Face[i]) for i in range(6)]

        for i in range(r):
            Temp=[intFace[i] for i in range(6)]

            if rot[i]== "E":
                intFace[0]=Temp[3]
                intFace[1]=Temp[1]
                intFace[2]=Temp[0]
                intFace[3]=Temp[5]
                intFace[4]=Temp[4]
                intFace[5]=Temp[2]

            elif rot[i]== "W":
                intFace[0]=Temp[2]
                intFace[1]=Temp[1]
                intFace[2]=Temp[5]
                intFace[3]=Temp[0]
                intFace[4]=Temp[4]
                intFace[5]=Temp[3]

            elif rot[i]== "N":
                intFace[0]=Temp[1]
                intFace[1]=Temp[5]
                intFace[2]=Temp[2]
                intFace[3]=Temp[3]
                intFace[4]=Temp[0]
                intFace[5]=Temp[4]

            elif rot[i]== "S":
                intFace[0]=Temp[4]
                intFace[1]=Temp[0]
                intFace[2]=Temp[2]
                intFace[3]=Temp[3]
                intFace[4]=Temp[5]
                intFace[5]=Temp[1]

        return intFace
#returnしてなかったわ インデント幅も注意

Face = input().split()
q=int(input())
intFace=[int(Face[i]) for i in range(6)]
j=0
output=[0 for i in range(q)]
while True:
    if j>=q:
        break
    else:
        rot=""
        faces =input().split()#入力を受け付ける
        #上面を合わせる回転を探す。
        if faces[0]==Face[0]:
            rot=""

        if faces[0]==Face[1]:
            rot+="N"

        if faces[0]==Face[2]:
            rot+="W"

        if faces[0]==Face[3]:
            rot+="E"

        if faces[0]==Face[4]:
            rot+="S"

        if faces[0]==Face[5]:
            rot+="NN"
    dice=Dice(rot,Face)
    tempFace= Dice.Rotate(rot,Face)#上面だけ合ってる
    non_intFace=[f"{tempFace[i]}" for i in range(6)]
    D= ["","NWS","NWSNWS","NWSNWSNWS",] #横に0回～3回回す。
    #前面を合わせる
    for i in range(4):
        temp2=Dice.Rotate(D[i],tempFace)
        if temp2[1]==int(faces[1]):
            finFace=temp2
            break
    output[j]=finFace[2]
    j+=1
for i in range(q):
    print(output[i])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.number = [i for i in range(6)]
        self.command_allpattern = "NNNNENNNNENNNNENNNNNENNNNEENNNNWWWSE"

    def set_number(self, d0, d1, d2, d3, d4, d5):
        self.number[0] = d0
        self.number[1] = d1
        self.number[2] = d2
        self.number[3] = d3
        self.number[4] = d4
        self.number[5] = d5

    def roll(self, command):
        if   command == "E":
            self.set_number(self.number[3],self.number[1],self.number[0],self.number[5],self.number[4],self.number[2])
        elif command == "N":
            self.set_number(self.number[1],self.number[5],self.number[2],self.number[3],self.number[0],self.number[4])
        elif command == "S":
            self.set_number(self.number[4],self.number[0],self.number[2],self.number[3],self.number[5],self.number[1])
        elif command == "W":
            self.set_number(self.number[2],self.number[1],self.number[5],self.number[0],self.number[4],self.number[3])
        else:
            raise

    def get_top(self):
        return self.number[0]

    def get_d2(self, d0, d1):
        flag_correct = False
        
        for command in self.command_allpattern:
            self.roll(command)

            if ((d0 == self.number[0]) and
                (d1 == self.number[1])):
                d2 = self.number[2]
                flag_correct = True

        if flag_correct:
            return d2
        else:
            raise



list_face = list(map(int, input().split()))

dice = Dice()
dice.set_number(list_face[0], list_face[1], list_face[2], list_face[3], list_face[4], list_face[5])


q = int(input())

for _ in range(q):

    d0, d1 = map(int, input().split())

    print(dice.get_d2(d0, d1))

Submission status: Accepted

Solution code:
# ITP_11_B
dice = input().split()
top_list =  [
    (-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 q in range(int(input())):
    top, front = [dice.index(s) for s in input().split()]
    print(dice[top_list[top][front]])

Submission status: Accepted

Solution code:
class Dice:
    N = [0, 1, 5, 4]
    S = N[::-1]
    E = [0, 3, 5, 2]
    W = E[::-1]
    R = [1, 2, 4, 3]
    L = R[::-1]

    def __init__(self, numbers):
        self.numbers = numbers

    def toX(self, x):
        data = getattr(self, x)
        self.numbers.append(0)
        for k in range(5):
            i1 = data[k - 1] if k != 0 else 6
            i2 = data[k] if k != 4 else 6
            self.numbers[i1] = self.numbers[i2]
        del self.numbers[6]

    def setTopandFront(self, top, front):
        while self.numbers[0] != top:
            if self.numbers.index(top) in self.N:
                self.toX('N')
            else:
                self.toX('E')
        while self.numbers[1] != front:
            self.toX('R')

dice = Dice([int(i) for i in input().split()])
for _ in range(int(input())):
    top, front = map(int, input().split())
    dice.setTopandFront(top, front)
    print(dice.numbers[2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from enum import Enum
import sys
import math

#0:中央,1:南,2:東,3:西,4:北,5:裏側
class Dice():

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

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

    def roll(self,loc):
        for i in range(6):
            self.work[i] = self.number[i]

        if loc == 'E':
            self.setNumber(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
        elif loc == 'N':
            self.setNumber(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
        elif loc == 'S':
            self.setNumber(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
        elif loc == 'W':
            self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])

    def query(self,top_num,front_num):
        self.save_data = [i for i in range(6)]
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]
        ret =-1

        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break
        for i in range(len(self.number)):
            self.number[i] = self.save_data[i]

        return ret;

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

num_query = int(input())
for loop in range(num_query):
    top_num,front_num = map(int,input().split())
    print("%d"%(dice.query(top_num,front_num)))

Submission status: Accepted

Solution code:
class Dice():
	def __init__(self):
		self.number = [int(i) for i in range(1,7)]
	
	def set_number(self,n0,n1,n2,n3,n4,n5):
		self.number[0] = n0
		self.number[1] = n1
		self.number[2] = n2
		self.number[3] = n3
		self.number[4] = n4
		self.number[5] = n5

	def roll(self,s):
		tmp = self.number
		if s == "S":
			self.set_number(tmp[4],tmp[0],tmp[2],tmp[3],tmp[5],tmp[1])
		elif s == "E":
			self.set_number(tmp[3],tmp[1],tmp[0],tmp[5],tmp[4],tmp[2])
		elif s == "N":
			self.set_number(tmp[1],tmp[5],tmp[2],tmp[3],tmp[0],tmp[4])
		elif s == "W":
			self.set_number(tmp[2],tmp[1],tmp[5],tmp[0],tmp[4],tmp[3])

	def get_number(self,i):
		# top: 0, front: 1, right: 2, left: 3, back: 4, bottom: 5 
		return self.number[i]

	def get_position(self,i):
		return self.number.index(i)

if __name__ == '__main__':
	dice_num = [int(s) for s in input().split()]
	num = int(input())
	dice = Dice()
	dice.set_number(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])

	for i in range(num):
		top,front = map(int,input().split())
		while dice.get_number(1) != front:
			if dice.get_position(front) == 2 or dice.get_position(front) == 3:
				dice.roll("E")
			else:
				dice.roll("S")
		while dice.get_number(0) != top:
			dice.roll("E")
		print("%d" % (dice.get_number(2)))
		dice.set_number(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.number = [i for i in range(6)]
        self.tmp = [i for i in range(6)]
        self.set_cmd = 'NNNNWNNNWNNNENNNENNNWNNN'


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

    def next(self,direction):
        for i in range(6):
            self.tmp[i] = self.number[i]

        if direction == 'E':
            self.setNumber(self.tmp[3], self.tmp[1], self.tmp[0], self.tmp[5], self.tmp[4], self.tmp[2])
        elif direction == 'N':
            self.setNumber(self.tmp[1], self.tmp[5], self.tmp[2], self.tmp[3], self.tmp[0], self.tmp[4])
        elif direction == 'S':
            self.setNumber(self.tmp[4], self.tmp[0], self.tmp[2], self.tmp[3], self.tmp[5], self.tmp[1])
        elif direction == 'W':
            self.setNumber(self.tmp[2], self.tmp[1], self.tmp[5], self.tmp[0], self.tmp[4], self.tmp[3])
    def get_result(self):
        return self.number[0]



    def search(self,top,forword):

        self.search_arr = [i for i in range(6)]
        for i in range(len(self.number)):
            self.search_arr[i] = self.number[i]


        for i in range(len(self.set_cmd)):
            self.next(self.set_cmd[i])

            right = 40

            if self.number[0] == top and self.number[1] == forword:
                right = self.number[2]
                break
        for i in range(len(self.number)):
            self.number[i] = self.search_arr[i]


        return right

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

cmd_fig = int(input())
for i in range(cmd_fig):
    top , forword = map(int,input().split())
    right_side = dice.search(top,forword)
    print("{}".format(right_side))

Submission status: Accepted

Solution code:
#! python3
# diceII.py - サイコロを転がす動きをシミュレートするプログラム 今度はトップとフロントが与えられたときの右

def getKeyFromValue(dice, value):
  keys = [k for k, v in dice.items() if v == value]
  if keys:
    return keys[0]
  return None

def initDice(numbers):  # サイコロオブジェクトに値を入れる
    # サイコロオブジェクトを定義, 配列の数値を、各面にわたす
    dice = {'top': numbers[0], 'bottom': numbers[5], 'front': numbers[1],
            'back': numbers[4], 'right': numbers[2], 'left': numbers[3]}
    return dice

def retrunRight(dice, first, second):  # サイコロのトップと前面の2つの値を受け取る
    first = getKeyFromValue(dice, first)
    second = getKeyFromValue(dice, second)
    if first == 'top':
        if second == 'front':
            right = dice['right']
        elif second == 'right':
            right = dice['back']
        elif second == 'back':
            right = dice['left']
        elif second == 'left':
            right = dice['front']
    elif first == 'bottom':
        if second == 'front':
            right = dice['left']
        elif second == 'left':
            right = dice['back']
        elif second == 'back':
            right = dice['right']
        elif second == 'right':
            right = dice['front']
    elif first == 'right':
        if second == 'front':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['back']
        elif second == 'back':
            right = dice['top']
        elif second == 'top':
            right = dice['front']
    elif first == 'left':
        if second == 'front':
            right = dice['top']
        elif second == 'top':
            right = dice['back']
        elif second == 'back':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['front']
    elif first == 'front':
        if second == 'top':
            right = dice['left']
        elif second == 'left':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['right']
        elif second == 'right':
            right = dice['top']
    elif first == 'back':
        if second == 'top':
            right = dice['right']
        elif second == 'right':
            right = dice['bottom']
        elif second == 'bottom':
            right = dice['left']
        elif second == 'left':
            right = dice['top']
    return right


numbers = [int(i) for i in input().split()]
dice = initDice(numbers)
q = int(input())

for i in range(q):
    first, second = [int(i) for i in input().split()]
    right = retrunRight(dice, first,second)
    print(right)

Submission status: Accepted

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

Submission status: Accepted

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

    def roll(self, axis, count=1):
        while count < 0:
            count += 4
        if axis == "X":
            for _ in range(count):
                self.top, self.back, self.bottom, self.front = self.front, self.top, self.back, self.bottom
        elif axis == "Y":
            for _ in range(count):
                self.top, self.right, self.bottom, self.left = self.left, self.top, self.right, self.bottom
        elif axis == "Z":
            for _ in range(count):
                self.front, self.left, self.back, self.right = self.right, self.front, self.left, self.back
        elif axis == "N":
            self.roll("X", 1)
        elif axis == "W":
            self.roll("Y", -1)
        elif axis == "S":
            self.roll("X", -1)
        elif axis == "E":
            self.roll("Y", 1)


def solve(d, top, front):
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("Z")
    d.roll("X")
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("Y")
    d.roll("X")
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("Z")
    d.roll("X")
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("Y")
    d.roll("X")
    d.roll("Y")
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("X")
    d.roll("Y", 2)
    for _ in range(4):
        if d.top == top and d.front == front:
            return d.right
        d.roll("X")


labels = [int(c) for c in input().split()]
d = Dice(labels)
q = int(input())
for _ in range(q):
    top, front = map(int, input().split())
    print(solve(d, top, front))

Submission status: Accepted

Solution code:
class Dice():
  def __init__ (self):
    # 1:ue 2:syomen 3:migi 4:hidari 5:oku 6:shita
    self.position = [0,0,0,0,0,0]
    self.search_result = []
  
  def setNumber (self, num):
    for i,x in enumerate(num):
      self.position[i] = x
    #print(self.position)
      
  def roll(self, roll):
    for i in range(len(roll)):
      s = roll[i:i+1]
      p = self.position
      if s == 'E':
        self.setNumber([p[3], p[1], p[0], p[5], p[4], p[2]])
      elif s == 'W':
        self.setNumber([p[2], p[1], p[5], p[0], p[4], p[3]])
      elif s == 'S':
        self.setNumber([p[4], p[0], p[2], p[3], p[5], p[1]])
      elif s == 'N':
        self.setNumber([p[1], p[5], p[2], p[3], p[0], p[4]])

  def printTop(self):
    print(self.position[0])

  def searchRight(self, a, b):
    i1 = self.position.index(a)
    i2 = self.position.index(b)
    #print("i1:" + str(i1) + " i2:" + str(i2) )
    
    # 0
    if (i1 == 1 and i2 == 2) or (i1 == 2 and i2 == 4) or (i1 == 4 and i2 == 3) or (i1 == 3 and i2 == 1):
      self.search_result.append(self.position[0])
    # 1
    elif (i1 == 0 and i2 == 3) or (i1 == 3 and i2 == 5) or (i1 == 5 and i2 == 2) or (i1 == 2 and i2 == 0):
      self.search_result.append(self.position[1])
    # 2
    elif (i1 == 0 and i2 == 1) or (i1 == 1 and i2 == 5) or (i1 == 5 and i2 == 4) or (i1 == 4 and i2 == 0):
      self.search_result.append(self.position[2])
    # 3
    elif (i1 == 1 and i2 == 0) or (i1 == 0 and i2 == 4) or (i1 == 4 and i2 == 5) or (i1 == 5 and i2 == 1):
      self.search_result.append(self.position[3])
    # 4
    elif (i1 == 0 and i2 == 2) or (i1 == 2 and i2 == 5) or (i1 == 5 and i2 == 3) or (i1 == 3 and i2 == 0):
      self.search_result.append(self.position[4])
    # 5
    elif (i1 == 2 and i2 == 1) or (i1 == 1 and i2 == 3) or (i1 == 3 and i2 == 4) or (i1 == 4 and i2 == 2):
      self.search_result.append(self.position[5])
    
  def printResult(self):
    for i in self.search_result:
      print (i)


d = Dice()

n = list(int(x) for x in input().split())
d.setNumber(n)

q = int(input())
for i in range(q):
  a,b = (int(x) for x in input().split())
  d.searchRight(a,b)

d.printResult()

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self,my_label):
        self.top,self.front,self.right,self.left,self.back,self.bottom = [int(x) for x in my_label]

    def Roll(self,i):
        if i == 0:
            self.top, self.front, self.right, self.left, self.back, self.bottom = \
            self.right, self.front, self.bottom, self.top, self.back, self.left
        
        if i == 1:
            self.top, self.front, self.right, self.left, self.back, self.bottom = \
            self.left, self.front, self.top, self.bottom, self.back, self.right
        
        if i == 2:
            self.top, self.front, self.right, self.left, self.back, self.bottom = \
            self.back, self.top, self.right, self.left, self.bottom, self.front
            
        if i == 3:
            self.top, self.front, self.right, self.left, self.back, self.bottom = \
            self.front, self.bottom, self.right, self.left, self.top, self.back

    def Open(self):
        print(self.top)


def CheckData(data,top,front):
    for i in range(len(data)):
        if(data[i][0] == top and data[i][1] == front):
            print(data[i][2])
            break

def MakeData(dice):
    num_data = 4 * len(my_label)-1
    
    dataset = list()
    data = [dice.top, dice.front, dice.right]

    dataset.append(data)
    
    isOnly = True
    
    while 0 < num_data:
        data = [dice.top, dice.front, dice.right]

        isOnly = True
        for i in range (len(dataset)):
            if(data == dataset[i]):
                isOnly = False
                break
        if isOnly == True:
            dataset.append(data)
            dataset.sort()
            num_data -= 1
        
        dice.Roll(int(random.random() * 4))
        
    
        
    return dataset


import random
my_label = [int(x) for x in input().split(" ")]
dice = Dice(my_label)
data = MakeData(dice)


q = int(input())
while q > 0:
    n_top,n_front = [int(x) for x in input().split(" ")]
    
    CheckData(data,n_top,n_front)
    
    q -= 1

Submission status: Accepted

Solution code:
import random

class dice:
    def __init__(self, t, S, E):
        self.dice = list(map(int, input().split()))
        self.t = t
        self.S = S
        self.E = E
        self.now_dice()
    
    def roll(self, order):
        if order == 'E':
            self.t, self.E = self.W, self.t
        elif order == 'N':
            self.t, self.S = self.S, self.g
        elif order == 'S':
            self.t, self.S = self.N, self.t
        elif order == 'W':
            self.t, self.E = self.E, self.g
        self.now_dice()
    
    def now_dice(self):
        self.N = 7 - self.S
        self.g = 7 - self.t
        self.W = 7 - self.E
        self.now = [self.t, self.S, self.E, self.N, self.W, self.g]
        return self.now

if __name__ == '__main__':
    dice = dice(1, 2, 3)
    x = int(input())
    for _ in range(x):
        a, b = map(int, input().split())
        while 1:
            direc = ['N', 'E', 'W']
            random.shuffle(direc)
            dice.roll(direc[0])
            if dice.dice[dice.t-1] == a and dice.dice[dice.S-1] == b:
                print(dice.dice[dice.E-1])
                break

Submission status: Accepted

Solution code:
def move(dice, order):
  if order == "E":
    temp = dice[0]
    dice[0] = dice[3]
    dice[3] = dice[5]
    dice[5] = dice[2]
    dice[2] = temp
  elif order == "N":
    temp = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = temp
  elif order == "S":
    temp = dice[0]
    dice[0] = dice[4]
    dice[4] = dice[5]
    dice[5] = dice[1]
    dice[1] = temp
  elif order == "W":
    temp = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = temp
  else:
    pass
  return dice

def roll(dice):
  temp = dice[1]
  dice[1] = dice[2]
  dice[2] = dice[4]
  dice[4] = dice[3]
  dice[3] = temp
  return dice

dice = list(map(int, input().split()))
q = int(input())
for i in range(q):
  up, front = map(int, input().split())
  cnt = 0
  while (dice[0] != up and cnt <= 3):
    dice = move(dice, "E")
    cnt += 1
  while dice[0] != up:
    dice = move(dice, "N")
  while dice[1] != front:
    dice = roll(dice)
  print(dice[2])

Submission status: Accepted

Solution code:
def sgn(a, b):
    return int(a*b/abs(a*b))

numbers = list(map(int, input().split()))
numbers_dict = {1:numbers[0], 2:numbers[1], 3:numbers[2], -3:numbers[3], -2:numbers[4], -1:numbers[5]}
numbers_ver_dict = {numbers[0]:1, numbers[1]:2,numbers[2]:3,numbers[3]:-3, numbers[4]:-2,numbers[5]:-1}
n = int(input())
for _ in range(n):
    a, b = map(int, input().split())
    var_a = numbers_ver_dict[a]
    var_b = numbers_ver_dict[b]
    if abs(var_a) == 1:
        if abs(var_b) == 2:
            print(numbers_dict[sgn(var_a,var_b)*3])
        else:
            print(numbers_dict[-sgn(var_a,var_b)*2])
    elif abs(var_a) == 2:
        if abs(var_b) == 1:
            print(numbers_dict[-sgn(var_a, var_b)*3])
        else:
            print(numbers_dict[sgn(var_a,var_b)])
    else:
        if abs(var_b) == 1:
            print(numbers_dict[sgn(var_a, var_b)*2])
        else:
            print(numbers_dict[-sgn(var_a,var_b)])

Submission status: Accepted

Solution code:
buff = "NNNNWNNNWNNNENNNENNNWNNN"

class Dice:
        num =[ -1 for x in range(6) ]
        def roll(self, d):
                if   d == "N":
                        b = self.num[0]
                        self.num[0] = self.num[1]
                        self.num[1] = self.num[5]
                        self.num[5] = self.num[4]
                        self.num[4] = b
                elif d == "S":
                        b = self.num[0]
                        self.num[0] = self.num[4]
                        self.num[4] = self.num[5]
                        self.num[5] = self.num[1]
                        self.num[1] = b
                elif d == "W":
                        b = self.num[3]
                        self.num[3] = self.num[0]
                        self.num[0] = self.num[2]
                        self.num[2] = self.num[5]
                        self.num[5] = b
                elif d == "E":
                        b = self.num[3]
                        self.num[3] = self.num[5]
                        self.num[5] = self.num[2]
                        self.num[2] = self.num[0]
                        self.num[0] = b
        
        def __init__(self, lst):
                self.num = lst
        
        def print_num(self, m):
                #print(self.num[m])
                return self.num[m]

lst = [ int(i) for i in input().split() ]
q = int(input())

qst = []
for p in range(q):
        qst.append([ int(i) for i in input().split() ])


dice = Dice(lst)

for c in qst:
        for d in buff:
                dice.roll(d)
                if dice.print_num(0) == c[0] and dice.print_num(1) == c[1]:
                        print(dice.print_num(2))

Submission status: Accepted

Solution code:
d=input().split()
mawari=[[1,2,4,3,1],[5,2,0,3,5],[5,4,0,1,5],[5,1,0,4,5],[5,3,0,2,5],[4,2,1,3,4]]

for _ in range(int(input())):
    a,b=[d.index(i) for i in input().split()]
    n=mawari[a]
    print(d[n[n.index(b)+1]])

Submission status: Accepted

Solution code:
l=input().split()
for _ in[0]*int(input()):
	t,f=[l.index(s)for s in input().split()]
	g=lambda i:i if t<3else 5-i
	print(l[g(int(['_2413_','3_05_2','15__04'][g(t)][f]))])

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, labels):
        self.labels = labels
        
    def __str__(self):
        return str(self.labels[0])
    
    def roll_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]
            
    def roll_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 roll_west(self):
        self.labels[0], self.labels[3], self.labels[5], self.labels[2] = self.labels[2], self.labels[0], self.labels[3], self.labels[5]
    
    def roll_north(self):
        self.labels[0], self.labels[4], self.labels[5], self.labels[1] = self.labels[1], self.labels[0], self.labels[4], self.labels[5]
        
    def get_faces(self):
        return self.labels
        
def main():
    labels = list(map(int, input().split()))
    d = Dice(labels)
    n = int(input())
    for i in range(n):
        faces = list(map(int, input().split()))
        ind0 = labels.index(faces[0])
        ind1 = labels.index(faces[1])
        if ind0 == 1:
            d.roll_north()
        elif ind0 == 4:
            d.roll_south()
        else:
            while d.get_faces()[0] != faces[0]:
                d.roll_east()
        while d.get_faces()[1] != faces[1]:
            d.roll_east()
            d.roll_south()
            d.roll_west()
        print(d.get_faces()[2])
        
main()

Submission status: Accepted

Solution code:
import random

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

    def __init__(self, u, s, e, w, n, d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d
        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


ans = []

u1, s1, e1, w1, n1, d1 = map(int, input().split())
dice1 = Dice(u1, s1, e1, w1, n1, d1)
q = int(input())

for i in range(q):
    a, b = map(int, input().split())

    while True:
        dice1.rot(random.randint(0,3))
        if dice1.u == a and dice1.s == b:
            ans.append(dice1.e)
            break

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