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:
    state={}
    writtenval={}
    def __init__(self, state_=None,writtenval_=None):
        self.writtenval = writtenval_
        

        self.state={}
        if state_ != None:
            for key in state_.keys():
                self.state[key]=writtenval.index(state_[key])
            for keyset in [["now","behind"], ["N","S"], ["S","N"], ["W","E"], ["E","W"], ["behind","now"]]:
                if not keyset[0] in self.state.keys():
                    if keyset[1] in self.state.keys():
                        self.state[keyset[0]]=7-self.state[keyset[1]]
            if len(self.state.keys()) == 4 :
                state=self.state
                keys=self.state.keys()
                #どのペアがnull?
                if not "N" in keys or not "S" in keys:
                    if state["now"] == 1:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["E"] == 3:
                            state["N"]=1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==3:
                        if state["E"] == 6:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==4:
                        if state["E"] == 1:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==5:
                        if state["E"] == 1:
                            state["N"]=4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==6:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    else:
                        print("miss")
                elif not "W" in keys or not "E" in keys:
                    if state["now"] == 1:
                        if state["N"] == 5:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["N"] == 1:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 3:
                        if state["N"] == 1:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 4:
                        if state["N"] == 1:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 5:
                        if state["N"] == 1:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 6:
                        if state["N"] == 2:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                elif not "now" in keys or not "behind" in keys:
                    if state["N"] == 1:
                        if state["W"] == 2:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 5:
                            state["now"]=4
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=2
                            state["E"]=7-state["now"]
                        elif state["W"] == 3:
                            state["now"]=5
                            state["E"]=7-state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 2:
                        if state["W"] == 3:
                            state["now"]=1
                            state["E"]=7-state["now"]
                        elif state["W"] == 6:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=6
                            state["E"]=7-state["now"]
                        elif state["W"] == 1:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 3:
                        if state["W"] == 5:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 4:
                        if state["W"] == 2:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 5:
                        if state["W"] == 4:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 3:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 6:
                        if state["W"] == 3:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 4:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    else:
                        print("miss")
            else:
                print("miss")
        else:
            self.state={"now":6,"N":5,"S":2,"W":4,"E":3,"behind":1}
        self.writtenval=writtenval_

    def chState(self):
        state=self.state
        for order in input():
            if order == "N":
                newstate = {"now": state["N"],
                            "N": state["behind"],
                            "S": state["now"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["S"]
                            }
                state=newstate
            elif order == "S":
                newstate = {"now": state["S"],
                            "N": state["now"],
                            "S": state["behind"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["N"]
                            }
                state=newstate
            elif order == "W":
                newstate = {"now": state["W"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["behind"],
                            "E":state["now"],
                            "behind":state["E"]
                            }
                state=newstate
            elif order == "E":
                newstate = {"now": state["E"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["now"],
                            "E":state["behind"],
                            "behind":state["W"]
                            }
                state = newstate
        self.state=state

writtenval=[-1]+[int(item) for item in input().split()]
loop=int(input())
for _ in range(loop):
    [behind,N]=[int(item) for item in input().split()]
    dice_ = dice({"N":N,"behind":behind},writtenval)
    print(dice_.writtenval[dice_.state["E"]])
#dice_ = dice()

Submission status: Accepted

Solution code:
dice = ['23542', '14631', '12651', '15621', '13641', '24532']

d = input().split()
q = int(input())

for _ in range(q):
    num = input().split()
    order = str(d.index(num[0]) + 1) + str(d.index(num[1]) + 1)
    [print(d[i]) for i in range(6) if order in dice[i]]

Submission status: Accepted

Solution code:
class Dice():

    def __init__(self, nums):
        self.__indexTop__ = 1
        self.__indexFront__ = 2
        self.__indexRight__ = 3
        self.__indexLeft__ = 4
        self.__indexBack__ = 5
        self.__indexBottom__ = 6
        self.__sides__ = {1:nums[0], 2:nums[1], 3:nums[2], 4:nums[3], 5:nums[4], 6:nums[5]}
    
    def getTopSideNum(self):
        return self.__sides__[self.__indexTop__]
    
    def getFrontSideNum(self):
        return self.__sides__[self.__indexFront__]
        
    def getRightSideNum(self):
        return self.__sides__[self.__indexRight__]
    
    def roll(self, direction):
        initTop = self.__indexTop__
        initFront = self.__indexFront__
        initRight = self.__indexRight__
        initLeft = self.__indexLeft__
        initBack = self.__indexBack__
        initBottom = self.__indexBottom__
        if direction == 'N':
            self.__indexTop__ = initFront
            self.__indexFront__ = initBottom
            self.__indexRight__ = initRight
            self.__indexLeft__ = initLeft
            self.__indexBack__ = initTop
            self.__indexBottom__ = initBack
        elif direction == 'E':
            self.__indexTop__ = initLeft
            self.__indexFront__ = initFront
            self.__indexRight__ = initTop
            self.__indexLeft__ = initBottom
            self.__indexBack__ = initBack
            self.__indexBottom__ = initRight
        elif direction == 'S':
            self.__indexTop__ = initBack
            self.__indexFront__ = initTop
            self.__indexRight__ = initRight
            self.__indexLeft__ = initLeft
            self.__indexBack__ = initBottom
            self.__indexBottom__ = initFront
        elif direction == 'W':
            self.__indexTop__ = initRight
            self.__indexFront__ = initFront
            self.__indexRight__ = initBottom
            self.__indexLeft__ = initTop
            self.__indexBack__ = initBack
            self.__indexBottom__ = initLeft
    
    def pivot(self, direction):
        initTop = self.__indexTop__
        initFront = self.__indexFront__
        initRight = self.__indexRight__
        initLeft = self.__indexLeft__
        initBack = self.__indexBack__
        initBottom = self.__indexBottom__
        if direction == 'C':
            self.__indexTop__ = initTop
            self.__indexFront__ = initRight
            self.__indexRight__ = initBack
            self.__indexLeft__ = initFront
            self.__indexBack__ = initLeft
            self.__indexBottom__ = initBottom
        elif direction == 'CC':
            self.__indexTop__ = initTop
            self.__indexFront__ = initLeft
            self.__indexRight__ = initFront
            self.__indexLeft__ = initBack
            self.__indexBack__ = initRight
            self.__indexBottom__ = initBottom

die = Dice([int(i) for i in input().split()])
q = int(input())
for i in range(q):
    top, front = (int(i) for i in input().split())
    while True:
        if die.getTopSideNum() == top:
            break
        die.roll('N')
        if die.getTopSideNum() == top:
            break
        die.roll('W')
    while True:
        if die.getFrontSideNum() == front:
            break
        die.pivot('C')
    print(die.getRightSideNum())

Submission status: Accepted

Solution code:
class Dice():
    
    def __init__(self,d1,d2,d3,d4,d5,d6):
        self.d1=d1
        self.d2=d2
        self.d3=d3
        self.d4=d4
        self.d5=d5
        self.d6=d6
    
    def roll(self,c):
        if c=='N':
            self.d1,self.d5,self.d6,self.d2=self.d2,self.d1,self.d5,self.d6
        elif c=='S':
            self.d1,self.d5,self.d6,self.d2=self.d5,self.d6,self.d2,self.d1
        elif c=='E':
            self.d1,self.d3,self.d6,self.d4=self.d4,self.d1,self.d3,self.d6
        else:
            self.d1,self.d3,self.d6,self.d4=self.d3,self.d6,self.d4,self.d1
    
a,b,c,d,e,f=map(int,input().split())
D=Dice(a,b,c,d,e,f)
n=int(input())
for i in range(n):
    up,forward=map(int,input().split())
    if up==D.d3:
        D.roll('W')
    elif up==D.d6:
        D.roll('E')
        D.roll('E')
    elif up==D.d4:
        D.roll('E')
    elif up==D.d2:
        D.roll('N')
    elif up==D.d5:
        D.roll('S')
    if forward==D.d2:
        print(D.d3)
    elif forward==D.d3:
        print(D.d5)
    elif forward==D.d5:
        print(D.d4)
    else:
        print(D.d2)

Submission status: Accepted

Solution code:
a,b,c,d,e,f=map(int,input().split())
d2={1:a,2:b,3:c,4:d,5:e,6:f}
d1={a:1,b:2,c:3,d:4,e:5,f:6}
a,b,c,d,e,f=1,2,3,4,5,6
ans=[[b,c,e,d,b],[a,d,f,c,a],[a,b,f,e,a],[a,e,f,b,a],[a,c,f,d,a],[b,d,e,c,b]]
for _ in range(int(input())):
    top,front=map(int,input().split())
    top,front=d1[top],d1[front]
    top-=1
    for i in range(len(ans[top])):
        if front==ans[top][i]:
            print(d2[ans[top][i+1]])
            break

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:
#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
    def get_Top(self):
        return self.number[0]

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(dice.query(top_num,front_num))

Submission status: Accepted

Solution code:
input_data = input().split(" ")
items = [int(cont) for cont in input_data]
keys = ['T', 'S', 'E', 'W', 'N', 'B']
dice = dict(zip(keys, items))

def q1():
    def rot_func_list(rot, dice):
        if rot == "N":
            keys = ['T', 'S', 'B', 'N']
            items = dice['S'], dice['B'], dice['N'], dice['T']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "S":
            keys = ['T', 'S', 'B', 'N']
            items = dice['N'], dice['T'], dice['S'], dice['B']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "E":
            keys = ['T', 'E', 'B', 'W']
            items = dice['W'], dice['T'], dice['E'], dice['B']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

        if rot == "W":
            keys = ['T', 'E', 'B', 'W']
            items = dice['E'], dice['B'], dice['W'], dice['T']
            new_dice = dict(zip(keys, items))
            dice.update(new_dice)

    controls = list(input())
    for i in controls:
        rot_func_list(i, dice)
    print(dice["T"])

def q2():

    def search_surf(conds, dice):
        a,b=conds
        a_key = [k for k, v in dice.items() if v == a ]
        b_key = [k for k, v in dice.items() if v == b ]
        key_list = a_key + b_key

        part_st = ''.join(key_list)

        def search(part_st):
            if part_st in "TNBST":
                return "W"

            if part_st in "TSBNT":
                return "E"

            if part_st in "TEBWT":
                return "N"

            if part_st in "TWBET":
                return "S"

            if part_st in "NESWN":
                return "B"

            if part_st in "NWSEN":
                return "T"


        target_key = search(part_st)
        print(dice[target_key])

    a = input()
    repeater = int(a)

    for neee in range(repeater):
        control_input = input().split(" ")
        conds = [int(i) for i in control_input]
        search_surf(conds, dice)

q2()

Submission status: Accepted

Solution code:
dct = {
    0: (0, 0, 1),
    5: (0, 0, -1),
    2: (0, 1, 0),
    3: (0, -1, 0),
    1: (1, 0, 0),
    4: (-1, 0, 0)
}

def calc(x, y):
    res = (
        x[1] * y[2] - x[2] * y[1],
        x[2] * y[0] - x[0] * y[2],
        x[0] * y[1] - x[1] * y[0]
    )
    return res

if __name__ == "__main__":
    a = [int(x) for x in input().split()]
    Q = int(input())

    for _ in range(Q):
        x, y = map(int, input().split())
        
        for i, value in enumerate(a):
            if x == value:
                x_ind = i
            elif y == value:
                y_ind = i
        
        s, t = dct[x_ind], dct[y_ind]

        res = calc(s,t)
        for key in dct:
            if dct[key] == res:
                print(a[key])
                break

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self):
        self.d = list(map(int, input().split()))

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

    def roll(self, direction):
        if direction == "E":
            self.setNum(
                self.d[3], self.d[1], self.d[0], self.d[5], self.d[4], self.d[2]
            )
        elif direction == "N":
            self.setNum(
                self.d[1], self.d[5], self.d[2], self.d[3], self.d[0], self.d[4]
            )
        elif direction == "S":
            self.setNum(
                self.d[4], self.d[0], self.d[2], self.d[3], self.d[5], self.d[1]
            )
        elif direction == "W":
            self.setNum(
                self.d[2], self.d[1], self.d[5], self.d[0], self.d[4], self.d[3]
            )

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

    def setTop(self, t):
        if self.d[0] == t:
            return
        elif self.d[1] == t:
            self.roll("N")
        elif self.d[2] == t:
            self.roll("W")
        elif self.d[3] == t:
            self.roll("E")
        elif self.d[4] == t:
            self.roll("S")
        elif self.d[5] == t:
            self.roll("N")
            self.roll("N")

    def getRight(self, f):
        if self.d[2] == f:
            return self.d[4]
        elif self.d[3] == f:
            return self.d[1]
        elif self.d[4] == f:
            return self.d[3]
        else:
            return self.d[2]


dice = Dice()
q = int(input())
for i in range(q):
    t, f = map(int, input().split())
    dice.setTop(t)
    print(dice.getRight(f))