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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#!/usr/bin/env python3
from __future__ import print_function
import numpy as np
import itertools
import sys
class Stick(object):
XPAR = 0
YPAR = 1
ZPAR = 2
def __init__(self, name, array):
self.name = name
self.array = np.asarray(array)
@property
def dir(self):
if len(self.array) > 2:
return self.ZPAR
elif len(self.array[0]) > 2:
return self.YPAR
return self.XPAR
def rot(self, axis, n=1):
if n == 0:
return self
if axis == 0: # X
newArray = np.rot90(self.array, n, (0, 1))
elif axis == 1: # Y
newArray = np.rot90(self.array, n, (0, 2))
elif axis == 2: # Z
newArray = np.rot90(self.array, n, (1, 2))
return self.__class__(self.name, newArray)
def __genXZRotations(self):
self.rotX = self.rot(0)
self.rotZ = self.rot(2)
def __genBasicRotations(self):
self.basicRotations = []
for rotY in (0, 1, 2, 3):
for rotX in (0, 2):
r = self.rot(0, rotX).rot(1, rotY)
for other in self.basicRotations:
if np.array_equal(other.array, r.array):
break
else:
r.__genXZRotations()
self.basicRotations.append(r)
def genRotations(self):
self.__genXZRotations()
self.rotX.__genBasicRotations()
self.rotZ.__genBasicRotations()
self.__genBasicRotations()
def __str__(self):
if self.dir == self.XPAR:
ret = [ self.name + " " ]
for y in range(2):
if y > 0:
ret.append(" " * (len(self.name) + 1))
for x in range(16):
if x <= 5 or x >= 10:
ret.append("=")
else:
xx = x - 6
if self.array[0][y][xx] and\
self.array[1][y][xx]:
ret.append("#")
elif self.array[0][y][xx]:
ret.append("@")
elif self.array[1][y][xx]:
ret.append("*")
else:
ret.append(" ")
ret.append("\n")
elif self.dir == self.YPAR:
ret = [ self.name + "\n" ]
for y in range(16):
for x in range(2):
if y <= 5 or y >= 10:
ret.append("=")
else:
yy = y - 6
if self.array[0][yy][x] and\
self.array[1][yy][x]:
ret.append("#")
elif self.array[0][yy][x]:
ret.append("@")
elif self.array[1][yy][x]:
ret.append("*")
else:
ret.append(" ")
ret.append("\n")
else:
ret = [ self.name + " " ]
for y in range(2):
if y > 0:
ret.append(" " * (len(self.name) + 1))
ret.append("|")
for x in range(2):
s = np.sum(self.array, 0)
ret.append(str(s[y][x]))
ret.append("|\n")
return "".join(ret)
def __repr__(self):
return repr(self.array).replace("array", "Stick")
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return self.name == other.name
def __ne__(self, other):
return self.name != other.name
class Solution(object):
def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
def rot(self, axis):
new = self.__class__(self.a, self.b, self.c, self.d, self.e, self.f)
new.a = new.a.rot(axis, 2)
new.b = new.b.rot(axis, 2)
new.c = new.c.rot(axis, 2)
new.d = new.d.rot(axis, 2)
new.e = new.e.rot(axis, 2)
new.f = new.f.rot(axis, 2)
if axis == 0:
new.c, new.d = new.d, new.c
new.e, new.f = new.f, new.e
elif axis == 1:
new.a, new.b = new.b, new.a
new.c, new.d = new.d, new.c
elif axis == 2:
new.a, new.b = new.b, new.a
new.e, new.f = new.f, new.e
return new
def __eq_exact(self, other):
return np.array_equal(self.a.array, other.a.array) and\
np.array_equal(self.b.array, other.b.array) and\
np.array_equal(self.c.array, other.c.array) and\
np.array_equal(self.d.array, other.d.array) and\
np.array_equal(self.e.array, other.e.array) and\
np.array_equal(self.f.array, other.f.array)
def __eq__(self, other):
sx = self
#FIXME this is wrong
for x in range(2):
if x > 0:
sx = sx.rot(0)
sy = sx
for y in range(2):
if y > 0:
sy = sy.rot(1)
sz = sy
for z in range(2):
if z > 0:
sz = sz.rot(2)
if sz.__eq_exact(other):
return True
return False
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
a, b, c, d, e, f = self.a, self.b, self.c, self.d, self.e, self.f
ret = "a=%s, b=%s, c=%s, d=%s e=%s f=%s\n" % (
a.name, b.name, c.name, d.name, e.name, f.name)
lines = []
for lineA, lineB in zip(str(a).splitlines(), str(b).splitlines()):
lines.append(lineA + " " + lineB)
ret += "\n".join(lines) + "\n\n"
ret += "\n".join((str(c), str(d), str(e), str(f)))
return ret
s1 = Stick("s1", (
( (1, 1),
(1, 1),
(1, 1),
(1, 1), ),
( (1, 1),
(1, 1),
(1, 1),
(1, 1), ), ))
s2 = Stick("s2", (
( (0, 0),
(0, 0),
(0, 0),
(0, 0), ),
( (1, 1),
(1, 1),
(1, 1),
(1, 1), ), ))
s3 = Stick("s3", (
( (1, 0),
(0, 0),
(0, 0),
(1, 1), ),
( (1, 0),
(1, 0),
(1, 1),
(1, 1), ), ))
s4 = Stick("s4", (
( (0, 1),
(0, 0),
(0, 0),
(1, 1), ),
( (0, 1),
(0, 1),
(1, 1),
(1, 1), ), ))
s5 = Stick("s5", (
( (0, 0),
(0, 0),
(0, 0),
(0, 0), ),
( (1, 1),
(0, 1),
(0, 1),
(1, 1), ), ))
s6 = Stick("s6", (
( (0, 1),
(0, 0),
(0, 0),
(0, 1), ),
( (0, 1),
(1, 1),
(1, 1),
(0, 1), ), ))
sticks = {s1, s2, s3, s4, s5, s6}
for s in sticks:
s.genRotations()
emptyPlaneXY = np.asarray(
(
( (0, 0, 0, 0),
(0, 0, 0, 0),
(0, 0, 0, 0),
(0, 0, 0, 0), ),
)
)
emptyPlaneYZ = np.rot90(emptyPlaneXY, 1, (0, 2))
emptyPlaneXZ = np.rot90(emptyPlaneXY, 1, (0, 1))
cornersXY = np.asarray(
(
( (1, 0, 0, 1),
(0, 0, 0, 0),
(0, 0, 0, 0),
(1, 0, 0, 1), ),
( (1, 0, 0, 1),
(0, 0, 0, 0),
(0, 0, 0, 0),
(1, 0, 0, 1), ),
)
)
cornersYZ = np.rot90(cornersXY, 1, (0, 2))
cornersXZ = np.rot90(cornersXY, 1, (0, 1))
def processZPlane(a, b, c, d, cRot, dRot, abcd):
remainingSticks = sticks - {a, b, c, d}
for _e, _f in itertools.permutations(remainingSticks, 2):
for e in _e.basicRotations:
eRot = e.rotX
for f in _f.basicRotations:
fRot = f.rotX
ef = np.hstack((eRot.array, fRot.array))
if np.any(ef - cornersYZ < 0):
continue # At least one corner is not filled.
efFilled = np.dstack((emptyPlaneYZ, ef, emptyPlaneYZ))
abcdef = abcd + efFilled
if not np.any(abcdef > 1):
yield Solution(a, b, cRot, dRot, eRot, fRot)
def processXYPlane():
for _a, _b, _c, _d in itertools.permutations(sticks, 4):
for a in _a.basicRotations:
for b in _b.basicRotations:
ab = np.dstack((a.array, b.array))
if np.any(ab - cornersXY < 0):
continue # At least one corner is not filled.
for c in _c.basicRotations:
cRot = c.rotZ
for d in _d.basicRotations:
dRot = d.rotZ
cd = np.vstack((cRot.array, dRot.array))
if np.any(cd - cornersXZ < 0):
continue # At least one corner is not filled.
abFilled = np.vstack((emptyPlaneXY, ab, emptyPlaneXY))
cdFilled = np.hstack((emptyPlaneXZ, cd, emptyPlaneXZ))
abcd = abFilled + cdFilled
if np.any(abcd > 1):
continue # This one collides in the XY plane already.
yield from processZPlane(a, b, c, d, cRot, dRot, abcd)
solutions = []
for solution in processXYPlane():
for otherSolution in solutions:
if solution == otherSolution:
break # We have that one already
else:
solutions.append(solution)
print("Found %s solution(s).\n" % len(solutions))
for i, solution in enumerate(solutions):
print("Solution #%d: %s" % (i + 1, solution))
|