-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
48 lines (38 loc) · 1.28 KB
/
grid.py
File metadata and controls
48 lines (38 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from dataclasses import dataclass
from typing import Any
UP = (-1, 0)
RIGHT = (0, 1)
DOWN = (1, 0)
LEFT = (0, -1)
CLOCKWISE_DIRS = [UP, RIGHT, DOWN, LEFT]
COUNTERCLOCKWISE_DIRS = [UP, LEFT, DOWN, RIGHT]
@dataclass
class GridNode:
val: Any
@dataclass
class Grid:
_grid: list[list[GridNode]]
num_rows: int
num_cols: int
def __init__(self, input_file_str: str):
self._grid = []
for row in input_file_str.splitlines():
self._grid.append([GridNode(val) for val in row])
self.num_rows, self.num_cols = len(self._grid), len(self._grid[0])
def print(self, row_type=str):
if row_type is str:
for row in self._grid:
print("".join([str(g.val) for g in row]))
else:
for row in self._grid:
print([g.val for g in row])
def in_bounds(self, row:int, col:int):
return 0 <= row < self.num_rows and 0 <= col < self.num_cols
def get_node_val(self, row:int, col:int):
return self._grid[row][col].val
def set_node_val(self, row: int, col: int, val: Any):
self._grid[row][col].val = val
def nodes_indicies(self):
for row in range(self.num_rows):
for col in range(self.num_cols):
yield (row, col)