Co-authored-by: Mikael CAPELLE <mikael.capelle@thalesaleniaspace.com> Co-authored-by: Mikaël Capelle <capelle.mikael@gmail.com> Reviewed-on: #3
26 lines
628 B
Python
26 lines
628 B
Python
# pyright: reportUnknownMemberType=false
|
|
|
|
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)
|