Skip to content

Instantly share code, notes, and snippets.

@maghorbani
Last active March 22, 2022 10:29
Show Gist options
  • Save maghorbani/36d558a616342617688311b210a8dfb3 to your computer and use it in GitHub Desktop.
Save maghorbani/36d558a616342617688311b210a8dfb3 to your computer and use it in GitHub Desktop.
from copy import deepcopy
class Wall:
def __init__(self, h, w):
self.w = w
self.h = h
self.array = [[0]*w for _ in range(h)]
self.tileValue = 1
def putTile(self, x, y, h, w, v=None):
for i in range(x,x+h):
for j in range(y, y+w):
if self.array[i][j] > 0:
raise Exception("position full")
for i in range(x,x+h):
for j in range(y, y+w):
self.array[i][j] = v or self.tileValue
self.tileValue += 1
def middle(self, tl, tr, bl, br):
if tl==tr==bl==br:
return " "
if tl!=tr==bl==br:
return "\x1b(0\x6a\x1b(B"
if tl==tr==br!=bl:
return "\x1b(0\x6b\x1b(B"
if tl==tr==bl!=br:
return "\x1b(0\x6c\x1b(B"
if tr!=tl==bl==br:
return "\x1b(0\x6d\x1b(B"
if tl==tr!=bl==br:
return "\x1b(0\x71\x1b(B"
if tl==bl!=tr==br:
return "\x1b(0\x78\x1b(B"
if tl!=bl!=tr==br:
return "\x1b(0\x75\x1b(B"
if tl==bl!=tr!=br:
return "\x1b(0\x74\x1b(B"
if tl!=tr!=bl==br:
return "\x1b(0\x76\x1b(B"
if tl==tr!=bl!=br:
return "\x1b(0\x77\x1b(B"
if tr!=tl!=bl!=br:
return "\x1b(0\x6e\x1b(B"
def draw(self):
for i in range(self.h):
if i == 0:
for j in range(self.w):
if j == 0:
print("\x1b(0\x6c", end="\x1b(B") #┌
else:
if self.array[i][j] == self.array[i][j-1]:
print("\x1b(0\x71", end="\x1b(B") # ─
else:
print("\x1b(0\x77", end="\x1b(B") #┬
print("\x1b(0\x71", end="\x1b(B") # ─
print("\x1b(0\x6b", end="\x1b(B\n") # ┐\n
else:
for j in range(self.w):
if j == 0:
if self.array[i][j] == self.array[i-1][j]:
print("\x1b(0\x78", end="\x1b(B") #│
else:
print("\x1b(0\x74", end="\x1b(B") #├
else:
print(self.middle(self.array[i-1][j-1], self.array[i-1][j],
self.array[i][j-1], self.array[i][j]), end="")
if self.array[i][j] == self.array[i-1][j]:
print(" ", end="")
else:
print("\x1b(0\x71", end="\x1b(B") # ─
if self.array[i][j] == self.array[i-1][j]:
print("\x1b(0\x78", end="\x1b(B\n") #│
else:
print("\x1b(0\x75", end="\x1b(B\n") # ┤\n
for j in range(self.w):
if j == 0:
print("\x1b(0\x6d", end="\x1b(B") #└
else:
if self.array[i][j] == self.array[i][j-1]:
print("\x1b(0\x71", end="\x1b(B") # ─
else:
print("\x1b(0\x76", end="\x1b(B") #┴
print("\x1b(0\x71", end="\x1b(B") # ─
print("\x1b(0\x6a", end="\x1b(B\n") # ┘\n
def recurse(c, idx):
inv_idx = c.w - idx
if inv_idx == 0:
c.draw()
return
c1 = deepcopy(c)
c1.putTile(0, idx, 2, 1)
if inv_idx == 1:
c1.draw()
return
c2 = deepcopy(c)
c2.putTile(0, idx, 1, 2)
c2.putTile(1, idx, 1, 2)
recurse(c1, idx+1)
recurse(c2, idx+2)
if __name__ == "__main__":
c = Wall(2,6)
recurse(c, 0)
@maghorbani
Copy link
Author

n=4

┌─┬─┬─┬─┐
│ │ │ │ │
└─┴─┴─┴─┘
┌─┬─┬───┐
│ │ ├───┤
└─┴─┴───┘
┌─┬───┬─┐
│ ├───┤ │
└─┴───┴─┘
┌───┬─┬─┐
├───┤ │ │
└───┴─┴─┘
┌───┬───┐
├───┼───┤
└───┴───┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment