Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Network Attack by archius11
def capture(matrix):
mins = 0
pc_num = len(matrix[0])
pc_matrix = [1] + ([0]*(pc_num-1))
while 0 in pc_matrix: #while exist clear pc
downcounted = [] #pc-s downcounted by current interation
for i in range(0,pc_num): #pc-s
if pc_matrix[i]==1: #if cathed pc
for n in range(0,pc_num): #pc-s connected i-th pc
if i!=n and matrix[i][n] == 1 and not n in downcounted: #if not diag and connected and not downcounted
downcounted+=[n]
for i in downcounted: #downcounted pc-s -1 security lvl
matrix[i][i]-=1
if matrix[i][i] == 0: #hacked pc
pc_matrix[i] = 1
mins+=1
return mins
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"
Jan. 23, 2019