Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: recursive revisited (this is only correction of my previous solution) solution in Clear category for The Buttons by przemyslaw.daniel
def buttons(ceiling):
ceiling = ceiling.split()
height, width = range(len(ceiling)), range(len(ceiling[0]))
valid = {(x, y) for x in height for y in width}
def calc(x, y):
if (x, y) not in valid or ceiling[x][y] == '0':
return 0
valid.remove((x,y))
points = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
return int(ceiling[x][y])+sum(calc(i, j) for i, j in points)
result = [calc(x, y) for x in height for y in width]
return sorted([x for x in result if x], reverse=True)
Sept. 24, 2018