Skip to content

Instantly share code, notes, and snippets.

@Bamimore-Tomi
Last active December 28, 2021 19:22
Show Gist options
  • Save Bamimore-Tomi/5e7634a6bbe988e80c8cca41bfbc7b77 to your computer and use it in GitHub Desktop.
Save Bamimore-Tomi/5e7634a6bbe988e80c8cca41bfbc7b77 to your computer and use it in GitHub Desktop.
shapes = {"l": [[1, 1], [1, 0]], "s": [[1, 1, 1]]}
class Grid:
def __init__(self, row, col):
self.row = row
self.col = col
self.grid = [[0 for i in range(col)] for i in range(row)]
self.filed = []
def __str__(self):
cols = "{} " * self.col + "\n"
rows = cols * self.row
return rows.format(
*[
int(i)
for i in str(self.grid).replace("[", "").replace("]", "").split(",")
]
)
@staticmethod
def visualize(two_dim_grid):
len_col = lambda x: len(x)
max_col = max([len_col(i) for i in two_dim_grid])
max_row = len(two_dim_grid)
cols = "{} " * max_col + "\n"
rows = cols * max_row
print(
rows.format(
*[
int(i)
for i in str(two_dim_grid)
.replace("[", "")
.replace("]", "")
.split(",")
]
)
)
@staticmethod
def right_flip(sub_grid):
return [i[::-1] for i in sub_grid]
@staticmethod
def down_flip(sub_grid):
return sub_grid[::-1]
@staticmethod
def up_flip(sub_grid):
return a.right_flip(a.down_flip(sub_grid))
@staticmethod
def rotate(sub_grid):
len_col = lambda x: len(x)
max_col = max([len_col(i) for i in sub_grid])
max_row = len(sub_grid)
res = [[0 for i in range(max_row)] for i in range(max_col)]
for i, j in enumerate(sub_grid):
for k, l in enumerate(j):
res[k][i] = sub_grid[i][k]
return res
def canonical(self, x, y):
assert 0 <= x < self.row - 1 and 0 <= y < self.col - 1, "Index out of range"
if (
self.grid[x][y] == 0
and self.grid[x][y + 1] == 0
and self.grid[x + 1][y] == 0
):
self.grid[x][y], self.grid[x][y + 1], self.grid[x + 1][y] = 1, 1, 1
self.filed.append({"canonical": [(x, y), (x, y + 1), (x + 1, y)]})
return True
else:
return False
def right_flip(self, x, y):
assert self.row - 1 > x >= 0 and self.col > y > 0, "Index out of range"
if (
self.grid[x][y] == 0
and self.grid[x][y - 1] == 0
and self.grid[x + 1][y] == 0
):
self.grid[x][y], self.grid[x][y - 1], self.grid[x + 1][y] = 1, 1, 1
self.filed.append({"right_flip": [(x, y), (x, y - 1), (x + 1, y)]})
return True
else:
return False
def down_flip(self, x, y):
assert 0 < x < self.row and self.row > y >= 0, "Index out of range"
if (
self.grid[x][y] == 0
and self.grid[x - 1][y] == 0
and self.grid[x][y + 1] == 0
):
self.grid[x][y], self.grid[x - 1][y], self.grid[x][y + 1] = 1, 1, 1
self.filed.append({"down_flip": [(x, y), (x - 1, y), (x, y + 1)]})
return True
else:
return False
def up_flip(self, x, y):
assert 0 < x < self.row and self.col > y > 0
if (
self.grid[x][y] == 0
and self.grid[x][y - 1] == 0
and self.grid[x - 1][y] == 0
):
self.grid[x][y], self.grid[x][y - 1], self.grid[x - 1][y] = 1, 1, 1
self.filed.append({"up_flip": [(x, y), (x, y - 1), (x - 1, y)]})
return True
else:
return False
def horizontal(self, x, y):
assert 0 <= x < self.row and 0 < y < self.col - 1
if (
self.grid[x][y] == 0
and self.grid[x][y - 1] == 0
and self.grid[x][y + 1] == 0
):
self.grid[x][y], self.grid[x][y - 1], self.grid[x][y + 1] = 1, 1, 1
self.filed.append({"horizontal": [(x, y), (x, y - 1), (x, y + 1)]})
return True
else:
return False
def vertical(self, x, y):
assert 0 <= x < self.row - 1 and 0 <= y < self.col
if (
self.grid[x][y] == 0
and self.grid[x + 1][y] == 0
and self.grid[x - 1][y] == 0
):
self.grid[x][y], self.grid[x + 1][y], self.grid[x - 1][y] = 1, 1, 1
self.filed.append({"vertical": [(x, y), (x + 1, y), (x - 1, y)]})
return True
else:
return False
def tile(self):
x, y = 0, 0
print(self.filed)
a = Grid(3, 3)
a.tile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment