Problem 90

completed February 16, 2012

Comments

Considering the number of combinations and permutations that needed to be considered in this problem, it’s pretty awesome that I got this to run in just 5 seconds.

What was toughest about this problem was taking into consideration all the different ways that two different digits could be aligned to make a number. The digits could be put in either order and 6’s could become 9’s and vice versa.

Then, I realized that I had counted everything twice because the two cubes could be reversed. I just dealt with that by dividing by 2.

Code

from itertools import *

def square(t):
	num = int(t[0])*10 + int(t[1])
	if num in [1, 4, 9, 16, 25, 36, 49, 64, 81]:
		return num
	elif num == 6:
		return 9
	elif num == 19:
		return 16
	elif num == 39:
		return 36
	elif num == 46:
		return 49
	
	num = int(t[1])*10 + int(t[0])
	if num in [1, 4, 9, 16, 25, 36, 49, 64, 81]:
		return num
	elif num == 6:
		return 9
	elif num == 19:
		return 16
	elif num == 39:
		return 36
	elif num == 46:
		return 49
		
	else:
		return False


count = 0
for cube1 in combinations(range(10),6):
	for cube2 in combinations(range(10),6):
		this_combo = set()
		for double in product(cube1,cube2):
			test = square(double)
			if test != False:
				this_combo.add(test)
				if test == 64:
					this_combo.add(49)
				elif test == 49:
					this_combo.add(64)
		if len(this_combo) == 9:
			count += 1

print count/2