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, top: str, front: str, right: str, left: str, back: str, bottom: str) -> None:
        self.top = top
        self.front = front
        self.right = right
        self.left = left
        self.back = back
        self.bottom = bottom

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

    def print_right(self):
        print(self.right)

    def roll(self, direction: str) -> None:
        if direction == 'S':
            self.south()
        elif direction == 'N':
            self.north()
        elif direction == 'E':
            self.east()
        elif direction == 'W':
            self.west()

    def north(self):
        [self.top, self.back, self.bottom, self.front] = [self.front, self.top, self.back, self.bottom]

    def south(self):
        [self.top, self.back, self.bottom, self.front] = [self.back, self.bottom, self.front, self.top]

    def east(self):
        [self.top, self.right, self.bottom, self.left] = [self.left, self.top, self.right, self.bottom]

    def west(self):
        [self.top, self.right, self.bottom, self.left] = [self.right, self.bottom, self.left, self.top]

    def rotate_right(self) -> None:
        for direction in 'SWN':
            dice.roll(direction)


def solve(dice: Dice, top: str, front: str) -> None:
    # find top
    found = False
    for direction in 'SSSS':
        dice.roll(direction)
        if dice.top == top:
            found = True
            break
    if not found:
        for direction in 'EEE':
            dice.roll(direction)
            if dice.top == top:
                break

    # find front
    for _ in range(4):
        dice.rotate_right()
        if dice.front == front:
            break

    # answer right
    dice.print_right()


labels = input().split()
dice = Dice(*labels)
n = int(input())
for i in range(n):
    top, front = input().split()
    solve(dice, top, front)

Submission status: Accepted

Solution code:
class Dice():
    def __init__(self, a, b, c, d, e, f):
        """面の数字とindexを一致させるために0を挿入"""
        self.s = [0, a, b, c, d, e, f]

    def rotate(self, dir):
        if dir == "N":
            self.s[0] = self.s[1] #s[0]に一時的にs[1]を保持
            self.s[1] = self.s[2] 
            self.s[2] = self.s[6]
            self.s[6] = self.s[5]
            self.s[5] = self.s[0]
            return

        elif dir == "W":
            self.s[0] = self.s[1]
            self.s[1] = self.s[3] 
            self.s[3] = self.s[6]
            self.s[6] = self.s[4]
            self.s[4] = self.s[0]
            return

        elif dir == "S":
            self.s[0] = self.s[1]
            self.s[1] = self.s[5] 
            self.s[5] = self.s[6]
            self.s[6] = self.s[2]
            self.s[2] = self.s[0]
            return

        elif dir == "E":
            self.s[0] = self.s[1]
            self.s[1] = self.s[4] 
            self.s[4] = self.s[6]
            self.s[6] = self.s[3]
            self.s[3] = self.s[0]
            return
        
        elif dir == "R":
            self.s[0] = self.s[2]
            self.s[2] = self.s[3] 
            self.s[3] = self.s[5]
            self.s[5] = self.s[4]
            self.s[4] = self.s[0]
            return

        elif dir == "L":
            self.s[0] = self.s[2]
            self.s[2] = self.s[4] 
            self.s[4] = self.s[5]
            self.s[5] = self.s[3]
            self.s[3] = self.s[0]
            return

        else:
            return


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

for _ in range(int(input())):
    t, s = map(int, input().split())

    while dice.s[1] != t:
        dice.rotate("S")
        if dice.s[1] != t:
            dice.rotate("E")
    
    while dice.s[2] != s:
        dice.rotate("R")

    print(dice.s[3])

Submission status: Accepted

Solution code:
class Dice(object):
    def __init__(self, numbers: list):
        self.numbers = dict(zip(("T", "S", "E", "W", "N", "B"), numbers))

    def _roll(self, old_part: tuple):
        new_part = old_part[1:] + (old_part[0],)
        new_numbers = [self.numbers[face] for face in new_part]

        for old, new in zip(old_part, new_numbers):
            self.numbers[old] = new

    def vroll(self, reverse=False):
        """北方向に回転"""
        old_part = ("N", "T", "S", "B")
        self._roll(old_part if not reverse else old_part[::-1])

    def hroll(self, reverse=False):
        """時計回りに回転"""
        old_part = ("N", "W", "S", "E")
        self._roll(old_part if not reverse else old_part[::-1])

    def roll_to(self, direction: str):

        if direction == "N":
            self.vroll()
        elif direction == "S":
            self.vroll(reverse=True)
        elif direction == "E":
            self.hroll(reverse=True)
            self.vroll()
            self.hroll()
        elif direction == "W":
            self.hroll()
            self.vroll()
            self.hroll(reverse=True)
        else:
            raise AssertionError

    def set_pos(self, top, front):
        numbers = self.numbers
        if top in (numbers["E"], numbers["W"]):
            self.hroll()
        while top != numbers["T"]:
            self.vroll()
        while front != numbers["S"]:
            self.hroll()


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

outs = []
for _ in range(int(input())):
    top, front = map(int, input().split())
    dice.set_pos(top, front)
    outs.append(dice.numbers["E"])

print(*outs, sep="\n")

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, one: int, two: int, three: int, four: int, five: int, six: int) -> None:
        self.__one = one
        self.__two = two
        self.__three = three
        self.__four = four
        self.__five = five
        self.__six = six

    def roll(self, direction) -> None:
        if direction == "N":
            tmp = self.__one
            self.__one = self.__two
            self.__two = self.__six
            self.__six = self.__five
            self.__five = tmp

        if direction == "E":
            tmp = self.__one
            self.__one = self.__four
            self.__four = self.__six
            self.__six = self.__three
            self.__three = tmp

        if direction == "S":
            tmp = self.__one
            self.__one = self.__five
            self.__five = self.__six
            self.__six = self.__two
            self.__two = tmp

        if direction == "W":
            tmp = self.__one
            self.__one = self.__three
            self.__three = self.__six
            self.__six = self.__four
            self.__four = tmp

    def set_top(self, top: int) -> None:
        if dice.top != top:
            for i in range(3):
                dice.roll("N")
                if (dice.top == top):
                    break
        if dice.top != top:
            for i in range(3):
                dice.roll("E")
                if (dice.top == top):
                    break

    def rotate(self) -> None:
        tmp = self.__two
        self.__two = self.__four
        self.__four = self.__five
        self.__five = self.__three
        self.__three = tmp

    @property
    def top(self) -> int:
        return self.__one

    @property
    def front(self) -> int:
        return self.__two

    @property
    def right(self) -> int:
        return self.__three


if __name__ == "__main__":
    one, two, three, four, five, six = map(int, input().split(' '))
    dice = Dice(one, two, three, four, five, six)
    q = int(input())
    for i in range(q):
        top, front = map(int, input().split(' '))
        dice.set_top(top)
        while dice.front != front:
            dice.rotate()
        print(dice.right)

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, lst):
        self.a = lst[0]
        self.b = lst[1]
        self.c = lst[2]
        self.d = lst[3]
        self.e = lst[4]
        self.f = lst[5]
        
    def roll(self, orders):
        for order in orders:
            if order=='W':
                tmp = self.a
                self.a = self.c
                self.c = self.f
                self.f = self.d
                self.d = tmp
            elif order=='E':
                tmp = self.a
                self.a = self.d
                self.d = self.f
                self.f = self.c
                self.c = tmp
            elif order=='S':
                tmp = self.a
                self.a = self.e
                self.e = self.f
                self.f = self.b
                self.b = tmp
            elif order=='N':
                tmp = self.a
                self.a = self.b
                self.b = self.f
                self.f = self.e
                self.e = tmp
#             print(self.a, self.b, self.c, self.d, self.e, self.f)
#         print(self.a)
       
            
import random
if __name__ == '__main__':
    lst = [int(x) for x in input().split()]
    q = int(input())
    questions = [[int(x) for x in input().split()] for _ in range(q)]
    
    dice = Dice(lst)    # 初期化
    direction = ['N', 'E', 'S', 'W']
    for question in questions:
        """ diceをランダムに回し、所望の状態になったら状態を出力
        """
        while True:
            dice.roll(random.choice(direction))
            if dice.a==question[0] and dice.b==question[1]:
                print(dice.c)
                break

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, dice):
        self.dice = dice
        self.face = {'head':1, 'tail':4, 'left':3, 'right':2, 'above':0, 'below':5}
    
    def roll(self, ch):
        dice = self.dice
        face = self.face
        if ch == 'S':
            tmp = dice[face['head']]
            dice[face['head']] = dice[face['above']]
            dice[face['above']] = dice[face['tail']]
            dice[face['tail']] = dice[face['below']]
            dice[face['below']] = tmp
        elif ch == 'N':
            tmp = dice[face['head']]
            dice[face['head']] = dice[face['below']]
            dice[face['below']] = dice[face['tail']]
            dice[face['tail']] = dice[face['above']]
            dice[face['above']] = tmp
        elif ch == 'W':
            tmp = dice[face['left']]
            dice[face['left']] = dice[face['above']]
            dice[face['above']] = dice[face['right']]
            dice[face['right']] = dice[face['below']]
            dice[face['below']] = tmp
        else:
            tmp = dice[face['left']]
            dice[face['left']] = dice[face['below']]
            dice[face['below']] = dice[face['right']]
            dice[face['right']] = dice[face['above']]
            dice[face['above']] = tmp

    def top(self):
        dice = self.dice
        face = self.face
        return dice[face['above']]
    
    def find_right(self, a, b):
        dice = self.dice
        face = self.face
        order = 'NNNNWNNNWNNNENNNENNNWNNN'
        for ch in order:
            self.roll(ch)
            if dice[face['above']] == a and dice[face['head']] == b:
                break
        return dice[face['right']]

dice = Dice([*map(int, input().split())])
q = int(input())
while q > 0:
    q -= 1
    a, b = map(int, input().split())
    print(dice.find_right(a, b))

Submission status: Accepted

Solution code:
roll_dict = dict(E = (3, 1, 0, 5, 4, 2), W = (2, 1, 5, 0, 4, 3), S = (4, 0, 2, 3, 5, 1), N = (1, 5, 2, 3, 0, 4))

dices = []
dices.append(list(map(int, input().split())))
q = int(input())

for i in "EWSN":
  dice = dices[0]
  new_dice = []
  for j in range(6):
    new_dice.append(dice[ roll_dict[i][j] ])
  dices.append(new_dice)

dice = dices[1]
new_dice = []
for i in range(6):
  new_dice.append(dice[ roll_dict["E"][i] ])
dices.append(new_dice)

for i in range(q):
  upper, front = map(int, input().split())
  for j in range(6):
    if dices[j][0] == upper:
      if dices[j][1] == front:
        print(dices[j][2])
      elif dices[j][2] == front:
        print(dices[j][4])
      elif dices[j][4] == front:
        print(dices[j][3])
      elif dices[j][3] == front:
        print(dices[j][1])

Submission status: Accepted

Solution code:
# Problem - サイコロ

# input
t, s, e, w, n, b = map(int, input().split())
p_num = int(input())

# initialization
dice_dic = {}
dice_dic[t] = {s:e, e:n, n:w, w:s}
dice_dic[b] = {n:e, w:n, s:w, e:s}
dice_dic[s] = {e:t, t:w, w:b, b:e}
dice_dic[n] = {t:e, e:b, b:w, w:t}
dice_dic[e] = {s:b, b:n, n:t, t:s}
dice_dic[w] = {s:t, t:n, n:b, b:s}

# process
for i in range(p_num):
    in_1, in_2 = map(int, input().split())
    print(dice_dic[in_1][in_2])

Submission status: Accepted

Solution code:
dice = input().rstrip().split(' ')
indication = "EEENEEENEEESEEESEEENEEENSWWSWWWNWWWNWWWSWWWSWWW"
indic = list(indication)
length = len(indic)
num = int(input())
for j in range(num):
  s = input().rstrip().split(' ')
  top = int(s[0])
  front = int(s[1])
  for i in range(length):
    newdice = [0] * 6
    if str(indic[i]) == "N":
      newdice[0] = dice[1]
      newdice[1] = dice[5]
      newdice[2] = dice[2]
      newdice[3] = dice[3]
      newdice[4] = dice[0]
      newdice[5] = dice[4]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "S":
      newdice[0] = dice[4]
      newdice[1] = dice[0]
      newdice[2] = dice[2]
      newdice[3] = dice[3]
      newdice[4] = dice[5]
      newdice[5] = dice[1]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "E":
      newdice[0] = dice[3]
      newdice[1] = dice[1]
      newdice[2] = dice[0]
      newdice[3] = dice[5]
      newdice[4] = dice[4]
      newdice[5] = dice[2]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;
    elif str(indic[i]) == "W":
      newdice[0] = dice[2]
      newdice[1] = dice[1]
      newdice[2] = dice[5]
      newdice[3] = dice[0]
      newdice[4] = dice[4]
      newdice[5] = dice[3]
      dice = newdice
      if int(dice[0]) == top and int(dice[1]) == front:
        print(int(dice[2]))
        break;

Submission status: Accepted

Solution code:
class dice():
    
    def __init__(self, nums):
        self.pos = []
        self.pos.extend(nums)
    
    def right(self, et, ef):
        # t : 上面のラベル
        # f : 正面のラベル

        t = self.pos.index(et)  # 上面（の扱いになっている）のラベルを取得
        f = self.pos.index(ef)  # 前面（の扱いになっている）のラベルを取得
        
        # u,f,r
        dr = [(1,2,4,3),(0,3,5,2),(0,1,5,4),(0,4,5,1),(0,2,5,3),(1,3,4,2)]
        r = (dr[t].index(f) + 1) % 4
        print(self.pos[dr[t][r]])

d = dice([int(x) for x in input().split()])
q = int(input())
for i in range(q):
    d.right(*map(int, input().split()))

Submission status: Accepted

Solution code:
class dice():
    def __init__(self,labels):
        self.l=labels

    def zen(self,a,b):
        x1=self.l[0]
        x2=self.l[1]
        x3=self.l[2]
        x4=self.l[3]
        x5=self.l[4]
        x6=self.l[5]
        if (a,b)==(x1,x2) or (a,b)==(x2,x6) or (a,b)==(x6,x5) or (a,b)==(x5,x1):
            print(x3)
        elif (a,b)==(x3,x2) or (a,b)==(x2,x4) or (a,b)==(x4,x5) or (a,b)==(x5,x3):
            print(x6)
        elif (a,b)==(x6,x2) or (a,b)==(x2,x1) or (a,b)==(x1,x5) or (a,b)==(x5,x6):
            print(x4)
        elif (a,b)==(x3,x1) or (a,b)==(x1,x4) or (a,b)==(x4,x6) or (a,b)==(x6,x3):
            print(x2)
        elif (a,b)==(x3,x6) or (a,b)==(x6,x4) or (a,b)==(x4,x1) or (a,b)==(x1,x3):
            print(x5)
        elif (a,b)==(x4,x2) or (a,b)==(x2,x3) or (a,b)==(x3,x5) or (a,b)==(x5,x4):
            print(x1)

dice=dice([int(i) for i in input().split()])
n=int(input())
for i in range(n):
    a,b=map(int,input().split())
    dice.zen(a,b)

Submission status: Accepted

Solution code:
from copy import deepcopy

class Dice():

    def __init__(self):
        self.E = [4,2,1,6,5,3]
        self.N = [2,6,3,4,1,5]
        self.S = [5,1,3,4,6,2]
        self.W = [3,2,6,1,5,4]
        self.order = "NNNNWNNNWNNNENNNENNNWNNN"

    def num_input(self):
        self.number = list(map(int,input().split()))

    def rotate(self,order):
        tmp = deepcopy(self.number)
        if order == "E":
            for i in range(6):
                self.number[i] = tmp[self.E[i]-1]
        elif order == "N":
            for i in range(6):
                self.number[i] = tmp[self.N[i]-1]
        elif order == "S":
            for i in range(6):
                self.number[i] = tmp[self.S[i]-1]
        elif order == "W":
            for i in range(6):
                self.number[i] = tmp[self.W[i]-1]
    
    def sidenum(self,x,y):
        for i in self.order:
            if self.number[1] == y and self.number[0] == x:
                print(self.number[2])
                break
            else:
                self.rotate(i)

if __name__ == "__main__":
    dice = Dice()
    dice.num_input()
    q = int(input())
    for i in range(q):
        x,y = map(int,input().split())
        dice.sidenum(x,y)

Submission status: Accepted

Solution code:


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

    def roll_n(self):
        d = self.dice
        self.dice = [d[1],d[5],d[2],d[3],d[0],d[4]]

    def roll_e(self):
        d = self.dice
        self.dice = [d[3],d[1],d[0],d[5],d[4],d[2]]

    def roll_s(self):
        d = self.dice
        self.dice = [d[4],d[0],d[2],d[3],d[5],d[1]]
    
    def roll_w(self):
        d = self.dice
        self.dice = [d[2],d[1],d[5],d[0],d[4],d[3]]

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

    def roll(self,command):
        if command=='N':self.roll_n()
        if command=='E':self.roll_e()
        if command=='S':self.roll_s()
        if command=='W':self.roll_w()

def main():
    com1 = 'SSSWSSS'
    com2 = 'WWWW'
    d = dice(list(map(int,input().split())))
    q = int(input())
    for _ in range(q):
        a,b = map(int,input().split())
        for c in com1:
            if b==d.dice[1]:break
            d.roll(c)
        for c in com2:
            if a==d.dice[0]:print (d.dice[2])
            d.roll(c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=list(map(int,input().split()))
p=['2354','3146','2651','1562','1364','2453']
for _ in range(int(input())):
    t,m=map(int,input().split())
    t=n.index(t)
    m=str(n.index(m)+1)
    print(n[int(p[t][(p[t].index(m)+1)%4])-1])