Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple recursion solution in Clear category for Numbered Triangles by nickie
def remove(s, x):
i = s.index(x)
return s[0:i] + s[i+1:]
def best(options):
successful = [x for x in options if x != None]
return min(successful) if successful else None
def search(end, outgoing, rest, sofar):
if rest:
return best(search(end, edge, remove(rest, v), sofar + outgoing)
for v in rest if outgoing in v
for edge in remove(v, outgoing))
else:
return sofar + outgoing if outgoing in end else None
def checkio(chips):
total = sum(sum(chip) for chip in chips)
start = chips.pop()
inner = best(search(remove(start, v), v, chips, 0) for v in start[:2])
return total - 2 * inner if inner else 0
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(
[[1, 4, 20], [3, 1, 5], [50, 2, 3],
[5, 2, 7], [7, 5, 20], [4, 7, 50]]) == 152, "First"
assert checkio(
[[1, 10, 2], [2, 20, 3], [3, 30, 4],
[4, 40, 5], [5, 50, 6], [6, 60, 1]]) == 210, "Second"
assert checkio(
[[1, 2, 3], [2, 1, 3], [4, 5, 6],
[6, 5, 4], [5, 1, 2], [6, 4, 3]]) == 21, "Third"
assert checkio(
[[5, 9, 5], [9, 6, 9], [6, 7, 6],
[7, 8, 7], [8, 1, 8], [1, 2, 1]]) == 0, "Fourth"
Jan. 18, 2014
Comments: