/
gaya.py
executable file
·173 lines (131 loc) · 4.7 KB
/
gaya.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
#!/usr/bin/env python
import pentominoes
import dl
def xyz_to_cell(t, size=5):
x, y, z = t
return z * size * size + y * size + x
def cell_to_xyz(n, size=5):
z = n / (size * size)
r = n % (size * size)
y = r / size
x = r % size
return x, y, z
def translate_to_plane(piece, xplane=None, yplane=None, zplane=None, size=5):
v = [0] * 3
v[0] = 0 if xplane is None else 1
v[1] = 0 if yplane is None else 1
v[2] = 0 if zplane is None else 1
if sum(v) != 1:
raise ValueError("translate_to_plane called stupidly")
result = []
if zplane is not None:
result = [(pt[0], pt[1], zplane) for pt in piece]
if yplane is not None:
result = [(pt[0], yplane, pt[1]) for pt in piece]
if xplane is not None:
result = [(xplane, pt[0], pt[1]) for pt in piece]
return result
def find_outlier(pts):
'''
pts is the coordinates of a y-piece in 3 space. find
the coordinates of the 'knob'.
'''
all_x = {}
all_y = {}
all_z = {}
# find the axis where there are exactly 4 of the same value
for p in pts:
x, y, z = p
if not x in all_x:
all_x[x] = 0
all_x[x] += 1
if not y in all_y:
all_y[y] = 0
all_y[y] += 1
if not z in all_z:
all_z[z] = 0
all_z[z] += 1
# print all_x
# print all_y
# print all_z
c = []
if len(all_x.keys()) == 2:
c = [i for i in all_x.items() if i[1] == 1]
item = c[0]
knob = [k for k in pts if k[0] == item[0]]
elif len(all_y.keys()) == 2:
c = [i for i in all_y.items() if i[1] == 1]
item = c[0]
knob = [k for k in pts if k[1] == item[0]]
elif len(all_z.keys()) == 2:
c = [i for i in all_z.items() if i[1] == 1]
item = c[0]
knob = [k for k in pts if k[2] == item[0]]
else:
raise ValueError('oh shit')
return knob[0]
def make_row(pts, size=5):
cells = [xyz_to_cell(x) for x in pts]
row = ['0'] * (size ** 3)
for c in cells:
row[c] = '1'
return ''.join(row)
def spin(size=5):
'''
generate 3-space coordes for every orientation of a piece
'''
ypiece = pentominoes.make_piece('y')
for o in pentominoes.orientations(ypiece):
for p in pentominoes.piece_placements(o, size, size):
for plane in range(size):
three_d = translate_to_plane(p, zplane=plane)
yield three_d
for plane in range(size):
three_d = translate_to_plane(p, xplane=plane)
yield three_d
for plane in range(size):
three_d = translate_to_plane(p, yplane=plane)
yield three_d
def print_matrix():
size = 5
column_headers = 'x' * (size ** 3)
for point_vector in spin(size):
print make_row(point_vector, size)
def solve():
matrix = dl.matrix_from_file('gaya.txt')
# matrix.add_column_headers([str(x) for x in range(size ** 3)])
#
# all_rows = []
# for point_vector in spin(5):
#
# r = list(make_row(point_vector, size))
# all_rows.append(r)
# matrix.add_row(r)
#
## knob = find_outlier(point_vector)
## cell = xyz_to_cell(knob, size)
## print r
#
#
initial_rows = [100]
dlx = dl.DLXAlgorithm(matrix, seeds=initial_rows)
dlx.dlx1()
scount = 0
for s in dlx.solutions:
print s
scount += 1
print '%d solutions' % scount
if __name__ == '__main__':
solve()
'''
[2, 26, 28, 64, 99, 159, 228, 260, 270, 284, 365, 386, 485, 585, 586, 587, 588, 627, 687, 712, 739, 828, 908, 921, 100]
[26, 28, 37, 86, 99, 129, 148, 159, 168, 232, 257, 260, 317, 365, 386, 527, 598, 626, 687, 828, 866, 895, 909, 934, 100]
[26, 28, 35, 37, 64, 86, 99, 129, 148, 159, 168, 232, 242, 257, 260, 270, 317, 365, 386, 527, 687, 828, 909, 934, 100]
[99, 102, 103, 118, 174, 177, 185, 238, 256, 327, 331, 332, 334, 373, 429, 430, 504, 514, 529, 566, 593, 816, 858, 866, 100]
[26, 28, 35, 37, 86, 99, 129, 148, 159, 168, 232, 242, 257, 260, 317, 365, 386, 527, 626, 687, 828, 895, 909, 934, 100]
[99, 103, 118, 174, 177, 256, 327, 331, 334, 374, 429, 430, 504, 514, 529, 566, 593, 632, 793, 816, 858, 865, 866, 867, 100]
[26, 28, 37, 64, 86, 99, 129, 148, 159, 168, 232, 257, 260, 270, 317, 365, 386, 527, 598, 687, 828, 866, 909, 934, 100]
[2, 26, 28, 99, 159, 228, 260, 284, 365, 386, 485, 585, 586, 587, 588, 626, 627, 687, 712, 739, 828, 895, 908, 921, 100]
[99, 102, 103, 118, 174, 177, 256, 327, 331, 332, 334, 429, 430, 504, 514, 529, 566, 593, 632, 793, 816, 858, 866, 867, 100]
[99, 103, 118, 174, 177, 185, 238, 256, 327, 331, 334, 373, 374, 429, 430, 504, 514, 529, 566, 593, 816, 858, 865, 866, 100]
'''