Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Network Attack by Nuszwkartlo
def capture(matrix):
infectedList = [0]
infectingList = [[0,0]]
temporaryList = []
time = 0
for item in (CorruptNextPositions(0,infectingList,matrix)):
infectingList.append(item)
while (len(infectedList) != len(matrix)):
temporaryList = []
time = time + 1
for item in infectingList:
if item[1] != 0:
item[1] = item[1] - 1
if item[1] == 0:
infectedList.append(item[0])
temporaryList.append(item[0])
for item in temporaryList:
for item2 in (CorruptNextPositions(item,infectingList,matrix)):
infectingList.append(item2)
return time
def CorruptNextPositions(motherPosition, currentlyInfected, matrix):
newSeeds = []
supplementaryList = []
for x in range(0,len(matrix)):
supplementaryList.append(x)
for item in currentlyInfected:
supplementaryList.remove(item[0])
for item in supplementaryList:
if matrix[motherPosition][item] == 1:
newSeeds.append([item,matrix[item][item]])
return newSeeds
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. 21, 2016