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)))])
# If all the pieces are placed check if the last one can be placed (fonction of the first one).
if r ==  and p[-1] == p:
# Return the sum of the exteriors side.
return sum([p[i] 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] in item:
# If it is get all the possible piece position.
a = [it for j, it in enumerate(list(perm(item))) if it == p[-1]]
# 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
def checkio(chips): return triangle(chips, )
Oct. 8, 2013