-
Notifications
You must be signed in to change notification settings - Fork 0
/
A-Puzzle-A-Day - Draw Symmetries.py
226 lines (176 loc) · 8.79 KB
/
A-Puzzle-A-Day - Draw Symmetries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from itertools import combinations, chain
from collections import defaultdict
from pyx import *
### AUXILIARY FUNCTIONS AND DATA STRUCTURES ####################################
def is_connected(pixels):
# DFS
stack = [pixels.pop()]
while stack:
(x,y) = stack.pop()
if (x+1,y) in pixels:
stack.append( (x+1,y))
pixels.remove((x+1,y))
if (x,y+1) in pixels:
stack.append( (x,y+1))
pixels.remove((x,y+1))
if (x-1,y) in pixels:
stack.append( (x-1,y))
pixels.remove((x-1,y))
if (x,y-1) in pixels:
stack.append( (x,y-1))
pixels.remove((x,y-1))
return len(pixels) == 0
def symmetries(pieces):
# Normalize:
pixels = tuple(chain.from_iterable(pieces))
min_x = min(x for (x,y) in pixels)
min_y = min(y for (x,y) in pixels)
pixels = tuple((x-min_x,y-min_y) for (x,y) in pixels)
max_x = max(x for (x,y) in pixels)
max_y = max(y for (x,y) in pixels)
# Check symmetries:
v_sym = all((max_x-x, y) in pixels for (x,y) in pixels)
h_sym = all(( x, max_y-y) in pixels for (x,y) in pixels)
d_sym = all(( y, x) in pixels for (x,y) in pixels)
D_sym = all((max_x-y, max_y-x) in pixels for (x,y) in pixels)
r_sym = all((max_x-x, max_y-y) in pixels for (x,y) in pixels)
return (v_sym or h_sym, d_sym or D_sym, r_sym)
def normalize(pieces):
# Flatten pieces:
pixels = tuple(chain.from_iterable(pieces))
# Bounding box:
min_x = min(x for (x,y) in pixels)
min_y = min(y for (x,y) in pixels)
max_x = max(x for (x,y) in pixels)
max_y = max(y for (x,y) in pixels)
# Return the best variant:
return min((tuple(sorted((x-min_x, y-min_y) for (x,y) in pixels)),
tuple( tuple(sorted((x-min_x, y-min_y) for (x,y) in p)) for p in pieces)),
(tuple(sorted((x-min_x, max_y-y) for (x,y) in pixels)),
tuple( tuple(sorted((x-min_x, max_y-y) for (x,y) in p)) for p in pieces)),
(tuple(sorted((max_x-x, y-min_y) for (x,y) in pixels)),
tuple( tuple(sorted((max_x-x, y-min_y) for (x,y) in p)) for p in pieces)),
(tuple(sorted((max_x-x, max_y-y) for (x,y) in pixels)),
tuple( tuple(sorted((max_x-x, max_y-y) for (x,y) in p)) for p in pieces)),
(tuple(sorted((y-min_y, x-min_x) for (x,y) in pixels)),
tuple( tuple(sorted((y-min_y, x-min_x) for (x,y) in p)) for p in pieces)),
(tuple(sorted((y-min_y, max_x-x) for (x,y) in pixels)),
tuple( tuple(sorted((y-min_y, max_x-x) for (x,y) in p)) for p in pieces)),
(tuple(sorted((max_y-y, x-min_x) for (x,y) in pixels)),
tuple( tuple(sorted((max_y-y, x-min_x) for (x,y) in p)) for p in pieces)),
(tuple(sorted((max_y-y, max_x-x) for (x,y) in pixels)),
tuple( tuple(sorted((max_y-y, max_x-x) for (x,y) in p)) for p in pieces)))[1]
def contour(pixels):
# Get all segments:
segments = []
for x,y in pixels:
if (x-1,y) not in pixels: segments.append(((x, y), (x, y+1)))
if (x+1,y) not in pixels: segments.append(((x+1, y), (x+1,y+1)))
if (x,y-1) not in pixels: segments.append(((x, y), (x+1,y )))
if (x,y+1) not in pixels: segments.append(((x, y+1), (x+1,y+1)))
# Remove redundant points:
points = set([s[0] for s in segments] + [s[1] for s in segments])
for p in points:
q,r = [_ for _ in points if (p,_) in segments or (_,p) in segments]
if p[0] == q[0] == r[0] or p[1] == q[1] == r[1]:
if (p,q) in segments: segments.remove((p,q))
if (q,p) in segments: segments.remove((q,p))
if (p,r) in segments: segments.remove((p,r))
if (r,p) in segments: segments.remove((r,p))
segments.append((q,r))
# Get polygon:
q = segments[0][0]
p, r = [_ for _ in points if (q,_) in segments or (_,q) in segments]
polygon = [p, q, r]
while polygon[-1] != polygon[0]:
q = polygon[-1]
p, r = [_ for _ in points if (q,_) in segments or (_,q) in segments]
if p == polygon[-2]: polygon.append(r)
else: polygon.append(p)
return polygon[1:]
def draw_symmetries(name, pieces, improve=True):
# Normalize:
if improve:
pixels = tuple(chain.from_iterable(pieces))
min_x = min(x for (x,y) in pixels)
min_y = min(y for (x,y) in pixels)
pixels = tuple((x-min_x,y-min_y) for (x,y) in pixels)
max_x = max(x for (x,y) in pixels)
max_y = max(y for (x,y) in pixels)
# Check symmetries:
v_sym = all((max_x-x, y) in pixels for (x,y) in pixels)
h_sym = all(( x, max_y-y) in pixels for (x,y) in pixels)
d_sym = all(( y, x) in pixels for (x,y) in pixels)
D_sym = all((max_x-y, max_y-x) in pixels for (x,y) in pixels)
if D_sym and not d_sym:
pieces = tuple(tuple((x,max_y-y) for (x,y) in p) for p in pieces)
if h_sym and not v_sym:
pieces = tuple(tuple((y,x) for (x,y) in p) for p in pieces)
# DRAW:
drawing = []
BASE = [style.linecap.round, style.linejoin.round, style.linestyle.solid]
BASE += [style.linewidth.THick, color.rgbfromhexstring("#000000")]
BASE += [deco.filled([color.rgbfromhexstring("#CCCCCC")])]
for piece in pieces:
polygon = contour(piece)
if len(polygon) == 4:
drawing.append((path.path(path.moveto(*polygon[0]),
path.lineto(*polygon[1]),
path.lineto(*polygon[2]),
path.lineto(*polygon[3]),
path.closepath()), BASE))
elif len(polygon) == 6:
drawing.append((path.path(path.moveto(*polygon[0]),
path.lineto(*polygon[1]),
path.lineto(*polygon[2]),
path.lineto(*polygon[3]),
path.lineto(*polygon[4]),
path.lineto(*polygon[5]),
path.closepath()), BASE))
elif len(polygon) == 8:
drawing.append((path.path(path.moveto(*polygon[0]),
path.lineto(*polygon[1]),
path.lineto(*polygon[2]),
path.lineto(*polygon[3]),
path.lineto(*polygon[4]),
path.lineto(*polygon[5]),
path.lineto(*polygon[6]),
path.lineto(*polygon[7]),
path.closepath()), BASE))
else: print("WRONG POLYGON:", polygon)
mycanvas = canvas.canvas()
for (p, s) in drawing: mycanvas.stroke(p, s)
mycanvas.writePDFfile("./symmetries/"+name)
### MAIN FUNCTION ##############################################################
if __name__ == "__main__":
# Read Puzzles
puzzles = []
with open("solutions.txt", 'r') as solutions:
for solution in solutions:
puzzles.append(tuple(eval(solution[12:])[2:]))
# Classify symmetric shapes
min_pieces = 2
max_pieces = 7
shapes = defaultdict(set)
for puzzle in puzzles:
for num_pieces in range(min_pieces, max_pieces+1):
for pieces in combinations(puzzle, num_pieces):
if is_connected(list(chain.from_iterable(pieces))):
pieces = normalize(pieces)
sym = symmetries(pieces)
if any(sym): shapes[num_pieces, sym].add(pieces)
# Draw symmetric shapes
for num_pieces, sym in sorted(shapes.keys()):
print("Sym-{}-{}{}{}-{:04d}".format(num_pieces,
"m" if sym[0] else "",
"d" if sym[1] else "",
"r" if sym[2] else "",
len(shapes[num_pieces, sym])))
for i, pieces in enumerate(shapes[num_pieces, sym]):
name = "Sym-{}-{}{}{}-{:04d}".format(num_pieces,
"m" if sym[0] else "",
"d" if sym[1] else "",
"r" if sym[2] else "",
i)
draw_symmetries(name, pieces)
################################################################################