Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
straightforward solution in Creative category for Network Attack by roman.bratishchev
# 'capturing' is the current nodes being attacked
# 'lifetimes' is the residual time for the node to surrender
# 'get_links' returns still alive nodes linked to the given node
def capture(net:list[list[int]]) -> int:
t,lifetimes=0,[net[i][i] for i in range(len(net))]
capturing=(get_links:=lambda i: {li for li,link in enumerate(net[i]) if link and lifetimes[li]})(0)
while capturing:
t+=(tdelta:=min(lifetimes[i] for i in capturing))
for i in filter(capturing.__contains__,range(len(net))): lifetimes[i]-=tdelta
capturing=set().union(*({i} if lifetimes[i] else get_links(i) for i in capturing))
return t
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"
Feb. 7, 2025