Problem:


立方体の作品

  芸術家品川は n 点の作品を出展するように依頼されました。そこで、立方体の 6 面をペンキで色付けしたものを作品として出展することにしました。作品は、Red、Yellow、Blue、Magenta、Green、Cyan の 6 色全てが使われていて、各面は 1 色で塗りつぶされています。品川は形が同じ立方体の作品でも色の配置の仕方を変え、異なる作品として n 点作成しました。

 彼の友人であるあなたは、友人のよしみで作品を出展前に閲覧させてもらいましたが、そこであることに気が付きました。それらの作品の中に異なる色付けをされているように見えても、実は同じ色の組み合わせをした立方体が存在していたのです。このままでは、 n 点の作品を出展することできなくなってしまいます。

 作成した作品の数と各作品の色の情報を入力とし、出展するためにあと何点必要かを出力するプログラムを作成してください。

 立方体の各面の色は c1 から c6 までの記号で表され、 以下のような配置となっています。また、c1 から c6 のそれぞれは Red、Yellow、Blue、Magenta、Green、Cyan のいずれか 1 色となります。

Input

複数のデータセットの並びが入力として与えられます。 入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

n
cube1
cube2
:
cuben

１行目に作品の数 n (1 ≤ n ≤ 30)、続く n 行に i 番目の作品の情報が与えられます。各作品の情報は次の形式で与えられます。

c1 c2 c3 c4 c5 c6

作品の色の配置 ci が空白区切りで与えられます。

データセットの数は 100 を超えません。

Output

データセットごとに、出展するのにあと何点の作品が必要かを１行に出力します。

Sample Input

3
Cyan Yellow Red Magenta Green Blue
Cyan Yellow Red Magenta Green Blue
Red Yellow Magenta Blue Green Cyan
4
Red Magenta Blue Green Yellow Cyan
Red Yellow Magenta Blue Green Cyan
Magenta Green Red Cyan Yellow Blue
Cyan Green Yellow Blue Magenta Red
0

Output for the Sample Input

1
1



Sample input:
3
Cyan Yellow Red Magenta Green Blue
Cyan Yellow Red Magenta Green Blue
Red Yellow Magenta Blue Green Cyan
4
Red Magenta Blue Green Yellow Cyan
Red Yellow Magenta Blue Green Cyan
Magenta Green Red Cyan Yellow Blue
Cyan Green Yellow Blue Magenta Red
0


Sample output:
1
1

Submission status: Wrong Answer

Solution code:
roll = [
(0,1,2,3,4,5),
(3,0,2,5,4,1),
(4,1,0,3,5,2),
(1,5,2,0,4,3),
(2,1,5,3,0,4),
(5,3,2,1,4,0)]

def zero_move_to_left(xi):
    r = roll[xi[0]]
    return [r[i] for i in xi]

def sort_middle_face(xi):
    m = xi[1:-1] 
    min_m = min(m)
    while m[0] != min_m:
        m = m[1:] + m[:1]
    return xi[:1] + m + xi[-1:]

import sys
f = sys.stdin


str2i = {'Blue':0, 'Cyan':1, 'Green':2, 'Magenta':3, 'Red':4, 'Yellow':5}

import collections
while True:
    n = int(f.readline())
    if n == 0:
        break
    c = collections.Counter()
    for i in range(n):
        xi = [str2i[w] for w in f.readline().split()]
        xi = zero_move_to_left(xi)
        xi = sort_middle_face(xi)
        c[tuple(xi)] += 1
    print(n - len(c))

Submission status: Wrong Answer

Solution code:
roll = [
(0,1,2,4,3,5),
(4,0,2,5,3,1),
(3,1,0,4,5,2),
(1,5,2,0,3,4),
(2,1,5,4,0,3),
(5,4,2,1,3,0)]

def zero_move_to_left(xi):
    r = roll[xi[0]]
    return [r[i] for i in xi]

def sort_middle_face(xi):
    m = xi[1:-1] 
    min_m = min(m)
    while m[0] != min_m:
        m = [m[1],m[3],m[0],m[2]]
    return xi[:1] + m + xi[-1:]

import sys
f = sys.stdin


str2i = {'Blue':0, 'Cyan':1, 'Green':2, 'Magenta':3, 'Red':4, 'Yellow':5}

import collections
while True:
    n = int(f.readline())
    if n == 0:
        break
    c = collections.Counter()
    for i in range(n):
        xi = [str2i[w] for w in f.readline().split()]
        xi = zero_move_to_left(xi)
        xi = sort_middle_face(xi)
        c[tuple(xi)] += 1
    print(n - len(c))

Submission status: Wrong Answer

Solution code:
def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

def equal(A,B):
    temp=B[:]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                roll(0,2,5,3,temp)
                if A==temp:return True
            roll(0,1,5,4,temp)
            if A==temp:return True
        roll(1,2,4,3,temp)
        if A==temp:return True
    return False


while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            if equal(L[i],L[j]):
                cnt+=1
    print cnt

Submission status: Wrong Answer

Solution code:
def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

def equal(A,B):
    temp=B[:]
    for i in range(4):
        for j in range(4):
            for k in range(4):
                roll(0,2,5,3,temp)
                if A==temp:return True
            roll(0,1,5,4,temp)
            if A==temp:return True
        roll(1,2,4,3,temp)
        if A==temp:return True
    return False


while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            if equal(L[i],L[j]):
                cnt+=1
    print cnt

Submission status: Wrong Answer

Solution code:
def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

def equal(A,B):
    temp=B[:]
    for i in range(4):
        print temp
        for j in range(4):
            for k in range(4):
                roll(0,2,5,3,temp)
                if A==temp:return True
            roll(0,1,5,4,temp)
        roll(1,2,4,3,temp)
    return False


while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            if equal(L[i],L[j]):
                cnt+=1
    print cnt

Submission status: Wrong Answer

Solution code:
def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

def equal(A,B):
    for i in range(6):
        for j in range(4):
            if A==B:return True
            roll(1,2,4,3,A)
        if i%2==0:
            roll(0,2,5,3,A)
        else:
            roll(0,1,5,4,A)
    return False

while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            if equal(L[i],L[j]):
                cnt+=1
    print cnt

Submission status: Wrong Answer

Solution code:
def equal(B):
    temp=B[:]
    S=set()
    for i in range(4):
        for j in range(4):
            for k in range(4):
                roll(0,2,5,3,temp)
                S.add(tuple(temp))
            roll(0,1,5,4,temp)
        roll(1,2,4,3,temp)
    return S

def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            S=equal(L[j])
            if tuple(L[i]) in S:
                cnt+=1
    print cnt

Submission status: Accepted

Solution code:
roll = [
(0,1,2,3,4,5),
(1,5,2,3,0,4),
(2,1,5,0,4,3),
(3,1,0,5,4,2),
(4,0,2,3,5,1),
(5,4,2,3,1,0)]

def zero_move_to_left(xi):
    r = roll[xi.index(0)]
    return [xi[ri] for ri in r]

def sort_middle_face(xi):
    m = xi[1:-1] 
    min_m = min(m)
    while m[0] != min_m:
        m = [m[1],m[3],m[0],m[2]]
    return xi[:1] + m + xi[-1:]

import sys
f = sys.stdin


str2i = {'Blue':0, 'Cyan':1, 'Green':2, 'Magenta':3, 'Red':4, 'Yellow':5}

import collections
while True:
    n = int(f.readline())
    if n == 0:
        break
    c = collections.Counter()
    for i in range(n):
        xi = [str2i[w] for w in f.readline().split()]
        xi = zero_move_to_left(xi)
        xi = sort_middle_face(xi)
        c[tuple(xi)] += 1
    print(n - len(c))

Submission status: Accepted

Solution code:
def to_top_red(cube):
  c1, c2, c3, c4, c5, c6 = cube
  if c2 == "Red":
    c1, c2, c6, c5 = c2, c6, c5, c1
  elif c3 == "Red":
    c1, c4, c6, c3 = c3, c1, c4, c6
  elif c4 == "Red":
    c1, c4, c6, c3 = c4, c6, c3, c1
  elif c5 == "Red":
    c1, c5, c6, c2 = c5, c6, c2, c1
  elif c6 == "Red":
    c1, c2, c6, c5 = c6, c5, c1, c2
  return (c1, c2, c3, c4, c5, c6)

def regist(cube, cube_dic):
  c1, c2, c3, c4, c5, c6 = cube
  cube_dic[(c1, c2, c3, c4, c5, c6)] = True
  cube_dic[(c1, c4, c2, c5, c3, c6)] = True
  cube_dic[(c1, c3, c5, c2, c4, c6)] = True
  cube_dic[(c1, c5, c4, c3, c2, c6)] = True

while True:
  n = int(input())
  if n == 0:
    break

  cube_dic = {}
  ans = 0
  
  for _ in range(n):
    cube = to_top_red(input().split())
    if cube in cube_dic:
      ans += 1
    else:
      regist(cube, cube_dic)
  
  print(ans)

Submission status: Accepted

Solution code:
# AOJ 0198 Trouble in Shinagawa's Artifacts
# Python3 2018.6.20 bal4u

r = [ \
	[0,1,2,3,4,5,6], [0,1,3,5,2,4,6], [0,1,4,2,5,3,6], [0,1,5,4,3,2,6], \
	[0,2,6,3,4,1,5], [0,2,3,1,6,4,5], [0,2,1,4,3,6,5], [0,2,4,6,1,3,5], \
	[0,3,1,2,5,6,4], [0,3,2,6,1,5,4], [0,3,5,1,6,2,4], [0,3,6,5,2,1,4], \
	[0,4,1,5,2,6,3], [0,4,2,1,6,5,3], [0,4,5,6,1,2,3], [0,4,6,2,5,1,3], \
	[0,5,1,3,4,6,2], [0,5,3,6,1,4,2], [0,5,4,1,6,3,2], [0,5,6,4,3,1,2], \
	[0,6,2,4,3,5,1], [0,6,3,2,5,4,1], [0,6,5,3,4,2,1], [0,6,4,5,2,3,1]]

def same(x, y):
	t = ['']*7
	for i in range(24):
		for j in range(1, 7): t[j] = a[y][r[i][j]]
		if a[x] == t: return True
	return False

while True:
	n = int(input())
	if n == 0: break
	f = [0]*n
	a = [['' for j in range(7)] for i in range(n)]
	for i in range(n):
		s = list(input().split())
		for j in range(6): a[i][j+1] = s[j]
	for i in range(n):
		if f[i] == 1: continue
		for j in range(i+1, n):
			if same(i, j): f[j] = 1
	print(sum(f))

Submission status: Accepted

Solution code:
def equal(B):
    temp=B[:]
    S=set()
    for i in range(4):
        for j in range(4):
            for k in range(4):
                roll(0,2,5,3,temp)
                S.add(tuple(temp))
                L.append(temp)
            roll(0,1,5,4,temp)
        roll(1,2,4,3,temp)
    return S

def roll(i,j,k,l,temp):
    t=temp[i]
    temp[i]=temp[j]
    temp[j]=temp[k]
    temp[k]=temp[l]
    temp[l]=t

while True:
    n=input()
    if n==0:break
    L=[raw_input().split() for i in range(n)]
    cnt=0
    for i in range(n-1):
        for j in range(i+1,n):
            S=equal(L[j])
            if tuple(L[i]) in S:
                cnt+=1
                break
    print cnt

Submission status: Accepted

Solution code:
N = ((4,2,1,3),(0,2,5,3),(4,5,1,0),(4,0,1,5),(5,2,0,3),(4,3,1,2))
while True:
    n = input()
    if n == 0:
        break
    s = set()
    for _ in xrange(n):
        L = raw_input().split()
        M = [L[i] for i in N[L.index("Red")]]
        if "Blue" in M:
            i = M.index("Blue")
        else:
            i = M.index("Cyan")
        s.add(tuple(M[i:] + M[:i]))
    print n - len(s)

Submission status: Accepted

Solution code:
class Dice:
    same_dice_index = ((0, 1, 2, 3, 4, 5), (0, 2, 4, 1, 3, 5), (0, 3, 1, 4, 2, 5),
                       (0, 4, 3, 2, 1, 5), (1, 0, 3, 2, 5, 4), (1, 2, 0, 5, 3, 4),
                       (1, 3, 5, 0, 2, 4), (1, 5, 2, 3, 0, 4), (2, 0, 1, 4, 5, 3),
                       (2, 1, 5, 0, 4, 3), (2, 4, 0, 5, 1, 3), (2, 5, 4, 1, 0, 3),
                       (3, 0, 4, 1, 5, 2), (3, 1, 0, 5, 4, 2), (3, 4, 5, 0, 1, 2),
                       (3, 5, 1, 4, 0, 2), (4, 0, 2, 3, 5, 1), (4, 2, 5, 0, 3, 1),
                       (4, 3, 0, 5, 2, 1), (4, 5, 3, 2, 0, 1), (5, 1, 3, 2, 4, 0),
                       (5, 2, 1, 4, 3, 0), (5, 3, 4, 1, 2, 0), (5, 4, 2, 3, 1, 0))

    def __init__(self, str_list):
        self.faces = str_list

    def is_same(self, dice):
        for i in Dice.same_dice_index:
            for n, j in enumerate(i):
                if dice.faces[n] != self.faces[j]:
                    break
            else:
                return True
        return False

    def is_unique(self, dices_list):
        for i in dices_list:
            if self.is_same(i):
                return False
        return True

def solve():
    from sys import stdin
    f_i = stdin
    
    ans_list = []
    
    while True:
        n = int(f_i.readline())
        
        if n == 0:
            break
        
        dices = [Dice(f_i.readline().split()) for i in range(n)]
        
        ans = 0
        while dices:
            tmp_dices = []
            d1 = dices.pop()
            for d2 in dices:
                if d1.is_same(d2):
                    ans += 1
                else:
                    tmp_dices.append(d2)
            dices = tmp_dices
        
        ans_list.append(ans)
    
    print('\n'.join(map(str, ans_list)))

solve()

Submission status: Accepted

Solution code:
import copy


class Dice:
    def __init__(self, a, b, c, d, e, f):
        self.top = a
        self.front = b
        self.right = c
        self.left = d
        self.behind = e
        self.bottom = f

    def S(self):
        self.top, self.front, self.behind, self.bottom = self.behind, self.top, self.bottom, self.front

    def N(self):
        self.behind, self.top, self.bottom, self.front = self.top, self.front, self.behind, self.bottom

    def E(self):
        self.top, self.bottom, self.left, self.right = self.left, self.right, self.bottom, self.top

    def W(self):
        self.left, self.right, self.bottom, self.top = self.top, self.bottom, self.left, self.right

    def spin(self):
        self.left, self.behind, self.right, self.front = self.front, self.left, self.behind, self.right

    def __eq__(self, _dice2):
        dice2 = copy.deepcopy(_dice2)
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.N()
        dice2.N()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.N()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.E()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.W()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.S()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        return False

    def _eqaul(self, dice2):
        return self.front == dice2.front and self.behind == dice2.behind and self.left == dice2.left and self.right == dice2.right and self.top == dice2.top and self.bottom == dice2.bottom

    def __repr__(self):
        return ",".join([str(object=i) for i in (self.top, self.front, self.right, self.left, self.behind, self.bottom)])


while True:
    n=int(input())
    if n==0:
        break
    dices=[]
    for i in range(n):
        c=input().split(" ")
        dice=Dice(*c)
        if (dice in dices)==False:
            dices.append(dice)
    print(n-len(dices))

Submission status: Accepted

Solution code:
D = [
    (1, 5, 2, 3, 0, 4), # 'U'
    (3, 1, 0, 5, 4, 2), # 'R'
    (4, 0, 2, 3, 5, 1), # 'D'
    (2, 1, 5, 0, 4, 3), # 'L'
]
p_dice = (0, 0, 0, 1, 1, 2, 2, 3)*3

def rotate_dice(L0):
    L = L0[:]
    for k in p_dice:
        yield L
        L[:] = (L[e] for e in D[k])

while 1:
    N = int(input())
    if N == 0:
        break
    res = []
    for i in range(N):
        cube = input().split()
        for dice in rotate_dice(cube):
            if dice in res:
                break
        else:
            res.append(cube)
    print(N - len(res))