Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
set operations solution in Clear category for Power Supply by roman.bratishchev
from typing import Iterable
def power_supply(network:Iterable[Iterable[str]], plants:dict[str, int]) -> set[str]:
not_supplied=set().union(*map(set,network))
for plant,r in plants.items():
supplied={plant} # nodes supplied by the plant
for _ in range(r): # calculated next level of nodes within the plant range of supply
supplied|={new_node.pop() for link in network if len(new_node:=set(link)-supplied)==1}
not_supplied-=supplied
return not_supplied
if __name__ == "__main__":
assert power_supply([["p1", "c1"], ["c1", "c2"]], {"p1": 1}) == set(
["c2"]
), "one blackout"
assert power_supply(
[["c0", "c1"], ["c1", "p1"], ["c1", "c3"], ["p1", "c4"]], {"p1": 1}
) == set(["c0", "c3"]), "two blackout"
assert power_supply([["p1", "c1"], ["c1", "c2"], ["c2", "c3"]], {"p1": 3}) == set(
[]
), "no blackout"
assert power_supply([["c0", "p1"], ["p1", "c2"]], {"p1": 0}) == set(
["c0", "c2"]
), "weak power-plant"
assert power_supply(
[["p0", "c1"], ["p0", "c2"], ["c2", "c3"], ["c3", "p4"], ["p4", "c5"]],
{"p0": 1, "p4": 1},
) == set([]), "cooperation"
assert power_supply(
[
["c0", "p1"],
["p1", "c2"],
["c2", "c3"],
["c2", "c4"],
["c4", "c5"],
["c5", "c6"],
["c5", "p7"],
],
{"p1": 1, "p7": 1},
) == set(["c3", "c4", "c6"]), "complex cities 1"
assert power_supply(
[
["p0", "c1"],
["p0", "c2"],
["p0", "c3"],
["p0", "c4"],
["c4", "c9"],
["c4", "c10"],
["c10", "c11"],
["c11", "p12"],
["c2", "c5"],
["c2", "c6"],
["c5", "c7"],
["c5", "p8"],
],
{"p0": 1, "p12": 4, "p8": 1},
) == set(["c6", "c7"]), "complex cities 2"
assert power_supply([["c1", "c2"], ["c2", "c3"]], {}) == set(
["c1", "c2", "c3"]
), "no power plants"
assert power_supply(
[["p1", "c2"], ["p1", "c4"], ["c4", "c3"], ["c2", "c3"]], {"p1": 1}
) == set(["c3"]), "circle"
assert power_supply([["p1", "c2"], ["p1", "c4"], ["c2", "c3"]], {"p1": 4}) == set(
[]
), "more than enough"
print("Looks like you know everything. It is time for 'Check'!")
Oct. 4, 2024