Skip to content

Commit

Permalink
day23: stub for Path
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 23, 2023
1 parent aa0efaa commit d27cefd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
30 changes: 30 additions & 0 deletions day23/lib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion day23/tests/test_classes.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)

0 comments on commit d27cefd

Please sign in to comment.