Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive solution in Uncategorized category for Numbered Triangles by Amachua
from itertools import permutations as perm
# 0 /\ 1 -> Must be equals the 0 side of his neighboor
# /__\
# 2 -> exterior side needed for the sum
def triangle(r, p):
# If it's the first call, place the first piece in all possible positions.
if p == []:
return max([triangle(r[1:], [item]) for i, item in enumerate(list(perm(r[0])))])
# If all the pieces are placed check if the last one can be placed (fonction of the first one).
if r == [] and p[-1][0] == p[0][1]:
# Return the sum of the exteriors side.
return sum([p[i][2] for i in range(len(p))])
result = 0
for i, item in enumerate(r):
# Check if the side 0 of the previous piece is in the current piece.
if p[-1][0] in item:
# If it is get all the possible piece position.
a = [it for j, it in enumerate(list(perm(item))) if it[1] == p[-1][0]]
# Check those positions and update the result.
for j, item in enumerate(a):
tmp = triangle(r[:i]+r[i+1:], p+[item]) if i < len(r)-1 else triangle(r[:i], p+[item])
result = result if tmp < result else tmp
return result
def checkio(chips): return triangle(chips, [])
Oct. 8, 2013
Comments: