-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
353 lines (325 loc) · 8.49 KB
/
model.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
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
import itertools
import random
# Directions
NORTH = 'N'
EAST = 'E'
SOUTH = 'S'
WEST = 'W'
DIRECTIONS = [NORTH, EAST, SOUTH, WEST]
REVERSE = {
NORTH: SOUTH,
EAST: WEST,
SOUTH: NORTH,
WEST: EAST,
}
OFFSET = {
NORTH: -16,
EAST: 1,
SOUTH: 16,
WEST: -1,
}
# Masks
M_NORTH = 0x01
M_EAST = 0x02
M_SOUTH = 0x04
M_WEST = 0x08
M_ROBOT = 0x10
M_LOOKUP = {
NORTH: M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H: WEST,
WEST: NORTH,
}
# Helper Functions
def idx(x, y, size=16):
return y * size + x
def xy(index, size=16):
x = index % size
y = index / size
return (x, y)
def rotate_quad(data, times=1):
for i in range(times):
result = [data[index] for index in ROTATE_QUAD]
result = [''.join(ROTATE_WALL.get(c, c) for c in x) for x in result]
data = result
return data
def create_grid(quads=None):
if quads is None:
quads = [random.choice(pair) for pair in QUADS]
random.shuffle(quads)
quads = [quad.split(',') for quad in quads]
quads = [rotate_quad(quads[i], i) for i in [0, 1, 3, 2]]
result = [None for i in range(16 * 16)]
for i, quad in enumerate(quads):
dx, dy = xy(i, 2)
for j, data in enumerate(quad):
x, y = xy(j, 8)
x += dx * 8
y += dy * 8
index = idx(x, y)
result[index] = data
return result
def to_mask(cell):
result = 0
for letter, mask in M_LOOKUP.items():
if letter in cell:
result |= mask
return result
# Game
class Game(object):
@staticmethod
def hardest():
quads = [QUAD_2B, QUAD_4B, QUAD_3B, QUAD_1B]
robots = [226, 48, 43, 18]
token = 'BT'
return Game(quads=quads, robots=robots, token=token)
def __init__(self, seed=None, quads=None, robots=None, token=None):
if seed:
random.seed(seed)
self.grid = create_grid(quads)
if robots is None:
self.robots = self.place_robots()
else:
self.robots = dict(zip(COLORS, robots))
self.token = token or random.choice(TOKENS)
self.moves = 0
self.last = None
def place_robots(self):
result = {}
used = set()
for color in COLORS:
while True:
index = random.randint(0, 255)
if index in (119, 120, 135, 136):
continue
if self.grid[index][-2:] in TOKENS:
continue
if index in used:
continue
result[color] = index
used.add(index)
break
return result
def get_robot(self, index):
for color, position in self.robots.iteritems():
if position == index:
return color
return None
def can_move(self, color, direction):
if self.last == (color, REVERSE[direction]):
return False
index = self.robots[color]
if direction in self.grid[index]:
return False
new_index = index + OFFSET[direction]
if new_index in self.robots.itervalues():
return False
return True
def compute_move(self, color, direction):
index = self.robots[color]
robots = self.robots.values()
while True:
if direction in self.grid[index]:
break
new_index = index + OFFSET[direction]
if new_index in robots:
break
index = new_index
return index
def do_move(self, color, direction):
start = self.robots[color]
last = self.last
if last == (color, REVERSE[direction]):
raise Exception
end = self.compute_move(color, direction)
if start == end:
raise Exception
self.moves += 1
self.robots[color] = end
self.last = (color, direction)
return (color, start, last)
def undo_move(self, data):
color, start, last = data
self.moves -= 1
self.robots[color] = start
self.last = last
def get_moves(self, colors=None):
result = []
colors = colors or COLORS
for color in colors:
for direction in DIRECTIONS:
if self.can_move(color, direction):
result.append((color, direction))
return result
def over(self):
color = self.token[0]
return self.token in self.grid[self.robots[color]]
def key(self):
return tuple(self.robots.itervalues())
def search(self):
max_depth = 1
while True:
#print 'Searching to depth:', max_depth
result = self._search([], set(), 0, max_depth)
if result is not None:
return result
max_depth += 1
def _search(self, path, memo, depth, max_depth):
if self.over():
return list(path)
if depth == max_depth:
return None
key = (depth, self.key())
if key in memo:
return None
memo.add(key)
if depth == max_depth - 1:
colors = [self.token[0]]
else:
colors = None
moves = self.get_moves(colors)
for move in moves:
data = self.do_move(*move)
path.append(move)
result = self._search(path, memo, depth + 1, max_depth)
path.pop(-1)
self.undo_move(data)
if result:
return result
return None
def export(self):
grid = []
token = None
robots = [self.robots[color] for color in COLORS]
for index, cell in enumerate(self.grid):
mask = to_mask(cell)
if index in robots:
mask |= M_ROBOT
grid.append(mask)
if self.token in cell:
token = index
robot = COLORS.index(self.token[0])
return {
'grid': grid,
'robot': robot,
'token': token,
'robots': robots,
}