Skip to content

Commit

Permalink
docs: day25
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 30, 2023
1 parent d122475 commit a04024c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions day25/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day25 solution."""
15 changes: 12 additions & 3 deletions day25/day25.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""day25 solution"""
"""day25 solution."""
from dataclasses import dataclass

import matplotlib.pyplot as plt
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions day25/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""day25 tests."""
4 changes: 4 additions & 0 deletions day25/tests/test_day25.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a04024c

Please sign in to comment.