From d27cefdf23590764ea1d015b677b655adbf32065 Mon Sep 17 00:00:00 2001 From: Alex-m2 Date: Sat, 23 Dec 2023 21:22:21 +1100 Subject: [PATCH] day23: stub for Path --- day23/lib/classes.py | 30 ++++++++++++++++++++++++++++++ day23/tests/test_classes.py | 16 +++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/day23/lib/classes.py b/day23/lib/classes.py index 123279b..239c753 100644 --- a/day23/lib/classes.py +++ b/day23/lib/classes.py @@ -16,6 +16,36 @@ def copy_modify( col = self.col return Position(row, col) + def __str__(self) -> str: + return f"{self.row}, {self.col}" + + +class Path: + route: list[Position] + nodes: set[Position] + + def __init__(self) -> None: + self.route = [] + self.nodes = set() + + def can_add(self, position: Position) -> bool: + return position not in self.nodes + + def add(self, position: Position) -> None: + self.route.append(position) + self.nodes.add(position) + + def copy(self) -> "Path": + result = Path() + result.route = self.route[:] + result.nodes = set(result.route) + return result + + def last(self) -> Position: + if len(self.route) == 0: + raise ValueError("Don't call last when i'm empty 4head") + return self.route[-1] + class Maze: grid: list[str] # 2d array of chars diff --git a/day23/tests/test_classes.py b/day23/tests/test_classes.py index 7064ab3..6823840 100644 --- a/day23/tests/test_classes.py +++ b/day23/tests/test_classes.py @@ -1,5 +1,5 @@ from day23.day23 import INPUT_SMALL -from day23.lib.classes import Maze, Position +from day23.lib.classes import Maze, Path, Position from day23.lib.parsers import get_maze @@ -18,3 +18,17 @@ def test_maze() -> None: assert maze[Position(0, 0)] == "#" assert maze[Position(0, 1)] == "." assert maze[Position(-1, 0)] is None + + +def test_path() -> None: + path = Path() + path.add(Position(0, 0)) + path.add(Position(0, 1)) + assert path.last() == Position(0, 1) + + path2 = path.copy() + + assert path2.last() == Position(0, 1) + path2.add(Position(0, 2)) + assert path.last() == Position(0, 1) + assert path2.last() == Position(0, 2)