Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Numbered Triangles by Moff
from itertools import permutations
class Solver(object):
def __init__(self, triangles):
self.max_score = 0
self.limit = sum(max(t) for t in triangles)
for p in permutations(triangles[0]):
self.find_path([p], triangles[1:])
def check(self, path):
if path[0][0] == path[-1][-1]:
self.max_score = max(self.max_score, sum(t[1] for t in path))
def find_path(self, path, rest):
if self.max_score == self.limit:
return
if not rest:
self.check(path)
t1 = path[-1]
for i, t in enumerate(rest):
for ct in permutations(t):
if ct[0] == t1[2]:
self.find_path(path + [ct], rest[:i] + rest[i+1:])
def checkio(triangles):
return Solver(triangles).max_score
Aug. 13, 2015