@MrAuer/

Subtraction Towers Solver

Python 2.7

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#Solves subtraction pyramids recursively

import itertools

#How many rows in the tower the program is looking for
#This now runs okay up to 5 lines on this website
lines = 4

#Calculates the largest number that can be used (ex: 3 in 2-row tower)
largest = lines * (lines + 1) / 2

def evalTopRow(tuple_of_top_row):
	final_tuple = tuple_of_top_row
	current_row = tuple_of_top_row
	while len(current_row) > 1:
		new_row = ()
		for k in range(1,len(current_row)):
			new_row += (abs(current_row[k] - current_row[k-1]),)
		final_tuple += new_row
		current_row = new_row
	return final_tuple

def check(tuple_of_answers):
	"""
	Checks to make sure tuple of answers is legit
	"""
	state = True
	for k in itertools.combinations(tuple_of_answers,2):
		if k[0] == k[1] or k[0] > max or k[1] > largest:
			state = False
	return state

def printSolution(tuple_of_answers, line_length):
	"""
	Prints a good solution in a reasonable way
	"""
	if len(tuple_of_answers) > 0:
		print tuple_of_answers[0:line_length]
		tuple_of_answers = tuple_of_answers[line_length:]
		printSolution(tuple_of_answers, line_length - 1)
		
print "Looking for solutions that are " + str(lines) + ' lines long.'
for k in itertools.permutations(range(1,1+largest),lines):
	if check(evalTopRow(k)) == True:
		printSolution(evalTopRow(k),lines)
		print ""
		print ""