24 lines
586 B
Python
24 lines
586 B
Python
from typing import Any, Iterator
|
|
|
|
import networkx as nx
|
|
|
|
from ..base import BaseSolver
|
|
|
|
|
|
class Solver(BaseSolver):
|
|
def solve(self, input: str) -> Iterator[Any]:
|
|
components = {
|
|
(p := line.split(": "))[0]: p[1].split() for line in input.splitlines()
|
|
}
|
|
|
|
graph: "nx.Graph[str]" = nx.Graph()
|
|
graph.add_edges_from((u, v) for u, vs in components.items() for v in vs)
|
|
|
|
cut = nx.minimum_edge_cut(graph)
|
|
graph.remove_edges_from(cut)
|
|
|
|
c1, c2 = nx.connected_components(graph)
|
|
|
|
# part 1
|
|
yield len(c1) * len(c2)
|