From e50565fb55b7720e9dbcb432dd3e0e5ca63bd59e Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sun, 3 May 2026 16:38:51 -0700 Subject: [PATCH] Fix width and height calculations --- tetris.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tetris.py b/tetris.py index 8181c24..fb3c86c 100644 --- a/tetris.py +++ b/tetris.py @@ -76,22 +76,25 @@ class Tetromino(NamedTuple): @property @lru_cache(maxsize=16) def bounding_box_width(self): - def ctz(arr: list): - for i, c in enumerate(reversed(arr)): - if c: - return i - return len(arr) - - return max(map(lambda r: len(r) - ctz(r), self.shape)) + start = self.width + end = 0 + for row in self.shape: + for x, cell in enumerate(row): + if cell: + end = max(end, x + 1) + start = min(start, x) + return end - start @property @lru_cache(maxsize=16) def bounding_box_height(self): - for y, row in reversed(list(enumerate(self.shape))): - for x, cell in enumerate(row): - if cell: - return y + 1 - return 0 + start = self.height + end = 0 + for y, row in enumerate(self.shape): + if any(row): + end = max(end, y + 1) + start = min(start, y) + return end - start def rotated(self, times: int): """Return self rotated TIMES times to the right."""