Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
8-liner solution in Clear category for The Buttons by Merzix
def buttons(ceiling):
ceiling = [list(map(int, x)) for x in ceiling.splitlines()[1:]]
max_h, max_w = len(ceiling) - 1, len(ceiling[0]) - 1
def sum_around(h, w):
if h < 0 or h > max_h or w < 0 or w > max_w or not ceiling[h][w]: return 0
temp, ceiling[h][w] = ceiling[h][w], 0
return temp + sum(sum_around(h + i, w + j) for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)])
return sorted(filter(bool, [sum_around(x, y) for x in range(max_h + 1) for y in range(max_w + 1)]), reverse=True)
if __name__ == '__main__':
assert buttons('''
1''') == [1]
assert buttons('''
001203
023001
100220''') == [8, 4, 4, 1]
assert buttons('''
000000
000055
000055''') == [20]
assert buttons('''
908070
060504
302010''') == [9, 8, 7, 6, 5, 4, 3, 2, 1]
assert buttons('''
010630
021510
700003''') == [19, 7, 3], "Like v"
assert buttons('''
011103
020406
310210''') == [16, 9], 'Like ^'
assert buttons('''
512011
003020
301410
701030
022004''') == [29, 10, 4], 'Like X'
Sept. 24, 2018
Comments: