Skip to content

Commit

Permalink
day23: maze stub
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 23, 2023
1 parent f5f01c8 commit aa0efaa
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
Empty file added day23/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions day23/day23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from day23.lib.parsers import get_maze
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from day23.lib.classes import Maze

INPUT = "day23/input.txt"
INPUT_SMALL = "day23/input-small.txt"


def main() -> None:
maze: Maze = get_maze(INPUT_SMALL)
print(maze)


if __name__ == "__main__":
main()
23 changes: 23 additions & 0 deletions day23/input-small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#
Empty file added day23/lib/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions day23/lib/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True, slots=True)
class Position:
row: int
col: int

def copy_modify(
self, row: Optional[int] = None, col: Optional[int] = None
) -> "Position":
if row is None:
row = self.row
if col is None:
col = self.col
return Position(row, col)


class Maze:
grid: list[str] # 2d array of chars
num_rows: int
num_cols: int

def __init__(self, data: list[str]) -> None:
self.grid = data
self.num_rows = len(data)
self.num_cols = len(data[0])

def __str__(self) -> str:
return "\n".join(row for row in self.grid)

def __getitem__(self, position: Position) -> Optional[str]:
"""Get item via position. Returns None if out of bounds"""
if not isinstance(position, Position):
raise ValueError(f"position is not a Position, {type(position)}")
if self.is_oob(position):
return None
return self.grid[position.row][position.col]

def is_oob(self, position: Position) -> bool:
"""true if position is out of bounds"""
return (
position.row < 0
or position.row >= self.num_rows
or position.col < 0
or position.col >= self.num_cols
)
8 changes: 8 additions & 0 deletions day23/lib/parsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from day23.lib.classes import Maze


def get_maze(path: str) -> Maze:
rows: list[str] = []
with open(path, encoding="utf8") as file:
rows = [line.strip() for line in file]
return Maze(rows)
20 changes: 20 additions & 0 deletions day23/tests/test_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from day23.day23 import INPUT_SMALL
from day23.lib.classes import Maze, Position
from day23.lib.parsers import get_maze


def test_position() -> None:
pos: Position = Position(0, 0)
pos2 = pos.copy_modify()
assert pos == pos2
pos3 = pos.copy_modify(row=1)
assert pos3.row == 1 and pos3.col == 0
pos4 = pos.copy_modify(col=1)
assert pos4.row == 0 and pos4.col == 1


def test_maze() -> None:
maze: Maze = get_maze(INPUT_SMALL)
assert maze[Position(0, 0)] == "#"
assert maze[Position(0, 1)] == "."
assert maze[Position(-1, 0)] is None
13 changes: 13 additions & 0 deletions day23/tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import TYPE_CHECKING

from day23.day23 import INPUT_SMALL
from day23.lib.parsers import get_maze

if TYPE_CHECKING:
from day23.lib.classes import Maze


def test_get_maze() -> None:
maze: Maze = get_maze(INPUT_SMALL)
assert maze.num_rows == 23
assert maze.num_cols == 23

0 comments on commit aa0efaa

Please sign in to comment.