Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Network Attack by marcopunteri
def capture(matrix):
elapsed = 0
surv_times = [matrix[i][i] for i in range(len(matrix))]
network_pairs = {computer : {other for other in range(len(matrix)) if matrix[computer][other] == 1 and computer != other} for computer in range(len(matrix)) }
while any(surv_times):
elapsed += 1
already_hacked = set()
for computer in [pos for pos,c in enumerate(surv_times) if c==0]:
for other in network_pairs[computer]:
if other not in already_hacked:
surv_times[other] = max(surv_times[other]-1,0)
already_hacked.add(other)
return elapsed
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, 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"
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"
June 7, 2021