From d782efc7bea40edcaa68bcf87c6a8f335e450a6d Mon Sep 17 00:00:00 2001 From: alexo Date: Mon, 25 Dec 2023 17:32:32 +1100 Subject: [PATCH] day25: done --- day25/day25.py | 68 +++++++++++++++++++++++++++++++++++++++++++ day25/input-small.txt | 13 +++++++++ pyproject.toml | 9 +++++- requirements.txt | 1 + 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 day25/day25.py create mode 100644 day25/input-small.txt diff --git a/day25/day25.py b/day25/day25.py new file mode 100644 index 0000000..e268795 --- /dev/null +++ b/day25/day25.py @@ -0,0 +1,68 @@ +"""day25 solution""" +from dataclasses import dataclass + +import matplotlib.pyplot as plt +import networkx as nx + +INPUT_SMALL = "day25/input-small.txt" +INPUT = "day25/input.txt" + + +@dataclass +class Connection: + src: str + dests: list[str] + + def node_names(self) -> list[str]: + return [self.src] + self.dests + + +def parse_connection(line: str) -> Connection: + src, dests = line.split(":") + return Connection(src, dests.split()) + + +def get_data(path: str) -> list[Connection]: + connections: list[Connection] = [] + with open(path, "r", encoding="utf8") as file: + for line in file: + connections.append(parse_connection(line)) + + return connections + + +def show_graph(graph: nx.Graph) -> None: + """Draws a graph that you can see""" + nx.draw(graph, with_labels=True) + plt.draw() + plt.show() + + +def graph_nodes(connections: list[Connection]) -> int: + """Graphs the modules""" + G = nx.Graph() + + nodes: set[str] = set() + for connection in connections: + to_add = connection.node_names() + for node_name in to_add: + if node_name not in nodes: + nodes.add(node_name) + G.add_node(node_name) + if node_name != connection.src: + G.add_edge(connection.src, node_name) + + show_graph(G) + cut_value, partition = nx.stoer_wagner(G) + print(f"num_cuts: {cut_value}") + return len(partition[0]) * len(partition[1]) + + +def main() -> None: + conns = get_data(INPUT) + result = graph_nodes(conns) + print(result) + + +if __name__ == "__main__": + main() diff --git a/day25/input-small.txt b/day25/input-small.txt new file mode 100644 index 0000000..bbfda0b --- /dev/null +++ b/day25/input-small.txt @@ -0,0 +1,13 @@ +jqt: rhn xhk nvd +rsh: frs pzl lsr +xhk: hfx +cmg: qnr nvd lhk bvb +rhn: xhk bvb hfx +bvb: xhk hfx +pzl: lsr hfx nvd +qnr: nvd +ntq: jqt hfx bvb xhk +nvd: lhk +lsr: lhk +rzs: qnr cmg lsr rsh +frs: qnr lhk lsr diff --git a/pyproject.toml b/pyproject.toml index ac3f2a5..dbd881f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,14 @@ python_version = '3.11' [[tool.mypy.overrides]] -module = ['graphviz', 'vpython', 'z3'] +module = [ + 'graphviz', + 'vpython', + 'z3', + 'networkx', + 'matplotlib.pyplot', + 'matplotlib', +] ignore_missing_imports = true [tool.ruff] diff --git a/requirements.txt b/requirements.txt index a9b85f6..7feea7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ colorama==0.4.6 graphviz==0.20.1 mypy==1.8.0 +networkx==3.2.1 pre-commit==3.6.0 python-dotenv==1.0.0 pytest==7.4.3