Files
advent-of-code/src/holt59/aoc/2023/day25.py
2024-12-06 14:11:35 +01:00

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)