Fix width and height calculations

This commit is contained in:
2026-05-03 16:38:51 -07:00
parent 2a5dee651e
commit e50565fb55
+15 -12
View File
@@ -76,22 +76,25 @@ class Tetromino(NamedTuple):
@property @property
@lru_cache(maxsize=16) @lru_cache(maxsize=16)
def bounding_box_width(self): def bounding_box_width(self):
def ctz(arr: list): start = self.width
for i, c in enumerate(reversed(arr)): end = 0
if c: for row in self.shape:
return i for x, cell in enumerate(row):
return len(arr) if cell:
end = max(end, x + 1)
return max(map(lambda r: len(r) - ctz(r), self.shape)) start = min(start, x)
return end - start
@property @property
@lru_cache(maxsize=16) @lru_cache(maxsize=16)
def bounding_box_height(self): def bounding_box_height(self):
for y, row in reversed(list(enumerate(self.shape))): start = self.height
for x, cell in enumerate(row): end = 0
if cell: for y, row in enumerate(self.shape):
return y + 1 if any(row):
return 0 end = max(end, y + 1)
start = min(start, y)
return end - start
def rotated(self, times: int): def rotated(self, times: int):
"""Return self rotated TIMES times to the right.""" """Return self rotated TIMES times to the right."""