Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.

When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly.


-----Input-----

Each of the eight lines contains three space-separated integers — the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 10^6 in their absolute value.


-----Output-----

If there is a way to restore the cube, then print in the first line "YES". In each of the next eight lines print three integers — the restored coordinates of the points. The numbers in the i-th output line must be a permutation of the numbers in i-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them.

If there is no valid way, print "NO" (without the quotes) in the first line. Do not print anything else.


-----Examples-----
Input
0 0 0
0 0 1
0 0 1
0 0 1
0 1 1
0 1 1
0 1 1
1 1 1

Output
YES
0 0 0
0 0 1
0 1 0
1 0 0
0 1 1
1 0 1
1 1 0
1 1 1

Input
0 0 0
0 0 0
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
1 1 1

Output
NO

Difficulty: HARD
Tags: Geometry

Complete search
Skill types: Complete search

Starter code:

Solutions:
import itertools
from itertools import permutations as perm
import copy

def getPerm(it, index):
	if index == 0:
		return it
	elif index == 1:
		return [it[0], it[2], it[1]]
	elif index == 2:
		return [it[1], it[0], it[2]]
	elif index == 3:
		return [it[1], it[2], it[0]]
	elif index == 4:
		return [it[2], it[0], it[1]]
	elif index == 5:
		return [it[2], it[1], it[0]]

def distance(coord1, coord2):
	return (coord2[0] - coord1[0]) ** 2 + (coord2[1] - coord1[1]) ** 2 + (coord2[2] - coord1[2]) ** 2
dists = [0] * 6 * 7
pointList = [[int(x) for x in input().split()] for y in range(0, 8)]
p0 = pointList[0]
for x in range(0, 7):
	y = 0
	for pt in perm(pointList[x + 1], 3):
		dists[x * 6 + y] = distance(p0, pt)
		y += 1
done = False
final = None

def same(it1, it2):
	if list(it1) == list(it2):
		return True
	return False

def checkNotSame(indicesSoFar, newIndex):
	nonlocal pointList
	ptToComp = getPerm(pointList[len(indicesSoFar) + 1], newIndex)
	for c in range(len(indicesSoFar)):
		if same(getPerm(pointList[c + 1], indicesSoFar[c]), ptToComp):
			return False
	return True

def checkCompatible(a, b):
	if a == b or a * 2 == b or a == 2 * b or (a * 3 == b) or (a == 3 * b) or (2 * a == 3 * b) or (3 * a == 2 * b):
		return True
	return False

def getSeven(distList, soFar, index):
	nonlocal done
	nonlocal final
	if done == True:
		return
	if index == 7:
		distsSoFar = [dists[i * 6 + soFar[i]] for i in range(0, 7)]
		lowest = min(distsSoFar)
		lows = 0
		twos = 0
		threes = 0
		for a in range(0, 7):
			if distsSoFar[a] == lowest:
				lows += 1
			elif distsSoFar[a] == lowest * 2:
				twos += 1
			elif distsSoFar[a] == lowest * 3:
				threes += 1
		if lows == 3 and twos == 3 and (threes == 1):
			done = True
			final = soFar
		return
	for x in range(0, 6):
		if done == True:
			return
		if soFar == []:
			curList = copy.copy(soFar)
			curList.append(x)
			getSeven(distList, curList, index + 1)
		elif checkCompatible(distList[x + index * 6], distList[soFar[index - 1] + (index - 1) * 6]):
			if checkNotSame(soFar, x):
				curList = copy.copy(soFar)
				curList.append(x)
				getSeven(distList, curList, index + 1)
getSeven(dists, [], 0)
if final is None:
	print('NO')
else:
	print('YES')
	print(p0[0], p0[1], p0[2])
	for b in range(0, 7):
		(x, y, z) = getPerm(pointList[b + 1], final[b])
		print(x, y, z)


import itertools
from itertools import permutations as perm
l = [[int(x) for x in input().split()] for i in range(8)]

def dist2(p0, p1):
	return sum([(p0[i] - p1[i]) ** 2 for i in range(3)])

def sub(p0, p1):
	return [p0[i] - p1[i] for i in range(3)]

def add(p0, p1):
	return [p0[i] + p1[i] for i in range(3)]

def div(p0, x):
	return [p0[i] // x for i in range(3)]

def cross(p0, p1):
	return [p0[(i + 1) % 3] * p1[(i + 2) % 3] - p0[(i + 2) % 3] * p1[(i + 1) % 3] for i in range(3)]

def match(p0, p1):
	return sorted(p0) == sorted(p1)
solved = False
for l2 in perm(l, 3):
	p0 = l2[0]
	for p1 in perm(l2[1]):
		s2 = dist2(p0, p1)
		if s2 == 0:
			continue
		s = round(s2 ** 0.5)
		for p2 in perm(l2[2]):
			if dist2(p0, p2) != s2 or dist2(p1, p2) != 2 * s2:
				continue
			p3 = sub(add(p1, p2), p0)
			x = div(cross(sub(p1, p0), sub(p2, p0)), s)
			(p4, p5, p6, p7) = (add(p0, x), add(p1, x), add(p2, x), add(p3, x))
			l3 = [p0, p1, p2, p3, p4, p5, p6, p7]
			if sorted([sorted(p) for p in l]) == sorted([sorted(p) for p in l3]):
				print('YES')
				used = [False for i in range(8)]
				for p in l:
					for i in range(8):
						if used[i]:
							continue
						if match(p, l3[i]):
							print(l3[i][0], l3[i][1], l3[i][2])
							used[i] = True
							break
				solved = True
				break
		if solved:
			break
	if solved:
		break
if not solved:
	print('NO')


from itertools import permutations as p

def razn(a, b):
	return (a[0] - b[0], a[1] - b[1], a[2] - b[2])

def sp(a, b):
	return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]

def su(a, b):
	return (a[0] + b[0], a[1] + b[1], a[2] + b[2])
a = list((tuple(map(int, input().split())) for i in range(8)))
cp = sorted((sorted(el) for el in a))
for p1 in p(a[0]):
	for i in range(1, 8):
		for j in range(i + 1, 8):
			for l in range(j + 1, 8):
				for p2 in p(a[i]):
					for p3 in p(a[j]):
						for p4 in p(a[l]):
							s2 = razn(p2, p1)
							s3 = razn(p3, p1)
							s4 = razn(p4, p1)
							le = sp(s2, s2)
							if le and sp(s3, s3) == le and (sp(s4, s4) == le) and (sp(s2, s3) == 0) and (sp(s2, s4) == 0) and (sp(s3, s4) == 0):
								mass = [su(su(s3, s4), p1), su(su(s3, s2), p1), su(su(s2, s4), p1), p1, p2, p3, p4, su(su(su(s3, s4), p1), s2)]
								if sorted((sorted(el) for el in mass)) == cp:
									print('YES')
									for el in a:
										tmp = sorted(el)
										for kk in range(8):
											if sorted(mass[kk]) == tmp:
												print(mass[kk][0], mass[kk][1], mass[kk][2])
												mass[kk] = ()
												break
									exit()
print('NO')


from itertools import permutations as p
d = lambda a, b: sum(((i - j) ** 2 for (i, j) in zip(a, b)))
f = lambda a, b: [i + j - k for (i, j, k) in zip(a, b, q)]
g = lambda t: sorted((sorted(q) for q in t))
v = [sorted(map(int, input().split())) for i in range(8)]
q = v.pop()

def yes(t, v):
	print('YES')
	d = [str(sorted(i)) for i in t]
	for j in v:
		i = d.index(str(j))
		q = t.pop(i)
		print(*q)
		d.pop(i)
	exit()
u = g(v)
for (a, b, c) in p(v, 3):
	for x in p(a):
		s = 2 * d(q, x)
		if not s:
			continue
		for y in p(b):
			if not 2 * d(q, y) == d(x, y) == s:
				continue
			for z in p(c):
				if not 2 * d(q, z) == d(x, z) == d(y, z) == s:
					continue
				t = [x, y, z] + [f(x, y), f(x, z), f(y, z), f(f(x, y), z)]
				if g(t) == u:
					yes(t + [q], v + [q])
print('NO')