Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Golden Pyramid by Rounin
def count_gold(pyramid, currentx=0, currenty=0):
global best
if currentx == 0 and currenty == 0:
best = {}
if str([currentx,currenty]) in best:
return best[str([currentx,currenty])]
ret = pyramid[currenty][currentx]
if currenty+1 < len(pyramid):
ret += max(count_gold(pyramid, currentx, currenty+1), count_gold(pyramid, currentx+1, currenty+1))
best[str([currentx,currenty])] = ret
return ret
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert count_gold((
(1,),
(2, 3),
(3, 3, 1),
(3, 1, 5, 4),
(3, 1, 3, 1, 3),
(2, 2, 2, 2, 2, 2),
(5, 6, 4, 5, 6, 4, 3)
)) == 23, "First example"
assert count_gold((
(1,),
(2, 1),
(1, 2, 1),
(1, 2, 1, 1),
(1, 2, 1, 1, 1),
(1, 2, 1, 1, 1, 1),
(1, 2, 1, 1, 1, 1, 9)
)) == 15, "Second example"
assert count_gold((
(9,),
(2, 2),
(3, 3, 3),
(4, 4, 4, 4)
)) == 18, "Third example"
Aug. 9, 2017