-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
87 lines (72 loc) · 2.17 KB
/
grid.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Utilities to work with 2D grids.
"""
NORTH = complex(0, -1)
NORTH_EAST = complex(1, -1)
EAST = complex(1, 0)
SOUTH_EAST = complex(1, 1)
SOUTH = complex(0, 1)
SOUTH_WEST = complex(-1, 1)
WEST = complex(-1, 0)
NORTH_WEST = complex(-1, -1)
NEIGHBORS_4 = NORTH, EAST, SOUTH, WEST
NEIGHBORS_8 = NEIGHBORS_4 + (NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST)
def rotate_cw(direction: complex) -> complex:
return direction * 1j
def rotate_ccw(direction: complex) -> complex:
return direction * -1j
class Grid(dict):
@property
def width(self):
return int(max(p.real for p in self.keys()) + 1)
@property
def height(self):
return int(max(p.imag for p in self.keys()) + 1)
@property
def corners(self):
return (
complex(0, 0),
complex(self.width - 1, 0),
complex(self.width - 1, self.height - 1),
complex(0, self.height - 1),
)
def get_neighbors(
self,
point: complex,
neighbors: tuple[complex, ...] = NEIGHBORS_4,
):
for n in neighbors:
other = point + n
if other in self:
yield other
def draw(self, whitespace: str = "."):
return "\n".join(
[
"".join(
[
str(self.get(complex(x, y), whitespace))
for x in range(self.width)
]
)
for y in range(self.height)
]
)
def is_in_bounds(self, point: complex):
return 0 <= point.real < self.width and 0 <= point.imag < self.height
@classmethod
def from_int_grid(cls, grid: str):
points = {
complex(x, y): int(c)
for y, line in enumerate(grid.strip().splitlines())
for x, c in enumerate(line)
}
return cls(points)
@classmethod
def from_ascii_grid(cls, grid: str, ignore_chars: str = ""):
points = {
complex(x, y): c
for y, line in enumerate(grid.strip().splitlines())
for x, c in enumerate(line)
if c not in ignore_chars
}
return cls(points)