Pentomino tiling: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: pre-generate shifted minos)
(→‎{{header|Python}}: speed improvement; change to unicode box drawing chars)
Line 1,474: Line 1,474:
<lang python>from itertools import product
<lang python>from itertools import product


minos = (((197123, 7, 6), (1797, 6, 7), (1287, 6, 7), (196867, 7, 6)), ((263937, 6, 6), (197126, 6, 6), (393731, 6, 6), (67332, 6, 6)),
minos = (((197123, 7, 6), (1797, 6, 7), (1287, 6, 7), (196867, 7, 6)),
((263937, 6, 6), (197126, 6, 6), (393731, 6, 6), (67332, 6, 6)),
((16843011, 7, 5), (2063, 5, 7), (3841, 5, 7), (271, 5, 7), (3848, 5, 7), (50463234, 7, 5), (50397441, 7, 5), (33686019, 7, 5)),
((16843011, 7, 5), (2063, 5, 7), (3841, 5, 7), (271, 5, 7), (3848, 5, 7), (50463234, 7, 5), (50397441, 7, 5), (33686019, 7, 5)),
((131843, 7, 6), (1798, 6, 7), (775, 6, 7), (1795, 6, 7), (1543, 6, 7), (197377, 7, 6), (197378, 7, 6), (66307, 7, 6)),
((131843, 7, 6), (1798, 6, 7), (775, 6, 7), (1795, 6, 7), (1543, 6, 7), (197377, 7, 6), (197378, 7, 6), (66307, 7, 6)),
Line 1,485: Line 1,486:
((4311810305, 8, 4), (31, 4, 8)),
((4311810305, 8, 4), (31, 4, 8)),
((132866, 6, 6),))
((132866, 6, 6),))

# for drawing in terminal; correct aspect ratio depends on your terminal font
boxchar_double_width = ' ██┘█─└┴█┐│┤┌┬├┼'
boxchar_single_width = (' ', '██', '██', '┘ ', '██', '──', '└─', '┴─', '██', '┐ ', '│ ', '┤ ', '┌─', '┬─', '├─', '┼─')
patterns = boxchar_single_width


tiles = []
tiles = []
for row in minos:
for row in reversed(minos):
tiles.append([])
tiles.append([])
for n, x, y in row:
for n, x, y in row:
for shift in (b*8 + a for a, b in product(range(x), range(y))):
for shift in (b*8 + a for a, b in product(range(x), range(y))):
tiles[-1].append(n << shift)
tiles[-1].append(n << shift)



def img(seq):
def img(seq):
Line 1,502: Line 1,507:
b[j + 1][k + 1] = i
b[j + 1][k + 1] = i


vert = ([' |'[row[i] != row[i+1]] for i in range(9)] for row in b)
idices = [[0]*9 for _ in range(9)]
hori = ([' _'[b[i+1][j] != b[i][j]] for j in range(1, 10)] for i in range(9))
for i, j in product(range(9), range(9)):
n = (b[i][j], b[i][j+1], b[i+1][j+1], b[i+1][j])
idices[i][j] = sum((n[i] != n[i - 1])<<i for i in range(4))


return '\n'.join([''.join(a + b for a, b in zip(v, h)) for v, h in zip(vert, hori)])
return '\n'.join(''.join(patterns[i] for i in row) for row in idices)


def tile(board=0, seq=tuple(), tiles=tiles):

if not tiles:
def tile(board, i, seq=tuple()):
if not i:
yield img(seq)
yield img(seq)
else:
return

for c in [c for c in tiles[i - 1] if not board & c]:
for c in tiles[0]:
yield from tile(board|c, i - 1, (c,) + seq)
b = board | c

tnext = [] # not using list comprehension ...
for t in tiles[1:]:
tnext.append(tuple(n for n in t if not n&b))
if not tnext[-1]: break # because early break is faster
else:
yield from tile(b, seq + (c,), tnext)


for i in tile(0, len(tiles)): print(i)</lang>
for x in tile():
print(x)</lang>
{{out}}
{{out}}
<pre> ┌─┬───┬───────┐
<pre> _ _ _ _ _ _ _
┌─┘ └─┐ └─┬─┐ ┌─┤
_| |_ |_ _ _|
├─┐ ┌─┼─┐ │ └─┘ │
|_ _|_ | |_| |
│ ├─┤ └─┴─┼─┬───┤
| |_| |_|_|_ _ _|
│ │ └───┐ ├─┴─┐ │
| | |_ _ |_|_ |
│ │ ┌─┬─┴─┘ ┌─┤ │
| | _ _|_| _| |
│ ├─┤ ├───┬─┘ │ │
| |_| |_ _ _| | |
├─┴─┘ │ └─┐ └─┤
|_|_| | |_ |_|
└─────┴─────┴───┘
|_ _ _|_ _ _|_ _|
┌─┬───┐ ┌─┬───┐
_ _ _ _ _ _
┌─┘ └─┐ └─┤ └─┐ │
_| |_ |_| |_ |
├─┐ ┌─┼─┐ │ ┌─┘ │
|_ _|_ | _| |
│ ├─┤ └─┴─┤ ├───┤
| |_| |_|_| |_ _|
│ │ └───┐ ├─┴─┐ │
| | |_ _ |_|_ |
│ │ ┌─┬─┴─┘ ┌─┤ │
| | _ _|_| _| |
│ ├─┤ ├───┬─┘ │ │
| |_| |_ _ _| | |
├─┴─┘ │ └─┐ └─┤
|_|_| | |_ |_|
└─────┴─────┴───┘</pre>
|_ _ _|_ _ _|_ _|
.
.
.</pre>


=={{header|Raku}}==
=={{header|Raku}}==