-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.py
More file actions
369 lines (292 loc) · 11.6 KB
/
structs.py
File metadata and controls
369 lines (292 loc) · 11.6 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from dataclasses import InitVar, dataclass
from predicate import Predicate
from typing import OrderedDict
@dataclass(eq=True, frozen=True)
class Vector:
x: int
y: int
z: int
def rotateX(self):
return Vector(self.x, self.z, -self.y)
def rotateY(self):
return Vector(self.z, self.y, -self.x)
def rotateZ(self):
return Vector(-self.y, self.x, self.z)
def halfX(self):
return Vector(self.x, -self.y, -self.z)
def halfY(self):
return Vector(-self.x, self.y, -self.z)
def halfZ(self):
return Vector(-self.x, -self.y, self.z)
def rrotateX(self):
return Vector(self.x, -self.z, self.y)
def rrotateY(self):
return Vector(-self.z, self.y, self.x)
def rrotateZ(self):
return Vector(self.y, -self.x, self.z)
def cross(self, o):
return Vector(self.y*o.z - o.y*self.z, o.x*self.z - self.x*o.z, self.x*o.y - self.y*o.x)
def copy(self):
return Vector(self.x, self.y, self.z)
def __add__(one, two):
return Vector(one.x+two.x, one.y+two.y, one.z+two.z)
def __mul__(self, scalar):
return Vector(self.x*scalar, self.y*scalar, self.z*scalar)
def __eq__(self, o):
return self.x == o.x and self.y == o.y and self.z == o.z
def __hash__(self):
return hash((self.x, self.y, self.z))
def __str__(self) -> str:
return f'[{self.x}, {self.y}, {self.z}]'
def __repr__(self) -> str:
return self.__str__()
def __iter__(self):
return iter((self.x,self.y,self.z))
class Matrix:
def __init__(self, c1, c2, c3):
self.c1 = c1
self.c2 = c2
self.c3 = c3
def __mul__(self, v):
x = self.c1.x*v.x + self.c2.x*v.y + self.c3.x*v.z
y = self.c1.y*v.x + self.c2.y*v.y + self.c3.y*v.z
z = self.c1.z*v.x + self.c2.z*v.y + self.c3.z*v.z
return Vector(x, y, z)
@staticmethod
def rotor(v1, v2):
return Matrix(v1, v2, v1.cross(v2))
class Block:
initial_orientations = [Vector(1, 0, 0), Vector(0, 1, 0)]
def __init__(self, solved_location, actual_location=None, orientations=None):
self.solved_location = solved_location
self.actual_location = actual_location or solved_location.copy()
self.orientations = orientations or Block.initial_orientations
self._hash = None
def apply(self, action):
if action.applies(self):
actual_location = action.transform.__call__(self.actual_location)
orientations = [action.transform.__call__(o) for o in self.orientations]
return Block(self.solved_location, actual_location, orientations)
else:
return self
def solved(self):
return self.solved_location == self.actual_location and (self.orientations == Block.initial_orientations or self.is_center())
def is_corner(self):
l = self.solved_location
return l.x != 0 and l.y != 0 and l.z != 0
def is_center(self):
l = self.solved_location
return (l.x == 0) + (l.y == 0) + (l.z == 0) == 2
def is_edge(self):
return not self.is_center() and not self.is_corner()
def correct_orbit(self):
if not self.is_corner():
return True
xMatch = self.solved_location.x == self.actual_location.x
yMatch = self.solved_location.y == self.actual_location.y
zMatch = self.solved_location.z == self.actual_location.z
sMatch = xMatch + yMatch + zMatch
return sMatch == 3 or sMatch == 1
def correct_slice(self):
if not self.is_edge():
return True
solved_slice = [self.solved_location.x == 0, self.solved_location.y == 0, self.solved_location.z == 0]
actual_slice = [self.actual_location.x == 0, self.actual_location.y == 0, self.actual_location.z == 0]
return any((f and s for (f,s) in zip(solved_slice, actual_slice)))
def turn_distance(self):
right_location = self.solved_location == self.actual_location
right_o = (self.orientations == Block.initial_orientations)
if right_location and right_o:
return 0
if right_location and not right_o:
return 3
if self.is_corner():
return (self.solved_location.x != self.actual_location.x) + (self.solved_location.y != self.actual_location.y) + (self.solved_location.z != self.actual_location.z)
return 1
def half_turn_distance(self):
if self.solved():
return 0
xMatch = self.solved_location.x == self.actual_location.x
yMatch = self.solved_location.y == self.actual_location.y
zMatch = self.solved_location.z == self.actual_location.z
sMatch = xMatch + yMatch + zMatch
if self.is_corner():
return 1 if sMatch >= 1 else 2
if self.is_edge():
return 3 - sMatch
def __str__(self) -> str:
return f'(L=[{self.actual_location.x}, {self.actual_location.y}, {self.actual_location.z}], O=[{self.orientations[0].x}, {self.orientations[0].y}, {self.orientations[0].z}])'
def __repr__(self) -> str:
return self.__str__()
def __eq__(self, other):
return self.actual_location == other.actual_location and self.orientations == other.orientations # and self.solved_location == other.solved_location
def __hash__(self):
if not self._hash:
self._hash = hash((self.solved_location, self.actual_location, self.orientations[0], self.orientations[1]))
return self._hash
class Cube:
def __init__(self, size, blocks=None, decorative_blocks=[]):
self.size = size
if blocks == None:
self.blocks = []
even = size % 2 == 0
half = int(size / 2)
for i in range(-half, half+1):
for j in range(-half, half+1):
for k in range(-half, half+1):
interior = all([abs(x) != half for x in (i, j, k)])
if not (even and i*j*k == 0) and not interior:
self.blocks.append(Block(Vector(i, j, k)))
self.decorative_blocks = [b for b in self.blocks if center(b)]
self.blocks = [b for b in self.blocks if not center(b)]
else:
self.blocks = blocks
self.decorative_blocks = decorative_blocks
self._hash = None
def apply(self, action):
blocks = [b.apply(action) for b in self.blocks]
return Cube(self.size, blocks=blocks, decorative_blocks=self.decorative_blocks)
def sub_cube(self, block_selector):
blocks = []
for b in self.blocks:
if block_selector(b):
blocks.append(b)
blocks = [b for b in self.blocks if block_selector(b)]
decor = [b for b in self.decorative_blocks if block_selector(b)]
return Cube(self.size, blocks, decorative_blocks = decor)
def sub_cube_from_cube(self, cube):
solved_positions = set((s.solved_location for s in cube.blocks))
return self.sub_cube(lambda c: c.solved_location in solved_positions)
def solved(self):
return all((b.solved() for b in self.blocks))
def turn_distance(self):
return sum((b.turn_distance() for b in self.blocks))
def half_turn_distance(self):
return sum((b.half_turn_distance() for b in self.blocks))
def __eq__(self, other):
return self.blocks == other.blocks
def __hash__(self):
# return super().__hash__()
if not self._hash:
self._hash = hash(tuple([hash(b) for b in self.blocks]))
return self._hash
class Action:
_id_generator = 0
def __init__(self, applies, axis, transform, inverse=None):
self.applies = applies
self.transform = transform
self.axis = axis
if isinstance(inverse, Action):
self.inverse = inverse
elif inverse is None:
self.inverse = self
else:
self.inverse = Action(applies, axis, inverse, self)
def id(self):
if not hasattr(self, '_id'):
Action._id_generator += 1
self._id = Action._id_generator
return self._id
def __hash__(self) -> int:
return self.id()
def __eq__(self, other) -> bool:
return self.id() == other.id()
def filter_actions(actions, previous_action):
if not previous_action:
return actions
new_actions = []
for a in actions:
if a == previous_action:
continue
if previous_action.inverse == a:
continue
if previous_action.axis == a.axis and a.id() < previous_action.id():
continue
new_actions.append(a)
return new_actions
@Predicate
def top(block: Block):
l = block.solved_location
x = l.x
y = l.y
z = l.z
return z == 1
def solved(attrib, val):
@Predicate
def f(block: Block):
return block.solved_location.__getattribute__(attrib) == val
return f
@Predicate
def edge(block: Block):
l = block.solved_location
x = l.x
y = l.y
z = l.z
return (x == 0) ^ (y == 0) ^ (z == 0)
@Predicate
def corner(block: Block):
l = block.solved_location
x = l.x
y = l.y
z = l.z
return (x != 0) and (y != 0) and (z != 0)
@Predicate
def center(block: Block):
l = block.solved_location
x = l.x
y = l.y
z = l.z
return ((x == 0) + (y == 0) + (z == 0)) == 2
QUARTER_X = [
Action(lambda b: b.actual_location.x == 1, "x", Vector.rotateX, Vector.rrotateX),
Action(lambda b: b.actual_location.x == -1, "x", Vector.rotateX, Vector.rrotateX),
]
QUARTER_Y = [
Action(lambda b: b.actual_location.y == 1, "y", Vector.rotateY, Vector.rrotateY),
Action(lambda b: b.actual_location.y == -1, "y", Vector.rotateY, Vector.rrotateY),
]
QUARTER_Z = [
Action(lambda b: b.actual_location.z == 1, "z", Vector.rotateZ, Vector.rrotateZ),
Action(lambda b: b.actual_location.z == -1, "z", Vector.rotateZ, Vector.rrotateZ),
]
QUARTER_X = QUARTER_X + list(map(lambda a: a.inverse, QUARTER_X))
QUARTER_Y = QUARTER_Y + list(map(lambda a: a.inverse, QUARTER_Y))
QUARTER_Z = QUARTER_Z + list(map(lambda a: a.inverse, QUARTER_Z))
QUARTER_FACE = QUARTER_X + QUARTER_Y + QUARTER_Z
HALF_X = [
Action(lambda b: b.actual_location.x == 1, "x", Vector.halfX),
Action(lambda b: b.actual_location.x == -1, "x", Vector.halfX),
]
HALF_Y = [
Action(lambda b: b.actual_location.y == 1, "y", Vector.halfY),
Action(lambda b: b.actual_location.y == -1, "y", Vector.halfY),
]
HALF_Z = [
Action(lambda b: b.actual_location.z == 1, "z", Vector.halfZ),
Action(lambda b: b.actual_location.z == -1, "z", Vector.halfZ),
]
HALF_FACE = HALF_X + HALF_Y + HALF_Z
QUARTER_SLICE = [
Action(lambda b: abs(b.actual_location.x) == 1, "x", Vector.rotateX, Vector.rrotateX),
Action(lambda b: abs(b.actual_location.y) == 1, "y", Vector.rotateY, Vector.rrotateY),
Action(lambda b: abs(b.actual_location.z) == 1, "z", Vector.rotateZ, Vector.rrotateZ),
]
QUARTER_SLICE = QUARTER_SLICE + list(map(lambda a: a.inverse, QUARTER_SLICE))
HALF_SLICE = [
Action(lambda b: abs(b.actual_location.x) == 1, "x", Vector.halfX),
Action(lambda b: abs(b.actual_location.y) == 1, "y", Vector.halfY),
Action(lambda b: abs(b.actual_location.z) == 1, "z", Vector.halfZ),
]
ACTIONS = QUARTER_FACE + HALF_FACE
if __name__ == "__main__":
# Performance testing
import random
import timeit
iterations = 40_000
cube = Cube(3)
start = timeit.default_timer()
for i in range(iterations):
ai = random.randrange(len(ACTIONS))
cube = cube.apply(ACTIONS[ai])
end = timeit.default_timer()
print(f'Actions per second {iterations/(end-start)}')