From a04024c5db1646d5dbb6279129546b5aaf77694d Mon Sep 17 00:00:00 2001 From: alex-ong Date: Sat, 30 Dec 2023 16:12:47 +1100 Subject: [PATCH] docs: day25 --- day25/__init__.py | 1 + day25/day25.py | 15 ++++++++++++--- day25/tests/__init__.py | 1 + day25/tests/test_day25.py | 4 ++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/day25/__init__.py b/day25/__init__.py index e69de29..902e6fc 100644 --- a/day25/__init__.py +++ b/day25/__init__.py @@ -0,0 +1 @@ +"""day25 solution.""" diff --git a/day25/day25.py b/day25/day25.py index b362bd1..d3dac05 100644 --- a/day25/day25.py +++ b/day25/day25.py @@ -1,4 +1,4 @@ -"""day25 solution""" +"""day25 solution.""" from dataclasses import dataclass import matplotlib.pyplot as plt @@ -11,19 +11,27 @@ @dataclass class Connection: + """Connection between two nodes.""" + src: str dests: list[str] def node_names(self) -> list[str]: + """Return all nodes in a connection.""" return [self.src] + self.dests def parse_connection(line: str) -> Connection: + """Parse connection into well defined class. + + E.g. ``src: dest1 dest2 dest3``. + """ src, dests = line.split(":") return Connection(src, dests.split()) def get_data(path: str) -> list[Connection]: + """Loads data and parses it into list of connections.""" connections: list[Connection] = [] with open(path, "r", encoding="utf8") as file: for line in file: @@ -33,14 +41,14 @@ def get_data(path: str) -> list[Connection]: def show_graph(graph: nx.Graph) -> None: # pragma: no cover - """Draws a graph that you can see""" + """Draws a graph that you can see.""" nx.draw(graph, with_labels=True) plt.draw() plt.show() def solve_nodes(connections: list[Connection]) -> int: - """Graphs the modules""" + """Graphs the modules.""" G = nx.Graph() nodes: set[str] = set() @@ -60,6 +68,7 @@ def solve_nodes(connections: list[Connection]) -> int: def main() -> None: + """Load data and solve.""" conns = get_data(INPUT) result = solve_nodes(conns) print(result) diff --git a/day25/tests/__init__.py b/day25/tests/__init__.py index e69de29..cf3dfb9 100644 --- a/day25/tests/__init__.py +++ b/day25/tests/__init__.py @@ -0,0 +1 @@ +"""day25 tests.""" diff --git a/day25/tests/test_day25.py b/day25/tests/test_day25.py index 986be6c..6d963be 100644 --- a/day25/tests/test_day25.py +++ b/day25/tests/test_day25.py @@ -1,18 +1,22 @@ +"""Test day25 main functions.""" from day25.day25 import INPUT_SMALL, Connection, get_data, parse_connection, solve_nodes def test_get_data() -> None: + """Test ``get_data()``.""" conns: list[Connection] = get_data(INPUT_SMALL) assert len(conns) == 13 assert conns[0].src == "jqt" def test_parse_connection() -> None: + """Test ``parse_connection()``.""" conn: Connection = parse_connection("zmx: vfl mgb tmr bsn") assert conn.src == "zmx" assert set(conn.dests) == {"vfl", "mgb", "tmr", "bsn"} def test_day25() -> None: + """Test ``solve_nodes()``.""" conns: list[Connection] = get_data(INPUT_SMALL) assert solve_nodes(conns) == 54