Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Network Attack by mehWincenty
def capture(matrix):
infected = [0]
hacked = []
m = 0
while max(matrix[i][i] for i in range(len(matrix))) > 0:
hacked = []
for i in infected:
for j in range(len(matrix[i])):
if matrix[i][j] == 1 and hacked.count(j) == 0 and i!=j:
matrix[j][j] -= 1
hacked.append(j)
for i in range(len(matrix)):
if matrix[i][i] == 0 and infected.count(i) == 0:
infected.append(i)
m += 1
return m
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert capture([[0, 1, 0, 1, 0, 1],
[1, 1, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 4, "Low security"
assert capture([[0, 1, 1],
[1, 9, 1],
[1, 1, 9]]) == 9, "Small"
assert capture([[0, 1, 0, 1, 0, 1],
[1, 8, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 8, "Base example"
Nov. 11, 2016