Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Radiation Search by tom-tom
def checkio(matrix):
parts ={(i, j): p for j, row in enumerate(matrix) for i, p in enumerate(row)}
def mark(i, j, p):
if parts.get((i, j)) == p:
del parts[(i, j)]
return 1 + mark(i + 1, j, p) + mark(i - 1, j, p) + mark(i, j + 1, p) + mark(i, j - 1, p)
return 0
max_group = [0, 0]
while parts:
(i, j), p = next(iter(parts.items()))
max_group = max(max_group, [mark(i, j, p), p])
return max_group
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
[1, 2, 3, 4, 5],
[1, 1, 1, 2, 3],
[1, 1, 1, 2, 2],
[1, 2, 2, 2, 1],
[1, 1, 1, 1, 1]
]) == [14, 1], "14 of 1"
assert checkio([
[2, 1, 2, 2, 2, 4],
[2, 5, 2, 2, 2, 2],
[2, 5, 4, 2, 2, 2],
[2, 5, 2, 2, 4, 2],
[2, 4, 2, 2, 2, 2],
[2, 2, 4, 4, 2, 2]
]) == [19, 2], '19 of 2'
Aug. 11, 2017