Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

imported patch whitespace

  • Loading branch information...
commit c8badeab931be2da50e817a6504ea7e2c00bb790 1 parent 6dee864
Robert Bradshaw authored December 21, 2010

Showing 1 changed file with 324 additions and 324 deletions. Show diff stats Hide diff stats

  1. 648  meteor_cython.pyx
648  meteor_cython.pyx
@@ -10,7 +10,7 @@ from stdio cimport *
10 10
 from stdlib cimport *
11 11
 
12 12
 cdef extern from *:
13  
-   void qsort (void *base, unsigned short n, unsigned short w, int (*cmp_func)(void*, void*))
  13
+    void qsort (void *base, unsigned short n, unsigned short w, int (*cmp_func)(void*, void*))
14 14
 
15 15
 
16 16
 # The board is a 50 cell hexagonal pattern.  For    . . . . .
@@ -63,16 +63,16 @@ cdef char ENE   = 11
63 63
 cdef char PIVOT = 12
64 64
 
65 65
 piece_list = [
66  
-   [  E,  E,  E, SE],
67  
-   [ SE,  E, NE,  E],
68  
-   [  E,  E, SE, SW],
69  
-   [  E,  E, SW, SE],
70  
-   [ SE,  E, NE,  S],
71  
-   [  E,  E, SW,  E],
72  
-   [  E, SE, SE, NE],
73  
-   [  E, SE, SE,  W],
74  
-   [  E, SE,  E,  E],
75  
-   [  E,  E,  E, SW]
  66
+    [  E,  E,  E, SE],
  67
+    [ SE,  E, NE,  E],
  68
+    [  E,  E, SE, SW],
  69
+    [  E,  E, SW, SE],
  70
+    [ SE,  E, NE,  S],
  71
+    [  E,  E, SW,  E],
  72
+    [  E, SE, SE, NE],
  73
+    [  E, SE, SE,  W],
  74
+    [  E, SE,  E,  E],
  75
+    [  E,  E,  E, SW]
76 76
 ]
77 77
 cdef char piece_def[10][4]
78 78
 for i, piece in enumerate(piece_list):
@@ -96,11 +96,11 @@ cdef char next_cell[10][50][12]
96 96
 
97 97
 # Returns the direction rotated 60 degrees clockwise
98 98
 cdef char rotate(char dir):
99  
-   return (dir + 2) % PIVOT
  99
+    return (dir + 2) % PIVOT
100 100
 
101 101
 # Returns the direction flipped on the horizontal axis
102 102
 cdef char flip(char dir):
103  
-   return (PIVOT - dir) % PIVOT
  103
+    return (PIVOT - dir) % PIVOT
104 104
 
105 105
 
106 106
 # Returns the new cell index from the specified cell in the
@@ -110,164 +110,164 @@ cdef char flip(char dir):
110 110
 #
111 111
 cdef char shift(char cell, char dir):
112 112
 # TODO don't branch on %2
113  
-      if dir == E:
114  
-         return cell + 1
115  
-      elif dir == ESE:
116  
-         if (cell / 5) % 2:
117  
-            return cell + 7
118  
-         else:
119  
-            return cell + 6
120  
-      elif dir == SE:
121  
-         if (cell / 5) % 2:
122  
-            return cell + 6
123  
-         else:
124  
-            return cell + 5
125  
-      elif dir == S:
126  
-         return cell + 10
127  
-      elif dir == SW:
128  
-         if (cell / 5) % 2:
129  
-            return cell + 5
130  
-         else:
131  
-            return cell + 4
132  
-      elif dir == WSW:
133  
-         if (cell / 5) % 2:
134  
-            return cell + 4
135  
-         else:
136  
-            return cell + 3
137  
-      elif dir == W:
138  
-         return cell - 1
139  
-      elif dir == WNW:
140  
-         if (cell / 5) % 2:
141  
-            return cell - 6
142  
-         else:
143  
-            return cell - 7
144  
-      elif dir == NW:
145  
-         if (cell / 5) % 2:
146  
-            return cell - 5
147  
-         else:
148  
-            return cell - 6
149  
-      elif dir == N:
150  
-         return cell - 10
151  
-      elif dir == NE:
152  
-         if (cell / 5) % 2:
153  
-            return cell - 4
154  
-         else:
155  
-            return cell - 5
156  
-      elif dir == ENE:
157  
-         if (cell / 5) % 2:
158  
-            return cell - 3
159  
-         else:
160  
-            return cell - 4
161  
-      else:
162  
-         return cell
  113
+        if dir == E:
  114
+            return cell + 1
  115
+        elif dir == ESE:
  116
+            if (cell / 5) % 2:
  117
+                return cell + 7
  118
+            else:
  119
+                return cell + 6
  120
+        elif dir == SE:
  121
+            if (cell / 5) % 2:
  122
+                return cell + 6
  123
+            else:
  124
+                return cell + 5
  125
+        elif dir == S:
  126
+            return cell + 10
  127
+        elif dir == SW:
  128
+            if (cell / 5) % 2:
  129
+                return cell + 5
  130
+            else:
  131
+                return cell + 4
  132
+        elif dir == WSW:
  133
+            if (cell / 5) % 2:
  134
+                return cell + 4
  135
+            else:
  136
+                return cell + 3
  137
+        elif dir == W:
  138
+            return cell - 1
  139
+        elif dir == WNW:
  140
+            if (cell / 5) % 2:
  141
+                return cell - 6
  142
+            else:
  143
+                return cell - 7
  144
+        elif dir == NW:
  145
+            if (cell / 5) % 2:
  146
+                return cell - 5
  147
+            else:
  148
+                return cell - 6
  149
+        elif dir == N:
  150
+            return cell - 10
  151
+        elif dir == NE:
  152
+            if (cell / 5) % 2:
  153
+                return cell - 4
  154
+            else:
  155
+                return cell - 5
  156
+        elif dir == ENE:
  157
+            if (cell / 5) % 2:
  158
+                return cell - 3
  159
+            else:
  160
+                return cell - 4
  161
+        else:
  162
+            return cell
163 163
 
164 164
 # Returns wether the specified cell and direction will land outside
165 165
 # of the board.  Used to determine if a piece is at a legal board
166 166
 # location or not.
167 167
 #
168 168
 cdef bint out_of_bounds(char cell, char dir):
169  
-      cdef char i
170  
-      if dir == E:
171  
-         return cell % 5 == 4
172  
-      elif dir == ESE:
173  
-         i = cell % 10
174  
-         return i == 4 or i == 8 or i == 9 or cell >= 45
175  
-      elif dir == SE:
176  
-         return cell % 10 == 9 or cell >= 45
177  
-      elif dir == S:
178  
-         return cell >= 40
179  
-      elif dir == SW:
180  
-         return cell % 10 == 0 or cell >= 45
181  
-      elif dir == WSW:
182  
-         i = cell % 10
183  
-         return i == 0 or i == 1 or i == 5 or cell >= 45
184  
-      elif dir == W:
185  
-         return cell % 5 == 0
186  
-      elif dir == WNW:
187  
-         i = cell % 10
188  
-         return i == 0 or i == 1 or i == 5 or cell < 5
189  
-      elif dir == NW:
190  
-         return cell % 10 == 0 or cell < 5
191  
-      elif dir == N:
192  
-         return cell < 10
193  
-      elif dir == NE:
194  
-         return cell % 10 == 9 or cell < 5
195  
-      elif dir == ENE:
196  
-         i = cell % 10
197  
-         return i == 4 or i == 8 or i == 9 or cell < 5
198  
-      else:
199  
-         return False
  169
+        cdef char i
  170
+        if dir == E:
  171
+            return cell % 5 == 4
  172
+        elif dir == ESE:
  173
+            i = cell % 10
  174
+            return i == 4 or i == 8 or i == 9 or cell >= 45
  175
+        elif dir == SE:
  176
+            return cell % 10 == 9 or cell >= 45
  177
+        elif dir == S:
  178
+            return cell >= 40
  179
+        elif dir == SW:
  180
+            return cell % 10 == 0 or cell >= 45
  181
+        elif dir == WSW:
  182
+            i = cell % 10
  183
+            return i == 0 or i == 1 or i == 5 or cell >= 45
  184
+        elif dir == W:
  185
+            return cell % 5 == 0
  186
+        elif dir == WNW:
  187
+            i = cell % 10
  188
+            return i == 0 or i == 1 or i == 5 or cell < 5
  189
+        elif dir == NW:
  190
+            return cell % 10 == 0 or cell < 5
  191
+        elif dir == N:
  192
+            return cell < 10
  193
+        elif dir == NE:
  194
+            return cell % 10 == 9 or cell < 5
  195
+        elif dir == ENE:
  196
+            i = cell % 10
  197
+            return i == 4 or i == 8 or i == 9 or cell < 5
  198
+        else:
  199
+            return False
200 200
 
201 201
 # Rotate a piece 60 degrees clockwise
202 202
 cdef void rotate_piece(int piece):
203  
-   for i in range(4):
204  
-      piece_def[piece][i] = rotate(piece_def[piece][i])
  203
+    for i in range(4):
  204
+        piece_def[piece][i] = rotate(piece_def[piece][i])
205 205
 
206 206
 # Flip a piece along the horizontal axis
207 207
 cdef void flip_piece(int piece):
208  
-   for i in range(4):
209  
-      piece_def[piece][i] = flip(piece_def[piece][i])
  208
+    for i in range(4):
  209
+        piece_def[piece][i] = flip(piece_def[piece][i])
210 210
 
211 211
 # Convenience function to quickly calculate all of the indices for a piece
212 212
 cdef void calc_cell_indices(char *cell, int piece, char index):
213  
-   cell[0] = index
214  
-   cell[1] = shift(cell[0], piece_def[piece][0])
215  
-   cell[2] = shift(cell[1], piece_def[piece][1])
216  
-   cell[3] = shift(cell[2], piece_def[piece][2])
217  
-   cell[4] = shift(cell[3], piece_def[piece][3])
  213
+    cell[0] = index
  214
+    cell[1] = shift(cell[0], piece_def[piece][0])
  215
+    cell[2] = shift(cell[1], piece_def[piece][1])
  216
+    cell[3] = shift(cell[2], piece_def[piece][2])
  217
+    cell[4] = shift(cell[3], piece_def[piece][3])
218 218
 
219 219
 
220 220
 # Convenience function to quickly calculate if a piece fits on the board
221 221
 cdef int cells_fit_on_board(char *cell, int piece):
222  
-   return (not out_of_bounds(cell[0], piece_def[piece][0]) and
223  
-         not out_of_bounds(cell[1], piece_def[piece][1]) and
224  
-         not out_of_bounds(cell[2], piece_def[piece][2]) and
225  
-         not out_of_bounds(cell[3], piece_def[piece][3]))
  222
+    return (not out_of_bounds(cell[0], piece_def[piece][0]) and
  223
+            not out_of_bounds(cell[1], piece_def[piece][1]) and
  224
+            not out_of_bounds(cell[2], piece_def[piece][2]) and
  225
+            not out_of_bounds(cell[3], piece_def[piece][3]))
226 226
 
227 227
 # Returns the lowest index of the cells of a piece.
228 228
 # I use the lowest index that a piece occupies as the index for looking up
229 229
 # the piece in the solve function.
230 230
 #
231 231
 cdef char minimum_of_cells(char *cell):
232  
-    # TODO min(...)
233  
-   cdef char minimum = cell[0]
234  
-   if cell[1] < minimum: minimum = cell[1]
235  
-   if cell[2] < minimum: minimum = cell[2]
236  
-   if cell[3] < minimum: minimum = cell[3]
237  
-   if cell[4] < minimum: minimum = cell[4]
238  
-   return minimum
  232
+     # TODO min(...)
  233
+    cdef char minimum = cell[0]
  234
+    if cell[1] < minimum: minimum = cell[1]
  235
+    if cell[2] < minimum: minimum = cell[2]
  236
+    if cell[3] < minimum: minimum = cell[3]
  237
+    if cell[4] < minimum: minimum = cell[4]
  238
+    return minimum
239 239
 
240 240
 # Calculate the lowest possible open cell if the piece is placed on the board.
241 241
 # Used to later reduce the amount of time searching for open cells in the
242 242
 # solve function.
243 243
 #
244 244
 cdef char first_empty_cell(char *cell, char minimum):
245  
-   cdef char first_empty = minimum
246  
-   # TODO: in ...
247  
-   while(first_empty == cell[0] or first_empty == cell[1] or
248  
-         first_empty == cell[2] or first_empty == cell[3] or
249  
-         first_empty == cell[4]):
250  
-      first_empty += 1
251  
-   return first_empty
  245
+    cdef char first_empty = minimum
  246
+    # TODO: in ...
  247
+    while(first_empty == cell[0] or first_empty == cell[1] or
  248
+            first_empty == cell[2] or first_empty == cell[3] or
  249
+            first_empty == cell[4]):
  250
+        first_empty += 1
  251
+    return first_empty
252 252
 
253 253
 
254 254
 # Generate the unsigned long long int that will later be anded with the
255 255
 # board to determine if it fits.
256 256
 #
257 257
 cdef unsigned long long bitmask_from_cells(char *cell):
258  
-   cdef unsigned long long piece_mask = 0ULL
259  
-   for i in range(5):
260  
-      piece_mask |= 1ULL << cell[i]
261  
-   return piece_mask
  258
+    cdef unsigned long long piece_mask = 0ULL
  259
+    for i in range(5):
  260
+        piece_mask |= 1ULL << cell[i]
  261
+    return piece_mask
262 262
 
263 263
 # Record the piece and other important information in arrays that will
264 264
 # later be used by the solve function.
265 265
 #
266 266
 cdef void record_piece(int piece, int minimum, char first_empty,
267  
-      unsigned long long piece_mask):
268  
-   pieces[piece][minimum][piece_counts[piece][minimum]] = piece_mask
269  
-   next_cell[piece][minimum][piece_counts[piece][minimum]] = first_empty
270  
-   piece_counts[piece][minimum] += 1
  267
+        unsigned long long piece_mask):
  268
+    pieces[piece][minimum][piece_counts[piece][minimum]] = piece_mask
  269
+    next_cell[piece][minimum][piece_counts[piece][minimum]] = first_empty
  270
+    piece_counts[piece][minimum] += 1
271 271
 
272 272
 
273 273
 
@@ -275,21 +275,21 @@ cdef void record_piece(int piece, int minimum, char first_empty,
275 275
 # they will be left alone.
276 276
 #
277 277
 cdef void fill_contiguous_space(char *board, int index):
278  
-   if board[index] == 1:
279  
-      return
280  
-   board[index] = 1
281  
-   if not out_of_bounds(index, E):
282  
-      fill_contiguous_space(board, shift(index, E))
283  
-   if not out_of_bounds(index, SE):
284  
-      fill_contiguous_space(board, shift(index, SE))
285  
-   if not out_of_bounds(index, SW):
286  
-      fill_contiguous_space(board, shift(index, SW))
287  
-   if not out_of_bounds(index, W):
288  
-      fill_contiguous_space(board, shift(index, W))
289  
-   if not out_of_bounds(index, NW):
290  
-      fill_contiguous_space(board, shift(index, NW))
291  
-   if not out_of_bounds(index, NE):
292  
-      fill_contiguous_space(board, shift(index, NE))
  278
+    if board[index] == 1:
  279
+        return
  280
+    board[index] = 1
  281
+    if not out_of_bounds(index, E):
  282
+        fill_contiguous_space(board, shift(index, E))
  283
+    if not out_of_bounds(index, SE):
  284
+        fill_contiguous_space(board, shift(index, SE))
  285
+    if not out_of_bounds(index, SW):
  286
+        fill_contiguous_space(board, shift(index, SW))
  287
+    if not out_of_bounds(index, W):
  288
+        fill_contiguous_space(board, shift(index, W))
  289
+    if not out_of_bounds(index, NW):
  290
+        fill_contiguous_space(board, shift(index, NW))
  291
+    if not out_of_bounds(index, NE):
  292
+        fill_contiguous_space(board, shift(index, NE))
293 293
 
294 294
 
295 295
 # To thin the number of pieces, I calculate if any of them trap any empty
@@ -299,24 +299,24 @@ cdef void fill_contiguous_space(char *board, int index):
299 299
 # can split the board in half where both halves are viable.
300 300
 #
301 301
 cdef bint has_island(char *cell, int piece):
302  
-   cdef char temp_board[50]
303  
-   cdef char c
304  
-   cdef int i
305  
-   # TODO: memset
306  
-   for i in range(50):
307  
-      temp_board[i] = 0
308  
-   for i in range(5):
309  
-      temp_board[<int>cell[i]] = 1
310  
-   i = 49
311  
-   while temp_board[i] == 1:
312  
-      i -= 1
313  
-   fill_contiguous_space(temp_board, i)
314  
-   c = 0
315  
-   for i in range(50):
316  
-      if temp_board[i] == 0:
317  
-         c += 1
318  
-   return not (c == 0 or (c == 5 and piece == 8) or (c == 40 and piece == 8) or
319  
-         (c % 5 == 0 and piece == 0))
  302
+    cdef char temp_board[50]
  303
+    cdef char c
  304
+    cdef int i
  305
+    # TODO: memset
  306
+    for i in range(50):
  307
+        temp_board[i] = 0
  308
+    for i in range(5):
  309
+        temp_board[<int>cell[i]] = 1
  310
+    i = 49
  311
+    while temp_board[i] == 1:
  312
+        i -= 1
  313
+    fill_contiguous_space(temp_board, i)
  314
+    c = 0
  315
+    for i in range(50):
  316
+        if temp_board[i] == 0:
  317
+            c += 1
  318
+    return not (c == 0 or (c == 5 and piece == 8) or (c == 40 and piece == 8) or
  319
+            (c % 5 == 0 and piece == 0))
320 320
 
321 321
 
322 322
 # Calculate all six rotations of the specified piece at the specified index.
@@ -327,29 +327,29 @@ cdef bint has_island(char *cell, int piece):
327 327
 # me the best time )
328 328
 #
329 329
 cdef void calc_six_rotations(char piece, char index):
330  
-   cdef char rotation, cell[5]
331  
-   cdef char minimum, first_empty
332  
-   cdef unsigned long long piece_mask
333  
-
334  
-   for rotation in range(5):
335  
-      if piece != 3 or rotation < 3:
336  
-         calc_cell_indices(cell, piece, index)
337  
-         if cells_fit_on_board(cell, piece) and not has_island(cell, piece):
338  
-            minimum = minimum_of_cells(cell)
339  
-            first_empty = first_empty_cell(cell, minimum)
340  
-            piece_mask = bitmask_from_cells(cell)
341  
-            record_piece(piece, minimum, first_empty, piece_mask)
342  
-      rotate_piece(piece)
  330
+    cdef char rotation, cell[5]
  331
+    cdef char minimum, first_empty
  332
+    cdef unsigned long long piece_mask
  333
+
  334
+    for rotation in range(5):
  335
+        if piece != 3 or rotation < 3:
  336
+            calc_cell_indices(cell, piece, index)
  337
+            if cells_fit_on_board(cell, piece) and not has_island(cell, piece):
  338
+                minimum = minimum_of_cells(cell)
  339
+                first_empty = first_empty_cell(cell, minimum)
  340
+                piece_mask = bitmask_from_cells(cell)
  341
+                record_piece(piece, minimum, first_empty, piece_mask)
  342
+        rotate_piece(piece)
343 343
 
344 344
 # Calculate every legal rotation for each piece at each board location.
345 345
 cdef void calc_pieces():
346  
-   cdef char piece, index
  346
+    cdef char piece, index
347 347
 
348  
-   for piece in range(10):
349  
-      for index in range(50):
350  
-         calc_six_rotations(piece, index)
351  
-         flip_piece(piece)
352  
-         calc_six_rotations(piece, index)
  348
+    for piece in range(10):
  349
+        for index in range(50):
  350
+            calc_six_rotations(piece, index)
  351
+            flip_piece(piece)
  352
+            calc_six_rotations(piece, index)
353 353
 
354 354
 
355 355
 # Calculate all 32 possible states for a 5-bit row and all rows that will
@@ -363,7 +363,7 @@ cdef char all_rows[32]
363 363
 
364 364
 # We need a better way to do this...
365 365
 for i,v in enumerate([0, 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]):
366  
-    all_rows[i] = v
  366
+     all_rows[i] = v
367 367
 
368 368
 cdef int bad_even_rows[32][32]
369 369
 cdef int bad_odd_rows[32][32]
@@ -371,97 +371,97 @@ cdef int bad_even_triple[32768]
371 371
 cdef int bad_odd_triple[32768]
372 372
 
373 373
 cdef int rows_bad(char row1, char row2, bint even):
374  
-   # even is referring to row1
375  
-   cdef int i, in_zeroes, group_okay
376  
-   cdef char block, row2_shift
377  
-   # Test for blockages at same index and shifted index
378  
-   if even:
379  
-      row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
380  
-   else:
381  
-      row2_shift = (row2 >> 1) | 0x10
382  
-   block = ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift)
383  
-   # Test for groups of 0's
384  
-   in_zeroes = False
385  
-   group_okay = False
386  
-   for i in range(5):
387  
-      if row1 & (1 << i):
388  
-         if in_zeroes:
389  
-            if not group_okay:
390  
-               return True
391  
-            in_zeroes = False
392  
-            group_okay = False
393  
-      else:
394  
-         if not in_zeroes:
395  
-            in_zeroes = True
396  
-         if not (block & (1 << i)):
397  
-            group_okay = True
398  
-   if in_zeroes:
399  
-      return not group_okay
400  
-   else:
401  
-      return False
  374
+    # even is referring to row1
  375
+    cdef int i, in_zeroes, group_okay
  376
+    cdef char block, row2_shift
  377
+    # Test for blockages at same index and shifted index
  378
+    if even:
  379
+        row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
  380
+    else:
  381
+        row2_shift = (row2 >> 1) | 0x10
  382
+    block = ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift)
  383
+    # Test for groups of 0's
  384
+    in_zeroes = False
  385
+    group_okay = False
  386
+    for i in range(5):
  387
+        if row1 & (1 << i):
  388
+            if in_zeroes:
  389
+                if not group_okay:
  390
+                    return True
  391
+                in_zeroes = False
  392
+                group_okay = False
  393
+        else:
  394
+            if not in_zeroes:
  395
+                in_zeroes = True
  396
+            if not (block & (1 << i)):
  397
+                group_okay = True
  398
+    if in_zeroes:
  399
+        return not group_okay
  400
+    else:
  401
+        return False
402 402
 
403 403
 # Check for cases where three rows checked sequentially cause a False
404 404
 # positive.  One scenario is when 5 cells may be surrounded where piece 5
405 405
 # or 7 can fit.  The other scenario is when piece 2 creates a hook shape.
406 406
 #
407 407
 cdef int triple_is_okay(char row1, char row2, char row3, int even):
408  
-   if even:
409  
-      # There are four cases:
410  
-      #  row1: 00011  00001  11001  10101
411  
-      #  row2: 01011  00101  10001  10001
412  
-      #  row3: 011??  00110  ?????  ?????
413  
-      
414  
-      return (((row1 == 0x03) and (row2 == 0x0B) and ((row3 & 0x1C) == 0x0C)) or
415  
-            ((row1 == 0x01) and (row2 == 0x05) and (row3 == 0x06)) or
416  
-            ((row1 == 0x19) and (row2 == 0x11)) or
417  
-            ((row1 == 0x15) and (row2 == 0x11)))
418  
-   else:
419  
-      # There are two cases:
420  
-      #  row1: 10011  10101
421  
-      #  row2: 10001  10001
422  
-      #  row3: ?????  ?????
423  
-      
424  
-      return ((row1 == 0x13) and (row2 == 0x11)) or ((row1 == 0x15) and (row2 == 0x11))
  408
+    if even:
  409
+        # There are four cases:
  410
+        #  row1: 00011  00001  11001  10101
  411
+        #  row2: 01011  00101  10001  10001
  412
+        #  row3: 011??  00110  ?????  ?????
  413
+        
  414
+        return (((row1 == 0x03) and (row2 == 0x0B) and ((row3 & 0x1C) == 0x0C)) or
  415
+                ((row1 == 0x01) and (row2 == 0x05) and (row3 == 0x06)) or
  416
+                ((row1 == 0x19) and (row2 == 0x11)) or
  417
+                ((row1 == 0x15) and (row2 == 0x11)))
  418
+    else:
  419
+        # There are two cases:
  420
+        #  row1: 10011  10101
  421
+        #  row2: 10001  10001
  422
+        #  row3: ?????  ?????
  423
+        
  424
+        return ((row1 == 0x13) and (row2 == 0x11)) or ((row1 == 0x15) and (row2 == 0x11))
425 425
 
426 426
 
427 427
 cdef void calc_rows():
428  
-   cdef int row1, row2, row3
429  
-   cdef int result1, result2
430  
-   for row1 in range(32):
431  
-      for row2 in range(32):
432  
-         bad_even_rows[row1][row2] = rows_bad(row1, row2, True)
433  
-         bad_odd_rows[row1][row2] = rows_bad(row1, row2, False)
434  
-   for row1 in range(32):
435  
-      for row2 in range(32):
436  
-         for row3 in range(32):
437  
-            result1 = bad_even_rows[row1][row2]
438  
-            result2 = bad_odd_rows[row2][row3]
439  
-            if (result1 == False and result2 == True
440  
-                  and triple_is_okay(row1, row2, row3, True)):
441  
-               bad_even_triple[row1+(row2*32)+(row3*1024)] = False
442  
-            else:
443  
-               bad_even_triple[row1+(row2*32)+(row3*1024)] = result1 or result2
444  
-
445  
-            result1 = bad_odd_rows[row1][row2]
446  
-            result2 = bad_even_rows[row2][row3]
447  
-            if (result1 == False and result2 == True
448  
-                  and triple_is_okay(row1, row2, row3, False)):
449  
-               bad_odd_triple[row1+(row2*32)+(row3*1024)] = False
450  
-            else:
451  
-               bad_odd_triple[row1+(row2*32)+(row3*1024)] = result1 or result2
  428
+    cdef int row1, row2, row3
  429
+    cdef int result1, result2
  430
+    for row1 in range(32):
  431
+        for row2 in range(32):
  432
+            bad_even_rows[row1][row2] = rows_bad(row1, row2, True)
  433
+            bad_odd_rows[row1][row2] = rows_bad(row1, row2, False)
  434
+    for row1 in range(32):
  435
+        for row2 in range(32):
  436
+            for row3 in range(32):
  437
+                result1 = bad_even_rows[row1][row2]
  438
+                result2 = bad_odd_rows[row2][row3]
  439
+                if (result1 == False and result2 == True
  440
+                        and triple_is_okay(row1, row2, row3, True)):
  441
+                    bad_even_triple[row1+(row2*32)+(row3*1024)] = False
  442
+                else:
  443
+                    bad_even_triple[row1+(row2*32)+(row3*1024)] = result1 or result2
  444
+
  445
+                result1 = bad_odd_rows[row1][row2]
  446
+                result2 = bad_even_rows[row2][row3]
  447
+                if (result1 == False and result2 == True
  448
+                        and triple_is_okay(row1, row2, row3, False)):
  449
+                    bad_odd_triple[row1+(row2*32)+(row3*1024)] = False
  450
+                else:
  451
+                    bad_odd_triple[row1+(row2*32)+(row3*1024)] = result1 or result2
452 452
 
453 453
 
454 454
 # Calculate islands while solving the board.
455 455
 #
456 456
 cdef bint boardHasIslands(char cell):
457  
-   # Too low on board, don't bother checking
458  
-   if cell >= 40:
459  
-      return False
460  
-   cdef int current_triple = (board >> ((cell / 5) * 5)) & TRIPLE_MASK
461  
-   if (cell / 5) % 2:
462  
-      return bad_odd_triple[current_triple]
463  
-   else:
464  
-      return bad_even_triple[current_triple]
  457
+    # Too low on board, don't bother checking
  458
+    if cell >= 40:
  459
+        return False
  460
+    cdef int current_triple = (board >> ((cell / 5) * 5)) & TRIPLE_MASK
  461
+    if (cell / 5) % 2:
  462
+        return bad_odd_triple[current_triple]
  463
+    else:
  464
+        return bad_even_triple[current_triple]
465 465
 
466 466
 
467 467
 # The recursive solve algorithm.  Try to place each permutation in the upper-
@@ -478,85 +478,85 @@ cdef int solution_count = 0
478 478
 cdef int max_solutions = 2100
479 479
 
480 480
 cdef void record_solution():
481  
-   global solution_count
482  
-   cdef int sol_no, index
483  
-   cdef unsigned long long sol_mask
484  
-   for sol_no in range(10):
485  
-      sol_mask = sol_masks[sol_no]
486  
-      for index in range(50):
487  
-         if sol_mask & 1ULL:
488  
-            solutions[solution_count][index] = sol_nums[sol_no]
489  
-            # Board rotated 180 degrees is a solution too!
490  
-            solutions[solution_count+1][49-index] = sol_nums[sol_no]
491  
-         sol_mask = sol_mask >> 1
492  
-   solution_count += 2
  481
+    global solution_count
  482
+    cdef int sol_no, index
  483
+    cdef unsigned long long sol_mask
  484
+    for sol_no in range(10):
  485
+        sol_mask = sol_masks[sol_no]
  486
+        for index in range(50):
  487
+            if sol_mask & 1ULL:
  488
+                solutions[solution_count][index] = sol_nums[sol_no]
  489
+                # Board rotated 180 degrees is a solution too!
  490
+                solutions[solution_count+1][49-index] = sol_nums[sol_no]
  491
+            sol_mask = sol_mask >> 1
  492
+    solution_count += 2
493 493
 
494 494
 cdef void solve(int depth, int cell):
495  
-   global board, avail
496  
-   cdef int piece, rotation, max_rots
497  
-   cdef unsigned long long *piece_mask
498  
-   cdef short piece_no_mask
499  
-
500  
-   if solution_count >= max_solutions:
501  
-      return
502  
-
503  
-   while board & (1ULL << cell):
504  
-      cell += 1
505  
-
506  
-   for piece in range(10):
507  
-      piece_no_mask = 1 << piece
508  
-      if not (avail & piece_no_mask):
509  
-         continue
510  
-      avail ^= piece_no_mask
511  
-      max_rots = piece_counts[piece][cell]
512  
-      piece_mask = pieces[piece][cell]
513  
-      for rotation in range(max_rots):
514  
-         if not (board & (piece_mask + rotation)[0]):
515  
-            sol_nums[depth] = piece
516  
-            sol_masks[depth] = (piece_mask + rotation)[0]
517  
-            if depth == 9:
518  
-               # Solution found!!!!!11!!ONE!
519  
-               record_solution()
520  
-               avail ^= piece_no_mask
521  
-               return
522  
-            board |= (piece_mask + rotation)[0]
523  
-            if not boardHasIslands(next_cell[piece][cell][rotation]):
524  
-               solve(depth + 1, next_cell[piece][cell][rotation])
525  
-            board ^= (piece_mask + rotation)[0]
526  
-      avail ^= piece_no_mask
  495
+    global board, avail
  496
+    cdef int piece, rotation, max_rots
  497
+    cdef unsigned long long *piece_mask
  498
+    cdef short piece_no_mask
  499
+
  500
+    if solution_count >= max_solutions:
  501
+        return
  502
+
  503
+    while board & (1ULL << cell):
  504
+        cell += 1
  505
+
  506
+    for piece in range(10):
  507
+        piece_no_mask = 1 << piece
  508
+        if not (avail & piece_no_mask):
  509
+            continue
  510
+        avail ^= piece_no_mask
  511
+        max_rots = piece_counts[piece][cell]
  512
+        piece_mask = pieces[piece][cell]
  513
+        for rotation in range(max_rots):
  514
+            if not (board & (piece_mask + rotation)[0]):
  515
+                sol_nums[depth] = piece
  516
+                sol_masks[depth] = (piece_mask + rotation)[0]
  517
+                if depth == 9:
  518
+                    # Solution found!!!!!11!!ONE!
  519
+                    record_solution()
  520
+                    avail ^= piece_no_mask
  521
+                    return
  522
+                board |= (piece_mask + rotation)[0]
  523
+                if not boardHasIslands(next_cell[piece][cell][rotation]):
  524
+                    solve(depth + 1, next_cell[piece][cell][rotation])
  525
+                board ^= (piece_mask + rotation)[0]
  526
+        avail ^= piece_no_mask
527 527
 
528 528
 
529 529
 # qsort comparator - used to find first and last solutions
530 530
 cdef int solution_sort(void *elem1, void *elem2):
531  
-   cdef signed char *char1 = <signed char *> elem1
532  
-   cdef signed char *char2 = <signed char *> elem2
533  
-   cdef int i = 0
534  
-   while i < 50 and char1[i] == char2[i]:
535  
-      i += 1
536  
-   return char1[i] - char2[i]
  531
+    cdef signed char *char1 = <signed char *> elem1
  532
+    cdef signed char *char2 = <signed char *> elem2
  533
+    cdef int i = 0
  534
+    while i < 50 and char1[i] == char2[i]:
  535
+        i += 1
  536
+    return char1[i] - char2[i]
537 537
 
538 538
 
539 539
 def my_printf(*args):
540  
-    pass
  540
+     pass
541 541
 
542 542
 # pretty print a board in the specified hexagonal format
543 543
 cdef void pretty(signed char *b):
544  
-   cdef int i
545  
-   for i in range(0, 50, 10):
546  
-        pass
  544
+    cdef int i
  545
+    for i in range(0, 50, 10):
  546
+          pass
547 547
 #      my_printf("%c %c %c %c %c \n %c %c %c %c %c \n", b[i]+'0', b[i+1]+'0',
548 548
 #            b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
549 549
 #            b[i+7]+'0', b[i+8]+'0', b[i+9]+'0')
550  
-   printf("\n")
  550
+    printf("\n")
551 551
 
552 552
 if __name__ == '__main__':
553  
-   from sys import argv
554  
-   if len(argv) > 1:
555  
-      max_solutions = int(argv[1])
556  
-   calc_pieces()
557  
-   calc_rows()
558  
-   solve(0, 0)
559  
-   printf("%d solutions found\n\n", solution_count)
560  
-   qsort(solutions, solution_count, 50 * sizeof(signed char), solution_sort)
561  
-   pretty(solutions[0])
562  
-   pretty(solutions[solution_count-1])
  553
+    from sys import argv
  554
+    if len(argv) > 1:
  555
+        max_solutions = int(argv[1])
  556
+    calc_pieces()
  557
+    calc_rows()
  558
+    solve(0, 0)
  559
+    printf("%d solutions found\n\n", solution_count)
  560
+    qsort(solutions, solution_count, 50 * sizeof(signed char), solution_sort)
  561
+    pretty(solutions[0])
  562
+    pretty(solutions[solution_count-1])

0 notes on commit c8badea

Please sign in to comment.
Something went wrong with that request. Please try again.