Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Numbered Triangles by mortonfox
def checkio(chips):
def trychip(remain_chips, score, first_side, last_side):
if not remain_chips:
# If we run out of chips, make sure the sides where the hexagon
# closes match too.
return score if last_side == first_side else 0
high_score = 0
for i in range(len(remain_chips)):
chip = remain_chips[i]
for side in range(3):
if chip[side] == last_side:
rest_of_the_chips = remain_chips[:i] + remain_chips[i+1:]
high_score = max(high_score,
# This chip fits. Try it.
trychip(rest_of_the_chips, score + chip[(side + 1) % 3], first_side, chip[(side + 2) % 3]),
# Flip the chip and try that too.
trychip(rest_of_the_chips, score + chip[(side + 2) % 3], first_side, chip[(side + 1) % 3]))
return high_score
first_chip = chips[0]
rest_of_the_chips = chips[1:]
return max(trychip(rest_of_the_chips, first_chip[(side + 1) % 3], first_chip[side], first_chip[(side + 2) % 3]) for side in range(3))
#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"
Nov. 20, 2017