Problem:
Dice IV Write a program which reads $n$ dices constructed in the same way as Dice I , and determines whether they are all different. For the determination, use the same way as Dice III . Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III . Output Print " Yes " if given dices are all different, otherwise " No " in a line. Constraints  $2 \leq n \leq 100$  $0 \leq $ the integer assigned to a face $ \leq 100$  Sample Input 1 3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1 Sample Output 1 No Sample Input 2 3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6 Sample Output 2 Yes

Solution (Python):
class Dice(object):
    """
    6??¢???????????????
    ?????¢???top, ?????¢???bottom???????????\??????4??¢??????????????? north, south, east, west ??¨?????¶???
    """
    def __init__(self, faces):
        
        
        self.top = faces[0]
        self.south = faces[1]
        self.east = faces[2]
        self.west = faces[3]
        self.north = faces[4]
        self.bottom = faces[5]
        self.values = (self.top, self.south, self.east, self.west, self.north, self.bottom)

    def rotate(self, dir):
        
        
        if dir == 'N':
            temp = self.top
            self.top = self.south
            self.south = self.bottom
            self.bottom = self.north
            self.north = temp
        elif dir == 'S':
            temp = self.top
            self.top = self.north
            self.north = self.bottom
            self.bottom = self.south
            self.south = temp
        elif dir == 'E':
            temp = self.top
            self.top = self.west
            self.west = self.bottom
            self.bottom = self.east
            self.east = temp
        elif dir == 'W':
            temp = self.top
            self.top = self.east
            self.east = self.bottom
            self.bottom = self.west
            self.west = temp

    def get_top_value(self):
        return self.top

    def get_bottom_value(self):
        return self.bottom

    def get_east_value(self):
        return self.east

    def get_west_value(self):
        return self.west

    def get_north_value(self):
        return self.north

    def get_south_value(self):
        return self.south

    def spin(self, count=1):
        """???????????????top??¨bottom?????????????????????????????????????????????????????????????¨??????????90????????¢?????????
        ?????????????????§???1?????????????????????????????§?????§?????°??????????????????
        """
        for i in range(count):
            temp = self.south
            self.south = self.east
            self.east = self.north
            self.north = self.west
            self.west = temp

    def place(self, upper, front):
        """ ?????¢(top)??¨??¨??¢(south)?????????????????????upper, front??¨???????????????????????????????????¢?????????
        ??????????????????????????¨??????????????????????????????????????¢?????????????????????????????????????????????????????´???????????????(ValueError)?????????????????????
        """
        
        if upper not in self.values or front not in self.values:
            raise ValueError
        if upper == self.top:
            pass
        elif upper == self.east:
            self.rotate('W')
        elif upper == self.west:
            self.rotate('E')
        elif upper == self.north:
            self.rotate('S')
        elif upper == self.south:
            self.rotate('N')
        else:
            self.rotate('N')
            self.rotate('N')
        
        
        
        count = 0
        while front != self.south:
            self.spin()
            count += 1
            if count > 3:
                raise ValueError

    def is_identical(self, dice):
        """ ????????¨???????????????????????????????§????????????£?????????????¢?????????? (?????¢???????????°??????????????????????????????)
        :param dice: ?????????????????????????????????
        :return: ???????§????????????????(True/False)
        """
        
        my_v = sorted(list(self.values))
        d_v = sorted(list(dice.values))
        if my_v != d_v:
            return False

        
        try:
            dice.place(self.get_top_value(), self.get_south_value())
        except ValueError:
            return False
        if self.get_east_value() != dice.get_east_value():
            return False
        self.spin()
        try:
            dice.place(self.get_top_value(), self.get_south_value())
        except ValueError:
            return False
        if self.get_east_value() != dice.get_east_value():
            return False
        self.spin()
        try:
            dice.place(self.get_top_value(), self.get_south_value())
        except ValueError:
            return False
        if self.get_east_value() != dice.get_east_value():
            return False
        self.spin()
        try:
            dice.place(self.get_top_value(), self.get_south_value())
        except ValueError:
            return False
        if self.get_east_value() != dice.get_east_value():
            return False

        return True


if __name__ == '__main__':
    from itertools import combinations

    
    num = int(input())
    dice_values = []
    for i in range(num):
        dice_values.append([int(x) for x in input().split(' ')])

    
    dice = []
    for dv in dice_values:
        dice.append(Dice(dv))

    
    duplicated = False
    combi = combinations(dice, 2)  
    for d1, d2 in combi:
        if d1.is_identical(d2):
            duplicated = True
            break

    if duplicated is False:
        print('Yes')
    else:
        print('No')

Solution (Python):
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
    def get_nums(self):   
        return {self.u,self.w,self.s,self.e,self.n,self.w,self.d}

def mk_dice():
    u,s,e,w,n,d=map(int,input().split())
    return Dice(u,w,s,e,n,d)
 
import random
q=int(input())
dice_col=[]
ans=True
for j in range(q):
    dice_b=mk_dice()
    for dice in dice_col:
        if(dice.get_nums()==dice_b.get_nums()):
            for i in range (1000):
                dice.rot(random.randint(0,2))
                if(dice.u==dice_b.u and dice.d==dice_b.d and dice.w==dice_b.w and dice.s==dice_b.s and dice.e==dice_b.e and dice.n==dice_b.n):
                    ans=False
    dice_col.append(dice_b)
 
if ans:
    print('Yes')
else:
    print('No')

Solution (Python):
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
    def get_nums(self):   
        return {self.u,self.w,self.s,self.e,self.n,self.w,self.d}

def mk_dice():
    u,s,e,w,n,d=map(int,input().split())
    return Dice(u,w,s,e,n,d)
 
import random
q=int(input())
dice_col=[]
ans=True
for j in range(q):
    dice_b=mk_dice()
    for dice in dice_col:
        if(dice.get_nums()==dice_b.get_nums()):
            for i in range (1000):
                dice.rot(random.randint(0,2))
                if(dice.u==dice_b.u and dice.d==dice_b.d and dice.w==dice_b.w and dice.s==dice_b.s and dice.e==dice_b.e and dice.n==dice_b.n):
                    ans=False
        if ~ans:
            break
    dice_col.append(dice_b)
 
if ans:
    print('Yes')
else:
    print('No')

Solution (Python):
from math import factorial
n = int(input())
dice = [list(map(int, input().split())) for _ in range(n)]
ans = [1 for _ in range(n)]
for i in range(1, n):
    j = len({v: k for k, v in enumerate(dice[i])})
    j = factorial(6) if j == 6 else factorial(6)//factorial(6-j)
    d = [v for v in dice[i]]
    tmp = {tuple(dice[i])}
    while j > 0:
        for h in "NWSE":
            if h == "N":
                tmp = tmp.union({(d[1],d[5],d[2],d[3],d[0],d[4])})
                d[0],d[1],d[2],d[3],d[4],d[5] = d[1],d[5],d[2],d[3],d[0],d[4]
            elif h == "S":
                tmp = tmp.union({(d[4],d[0],d[2],d[3],d[5],d[1])})
                d[0],d[1],d[2],d[3],d[4],d[5] = d[4],d[0],d[2],d[3],d[5],d[1]
            elif h == "E":
                tmp = tmp.union({(d[0],d[3],d[1],d[4],d[2],d[5])})
                d[0],d[1],d[2],d[3],d[4],d[5] = d[0],d[3],d[1],d[4],d[2],d[5]
            else:
                tmp = tmp.union({(d[0],d[2],d[4],d[1],d[3],d[5])})
                d[0],d[1],d[2],d[3],d[4],d[5] = d[0],d[2],d[4],d[1],d[3],d[5]
        if tuple(dice[0]) in tmp:
            ans[i] = 0
            break
        j -= 1
print("No" if 0 in ans else "Yes")

Solution (Python):
n=int(input())

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

	def roll(self,com):
		a1,a2,a3,a4,a5,a6=self.d
		if com=="E":
			self.d = [a4,a2,a1,a6,a5,a3]
		elif com=="W":
			self.d = [a3,a2,a6,a1,a5,a4]
		elif com=="S":
			self.d = [a5,a1,a3,a4,a6,a2]
		elif com=="N":
			self.d = [a2,a6,a3,a4,a1,a5]

import random
ds=[]
for i in range(n):
	ds.append(dice(list(map(int,input().split()))))
flag="Yes"
for i in range(n):
	for j in range(i):
		if sorted(ds[i].d)!=sorted(ds[j].d):
			continue
		else:
			c=["E","W","S","N"]
			n=0
			while n<1000:
				if ds[i].d==ds[j].d:
					break 
				else:
					ds[i].roll(c[random.randint(1,2)])
				n+=1
			if n==1000:
				pass
			else:
				flag="No"

print(flag)

Solution (Python):
class Dice:
    index_list_map = [[[-1],[2,3,4,5],[4,1,3,5],[1,4,2,5],[3,2,1,5],[-1]],
                      [[3,2,5,4],[-1],[0,5,3,4],[5,0,2,4],[-1],[2,3,0,4]],
                      [[1,4,5,3],[5,0,4,3],[-1],[-1],[0,5,1,3],[4,1,0,3]],
                      [[4,1,5,2],[0,5,4,2],[-1],[-1],[5,0,1,2],[1,4,0,2]],
                      [[2,3,5,1],[-1],[5,0,3,1],[0,5,2,1],[-1],[3,2,0,1]],
                      [[-1],[3,2,4,0],[1,4,3,0],[4,1,2,0],[2,3,1,0],[-1]]]

    def __init__(self,list):
        self.num = list

    def lookup_east_side(self,u,s):
        index_upside = u
        index_sside = s
        return self.num[self.index_list_map[index_upside][index_sside][0]]


    def lookup_west_side(self,u,s):
        index_upside = u
        index_sside = s
        return self.num[self.index_list_map[index_upside][index_sside][1]]

    def lookup_north_side(self,u,s):
        index_upside = u
        index_sside = s
        return self.num[self.index_list_map[index_upside][index_sside][2]]

    def lookup_down_side(self,u,s):
        index_upside = u
        index_sside = s
        return self.num[self.index_list_map[index_upside][index_sside][3]]

    def is_same(self,dice):
        index_u = [ i for i,x in enumerate(dice.num) if x == self.num[0]]
        index_s = [ i for i,x in enumerate(dice.num) if x == self.num[1]]

        combinations_u_s = [ [x,y] for x in index_u for y in index_s if x != y and (abs(x-2.5) != abs(y-2.5))]
        for comb in  combinations_u_s :
            e = dice.lookup_east_side(comb[0],comb[1])
            w = dice.lookup_west_side(comb[0],comb[1])
            n = dice.lookup_north_side(comb[0],comb[1])
            d = dice.lookup_down_side(comb[0],comb[1])
            if e == self.num[2] and w == self.num[3] and n == self.num[4] and d == self.num[5]:
                return True
        return False

n = input()
dices = []
for i in range(n):
  spots = map(int,raw_input().split())
  dices.append(Dice(spots))

flag = False
for i in range(len(dices)):
    for j in range(i+1, len(dices)):
        if dices[i].is_same(dices[j]):
            flag = True
            break
if flag:
    print "No"
else:
    print "Yes"

Solution (Python):
class Dice(object):
    def __init__(self,num):
        self.f=num[0]
        self.s=num[1]
        self.e=num[2]
        self.w=num[3]
        self.n=num[4]
        self.b=num[5]
    def create(self,order):
        if order=='N':
            return [self.s,self.b,self.e,self.w,self.f,self.n]
        elif order=='S':
            return [self.n,self.f,self.e,self.w,self.b,self.s]
        elif order=='E':
            return [self.w,self.s,self.f,self.b,self.n,self.e]
        else:
            return [self.e,self.s,self.b,self.f,self.n,self.w]
    def judge(self,dice_2):
        T={0:'N',1:'E',2:'S',3:'W'}
        m=[dice_2.s,dice_2.w,dice_2.n,dice_2.e,dice_2.f,dice_2.b]
        for i in range(6):
            if m[i]==self.f:
                if i==4:temp=dice_2
                elif i==5:temp=Dice(Dice(dice_2.create('N')).create('N'))
                else:temp=Dice(dice_2.create(T[i]))

                if self.b==temp.b:
                        side_1=''.join(list(map(str,[self.s,self.w,self.n,self.e])))*2
                        side_2=''.join(list(map(str,[temp.s,temp.w,temp.n,temp.e])))
                        if side_2 in side_1:return 'accordance'
        return 'discordance'
    @staticmethod
    def isdifferent(dices):
        for i in range(len(dices)):
            for j in range(i+1,len(dices)):
                if dices[i].judge(dices[j])=='accordance':return 'No'
        return 'Yes'

n=eval(input())
dices=[]
for i in range(n):
    dices.append(Dice(list(map(int,input().split()))))
print(Dice.isdifferent(dices))

Solution (Python):
class Dice:
    def __init__(self, init_values = ['1','2','3','4','5','6']):
        self.value = dict(zip(["top", "front","right","left","back","bottom"]\
                             , init_values))

    
    def roll(self, direction):
        if direction == 'N':
            self.value["top"], self.value["front"], self.value["bottom"], self.value["back"] = \
            self.value["front"], self.value["bottom"], self.value["back"], self.value["top"]
        elif direction == 'S':
            self.value["top"], self.value["back"], self.value["bottom"], self.value["front"] = \
            self.value["back"], self.value["bottom"], self.value["front"], self.value["top"]
        elif direction == 'E':
            self.value["top"], self.value["left"], self.value["bottom"], self.value["right"] = \
            self.value["left"], self.value["bottom"], self.value["right"], self.value["top"]
        elif direction == 'W':
            self.value["top"], self.value["right"], self.value["bottom"], self.value["left"] = \
            self.value["right"], self.value["bottom"], self.value["left"], self.value["top"]
        else:
            print("Error: illegal format.")

    
    def spin(self, direction):
        if direction == "CW":
            self.value["front"], self.value["right"], self.value["back"], self.value["left"] = \
            self.value["right"], self.value["back"], self.value["left"], self.value["front"]
        elif direction == "CCW":
            self.value["front"], self.value["left"], self.value["back"], self.value["right"] = \
            self.value["left"], self.value["back"], self.value["right"], self.value["front"]
        else:
            print("Error: illegal format.")

    def is_same(self, other_dice):
        for direction in 'SESESE':
            other_dice.roll(direction)
            if self.value["top"] == other_dice.value["top"]:
                for j in range(4):
                    other_dice.spin("CW")
                    if self.value == other_dice.value:
                        return True
        return False

n = int(input())
dice_arr = []
for i in range(n):
    dice_arr.append(Dice(input().split()))

result = "Yes"
for i in range(len(dice_arr)-1):
    for j in range(i+1,len(dice_arr)):
        if dice_arr[i].is_same(dice_arr[j]):
            result = "No"
            break
    if result == "No":
        break

print(result)

Solution (Python):

class Dice:
    def __init__(self, label):
        self.d = label

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

n = int(input())


cmd = "N" * 4 + ("W" + "N" * 4) * 3 + "NE" + "N" * 4 + "EE" + "N" * 4 

labels = []

for i in range(n):
    labels.append([int(j) for j in input().split()])

for i in range(n):
    flag = False

    for j in range(i + 1, n):
        label1 = labels[i]
        label2 = labels[j]

        dice1 = Dice(label1)
        dice2 = Dice(label2)

        for op in cmd:
            dice2.roll(op)

            if dice1.d == dice2.d:
                flag = True
                break

        if flag:
            break

    if flag:
        print("No")
        break
else:
    print("Yes")

Solution (Python):
class Dice(object):
    def __init__(self,num):
        self.n = num[4]
        self.e = num[2]
        self.w = num[3]
        self.s = num[1]
        self.center = num[0]
        self.back = num[5]

    def number(self):
        number = [self.center, self.s, self.e, self.w, self.n, self.back]
        return number

    def move(self,str):
        if str == 'N':
            x = self.n
            self.n = self.center
            self.center = self.s
            self.s = self.back
            self.back = x
            return self.center
        elif str == 'S':
            x = self.s
            self.s = self.center
            self.center = self.n
            self.n = self.back
            self.back = x
            return self.center
        elif str== 'E':
            x = self.e
            self.e = self.center
            self.center = self.w
            self.w = self.back
            self.back = x
            return self.center
        elif str == 'W':
            x = self.w
            self.w = self.center
            self.center = self.e
            self.e = self.back
            self.back = x
            return self.center


n = int(input())
dice = []
for i in range(n):
    dice.append([int(j) for j in input().split()])

bool = False
d = []


for dice1 in range(len(dice)-1):
    d1 = Dice(dice[dice1])
    for dice2 in range(len(dice)-1-dice1):
        d2 = Dice(dice[dice2+dice1+1])
        for i in range(4):
            for j in range(4):
                for k in range(4):
                    
                    if d1.number() == d2.number():
                        bool = True
                    else:
                        d1.move('N')
                        d1.move('E')
                        d1.move('S')
                d1.move('E')
            d1.move('N')

print('Yes') if bool == False else print('No')

Solution (Python):
class Dice:
    def __init__(self, men):
        self.men = men[:]

    def throw(self, direction):
        if direction == "E":
            pmen = self.men[:]
            self.men[0] = pmen[3]
            self.men[1] = pmen[1]
            self.men[2] = pmen[0]
            self.men[3] = pmen[5]
            self.men[4] = pmen[4]
            self.men[5] = pmen[2]
        elif direction == "W":
            pmen = self.men[:]
            self.men[0] = pmen[2]
            self.men[1] = pmen[1]
            self.men[2] = pmen[5]
            self.men[3] = pmen[0]
            self.men[4] = pmen[4]
            self.men[5] = pmen[3]
        elif direction == "S":
            pmen = self.men[:]
            self.men[0] = pmen[4]
            self.men[1] = pmen[0]
            self.men[2] = pmen[2]
            self.men[3] = pmen[3]
            self.men[4] = pmen[5]
            self.men[5] = pmen[1]
        elif direction == "N":
            pmen = self.men[:]
            self.men[0] = pmen[1]
            self.men[1] = pmen[5]
            self.men[2] = pmen[2]
            self.men[3] = pmen[3]
            self.men[4] = pmen[0]
            self.men[5] = pmen[4]
        elif direction == "C":
            pmen = self.men[:]
            self.men[0] = pmen[0]
            self.men[1] = pmen[2]
            self.men[2] = pmen[4]
            self.men[3] = pmen[1]
            self.men[4] = pmen[3]
            self.men[5] = pmen[5]
        elif direction == "U":
            pmen = self.men[:]
            self.men[0] = pmen[0]
            self.men[1] = pmen[3]
            self.men[2] = pmen[1]
            self.men[3] = pmen[4]
            self.men[4] = pmen[2]
            self.men[5] = pmen[5]

n = int(raw_input())
mens = []
for i in range(n):
    men = map(int,(raw_input()).split(" "))
    mens.append(men)

flag = False

for p in range(n-1):
    for q in range(p+1, n):
        d1 = Dice(mens[p][:])
        d2 = Dice(mens[q][:])

        for i in range(4):
            d1.throw("W")
            for j in range(4):
                d1.throw("S")
                if d1.men == d2.men:
                    flag = True

        for i in range(4):
            d1.throw("C")
            for j in range(4):
                d1.throw("S")
                if d1.men == d2.men:
                    flag = True
if flag:
    print "No"
else:
    print "Yes"

Solution (Python):
class Dice:

    __top = 0
    __front = 1
    __right = 2
    __left = 3
    __back = 4
    __bottom = 5

    def __init__(self, a, b, c, d, e, f):
        self.__dice = [a,b,c,d,e,f]

    def S(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__back]
        self.__dice[Dice.__front]  = dice_before[Dice.__top]
        self.__dice[Dice.__right]  = dice_before[Dice.__right]
        self.__dice[Dice.__left]   = dice_before[Dice.__left]
        self.__dice[Dice.__back]   = dice_before[Dice.__bottom]
        self.__dice[Dice.__bottom] = dice_before[Dice.__front]

    def E(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__left]
        self.__dice[Dice.__front]  = dice_before[Dice.__front]
        self.__dice[Dice.__right]  = dice_before[Dice.__top]
        self.__dice[Dice.__left]   = dice_before[Dice.__bottom]
        self.__dice[Dice.__back]   = dice_before[Dice.__back]
        self.__dice[Dice.__bottom] = dice_before[Dice.__right]

    def W(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__right]
        self.__dice[Dice.__front]  = dice_before[Dice.__front]
        self.__dice[Dice.__right]  = dice_before[Dice.__bottom]
        self.__dice[Dice.__left]   = dice_before[Dice.__top]
        self.__dice[Dice.__back]   = dice_before[Dice.__back]
        self.__dice[Dice.__bottom] = dice_before[Dice.__left]

    def N(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__front]
        self.__dice[Dice.__front]  = dice_before[Dice.__bottom]
        self.__dice[Dice.__right]  = dice_before[Dice.__right]
        self.__dice[Dice.__left]   = dice_before[Dice.__left]
        self.__dice[Dice.__back]   = dice_before[Dice.__top]
        self.__dice[Dice.__bottom] = dice_before[Dice.__back]

    def turn(self):
        dice_before = self.__dice[:]
        self.__dice[Dice.__top]    = dice_before[Dice.__top]
        self.__dice[Dice.__front]  = dice_before[Dice.__right]
        self.__dice[Dice.__right]  = dice_before[Dice.__back]
        self.__dice[Dice.__left]   = dice_before[Dice.__front]
        self.__dice[Dice.__back]   = dice_before[Dice.__left]
        self.__dice[Dice.__bottom] = dice_before[Dice.__bottom]

    def top(self):
        return(self.__dice[Dice.__top])

    def front(self):
        return(self.__dice[Dice.__front])

    def right(self):
        return(self.__dice[Dice.__right])

    def left(self):
        return(self.__dice[Dice.__left])

    def back(self):
        return(self.__dice[Dice.__back])

    def bottom(self):
        return(self.__dice[Dice.__bottom])

    def compare(self, dice):
        if (self.__dice[Dice.__top]    == dice.__dice[Dice.__top] and
            self.__dice[Dice.__front]  == dice.__dice[Dice.__front] and
            self.__dice[Dice.__right]  == dice.__dice[Dice.__right] and
            self.__dice[Dice.__left]   == dice.__dice[Dice.__left] and
            self.__dice[Dice.__back]   == dice.__dice[Dice.__back] and
            self.__dice[Dice.__bottom] == dice.__dice[Dice.__bottom]):
            return("Match")
        else:
            return("Unmatch")

    def print(self):
        print('[top   ] :', self.__dice[Dice.__top])
        print('[front ] :', self.__dice[Dice.__front])
        print('[right ] :', self.__dice[Dice.__right])
        print('[left  ] :', self.__dice[Dice.__left])
        print('[back  ] :', self.__dice[Dice.__back])
        print('[bottom] :', self.__dice[Dice.__bottom])


def S(dice, top):
    for t in range(4):
        if dice.top() == top:
            return dice
        dice.S()
    return dice

def turn(dice, front):
    for t in range(4):
        if dice.front() == front:
            return dice
        dice.turn()
    return dice


n = int(input())
dice = []

a, b, c, d, e, f = input().split()
top = max(a,b,c,d,e,f)
dice1 = Dice(a, b, c, d, e, f)
dice1 = S(dice1, top)

for i in range(n - 1):
    a, b, c, d, e, f = input().split()
    dice.append(Dice(a, b, c, d, e, f))

for i in range(len(dice)):
    for to in range(2):
        dice[i] = S(dice[i], top)
        if dice[i].top() == top:
            break
        else:
            dice[i].turn()

    front = dice1.front()
    for fr in range(4):
        dice[i] = turn(dice[i], front)
        if dice[i].front() == front:
            break

    if (dice1.compare(dice[i]) == 'Match'):
        print("No")
        exit()

print("Yes")

Solution (Python):
def move(d, r):
    if r == 'N':
        return (d[1], d[5], d[2], d[3], d[0], d[4])
    if r == 'E':
        return (d[3], d[1], d[0], d[5], d[4], d[2])
    if r == 'W':
        return (d[2], d[1], d[5], d[0], d[4], d[3])
    if r == 'S':
        return (d[4], d[0], d[2], d[3], d[5], d[1])
    return d
     
def gen_all(dice_1):
    dice = {dice_1}
    while True:
        dice2 = {move(d,r) for d in dice for r in list("NEWS")}.union(dice)
        if dice == dice2: break
        dice = dice2
    return dice    


n = int(input())
count = 0
dice = [0 for i in range(n)]


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

for i in range(n):
    for k in range(i):
        if count == 1: break
        if dice[i] in gen_all(dice[k]):
            print("No")
            count = 1
            break
if count == 0:
    print("Yes")

Solution (Python):
n = int(input())

def rot(dice,rotation):
    if rotation == "E":
        dice = (dice[3],dice[1],dice[0],dice[5],dice[4],dice[2])
    elif rotation == "W":
        dice = (dice[2],dice[1],dice[5],dice[0],dice[4],dice[3])
    elif rotation == "N":
        dice = (dice[1],dice[5],dice[2],dice[3],dice[0],dice[4])
    elif rotation == "S":
        dice = (dice[4],dice[0],dice[2],dice[3],dice[5],dice[1])
    return dice

def make_all_pattern(dice):
    pattern = {dice}
    pattern2 = pattern
    while True:
        pattern2 = pattern.union({rot(d,r) for d in pattern for r in list("NEWS")})
        if pattern2 == pattern:
            break
        pattern = pattern2
    return pattern

dice = [0 for i in range(n)]
for i in range(n):
    dice[i] = tuple(map(int,input().split()))


pattern = set()
for i in range(n-1):
    pattern = pattern.union(make_all_pattern(dice[i]))
    if dice[i+1] in pattern:
        print("No")
        break

else:
    print("Yes")