-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |