/
towers_of_hanoi.py
271 lines (232 loc) · 11.3 KB
/
towers_of_hanoi.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
from pprint import pprint
from time import time
import SelectScript
import SelectScript.Interpreter
def move(step, towers):
if not towers or not towers[step[0]]:
pass
elif not towers[step[1]] or towers[step[1]][-1] > towers[step[0]][-1]:
towers[step[1]].append( towers[step[0]].pop() )
return towers
return []
ss = SelectScript.Interpreter()
ss.addFunction('move', move)
ss.addFunction('str', str)
####################################################################################
####################################################################################
problem1 = """# Basic nested functions iterating over all combinations, not very effective
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT mov1.this, mov2.this, mov3.this, mov4.this, mov5.this, mov6.this, mov7.this
FROM mov1=moves, mov2=moves, mov3=moves, mov4=moves, mov5=moves, mov6=moves, mov7=moves
WHERE [[],[],["c","b","a"]] == move( mov7.this,
move( mov6.this,
move( mov5.this,
move( mov4.this,
move( mov3.this,
move( mov2.this,
move( mov1.this, [["c","b","a"],[],[]] )))))))
AS list; """
bytecode = SelectScript.compile(problem1)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem1
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 10.0932531357
# result: [[0, 2], [0, 1], [2, 1], [0, 2], [1, 0], [1, 2], [0, 2]]
####################################################################################
####################################################################################
problem2 = """# Basic recursive query
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT mov.this
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]], level=1 # the local level variable
CONNECT BY tower = move(mov.this, tower), level=level+1 # is required to prevent
STOP WITH level==7 or []==move(mov.this, tower) # an infinite recursion
AS list; """
bytecode = SelectScript.compile(problem2)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem2
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.885102987289
# result: [[[0, 2], [0, 1], [2, 1], [0, 2], [1, 0], [1, 2], [0, 2]]]
####################################################################################
####################################################################################
problem3 = """# Recursive query with no cycles allowed ... reduces the search space
# because one moves can be applied a couple of times, the tower is included as
# an additional result parameter
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT mov.this, tower
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]], level=1
CONNECT BY NO CYCLE
tower = move( mov.this, tower ), level=level+1
STOP WITH level==7 or [] == move( mov.this, tower )
AS dict; """
bytecode = SelectScript.compile(problem3)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem3
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.324506998062
# result: [[{'tower': [[3, 2], [], [1]], 'mov': [0, 2]},
# {'tower': [[3], [2], [1]], 'mov': [0, 1]},
# {'tower': [[3], [2, 1], []], 'mov': [2, 1]},
# {'tower': [[], [2, 1], [3]], 'mov': [0, 2]},
# {'tower': [[1], [2], [3]], 'mov': [1, 0]},
# {'tower': [[1], [], [3, 2]], 'mov': [1, 2]},
# {'tower': [[], [], [3, 2, 1]], 'mov': [0, 2]}]]
####################################################################################
####################################################################################
problem4 = """# a result can only appear once in a search, note the changed SELECT-statement
# the "to" function is used to format/name the titles in the dictionary
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT to([mov.this, tower], "config"+str(level))
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]], level=1
CONNECT BY UNIQUE
tower = move( mov.this, tower ), level=level+1
STOP WITH level==7 or [] == move( mov.this, tower )
AS dict; """
bytecode = SelectScript.compile(problem4)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem4
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.194886922836
# result: [[{'config1': [[0, 2], [[3, 2], [], [1]]]},
# {'config2': [[0, 1], [[3], [2], [1]]]},
# {'config3': [[2, 1], [[3], [2, 1], []]]},
# {'config4': [[0, 2], [[], [2, 1], [3]]]},
# {'config5': [[1, 0], [[1], [2], [3]]]},
# {'config6': [[1, 2], [[1], [], [3, 2]]]},
# {'config7': [[0, 2], [[], [], [3, 2, 1]]]}]]
####################################################################################
####################################################################################
problem5 = """# both options can also be applied in combination
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT to([mov.this, tower], "config"+str(level))
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]], level=1
CONNECT BY NO CYCLE UNIQUE
tower = move( mov.this, tower ), level=level+1
STOP WITH level==7 or [] == move( mov.this, tower )
AS dict; """
bytecode = SelectScript.compile(problem5)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem5
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.201504945755
# result: [[{'config1': [[0, 2], [[3, 2], [], [1]]]},
# {'config2': [[0, 1], [[3], [2], [1]]]},
# {'config3': [[2, 1], [[3], [2, 1], []]]},
# {'config4': [[0, 2], [[], [2, 1], [3]]]},
# {'config5': [[1, 0], [[1], [2], [3]]]},
# {'config6': [[1, 2], [[1], [], [3, 2]]]},
# {'config7': [[0, 2], [[], [], [3, 2, 1]]]}]]
####################################################################################
####################################################################################
problem6 = """# The memorize option generates a graph structure in the background
# with a certain depth, which can speed-up a query ...
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT mov.this, tower
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]]
CONNECT BY MEMORIZE
tower = move( mov.this, tower )
STOP WITH [] == move( mov.this, tower )
AS dict; """
bytecode= SelectScript.compile(problem6)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem6
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.0428931713104
# result: [[[{'tower': [[3, 2], [], [1]], 'mov': [0, 2]}],
# [{'tower': [[3], [2], [1]], 'mov': [0, 1]}],
# [{'tower': [[3], [2, 1], []], 'mov': [2, 1]}],
# [{'tower': [[], [2, 1], [3]], 'mov': [0, 2]}],
# [{'tower': [[1], [2], [3]], 'mov': [1, 0]}],
# [{'tower': [[1], [], [3, 2]], 'mov': [1, 2]}],
# [{'tower': [[], [], [3, 2, 1]], 'mov': [0, 2]}]]]
####################################################################################
####################################################################################
problem7 = """# stop at a certain number of results... note the changed graph-size
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT mov.this, tower
FROM mov=moves
WHERE [[],[],[3,2,1]] == move( mov.this, tower )
START WITH tower = [[3,2,1],[],[]]
CONNECT BY MEMORIZE 10 MAXIMUM 3
tower = move( mov.this, tower )
STOP WITH [] == move( mov.this, tower )
AS dict; """
bytecode= SelectScript.compile(problem7)
t_start = time()
result = ss.eval(bytecode)
t_end = time()
print "----------------------------------------------------------------------------"
print problem7
print "----------------------------------------------------------------------------"
print "time:", t_end - t_start
print "result:", result
# time: 0.075532913208
# result: [[[{'tower': [[3, 2], [1], []], 'mov': [0, 1]}],
# [{'tower': [[3], [1], [2]], 'mov': [0, 2]}],
# [{'tower': [[3], [], [2, 1]], 'mov': [1, 2]}],
# [{'tower': [[], [3], [2, 1]], 'mov': [0, 1]}],
# [{'tower': [[], [3, 1], [2]], 'mov': [2, 1]}],
# [{'tower': [[2], [3, 1], []], 'mov': [2, 0]}],
# [{'tower': [[2, 1], [3], []], 'mov': [1, 0]}],
# [{'tower': [[2, 1], [], [3]], 'mov': [1, 2]}],
# [{'tower': [[2], [1], [3]], 'mov': [0, 1]}],
# [{'tower': [[], [1], [3, 2]], 'mov': [0, 2]}],
# [{'tower': [[], [], [3, 2, 1]], 'mov': [1, 2]}]],
#
# [[{'tower': [[3, 2], [], [1]], 'mov': [0, 2]}],
# [{'tower': [[3], [2], [1]], 'mov': [0, 1]}],
# [{'tower': [[3], [2, 1], []], 'mov': [2, 1]}],
# [{'tower': [[], [2, 1], [3]], 'mov': [0, 2]}],
# [{'tower': [[1], [2], [3]], 'mov': [1, 0]}],
# [{'tower': [[1], [], [3, 2]], 'mov': [1, 2]}],
# [{'tower': [[], [], [3, 2, 1]], 'mov': [0, 2]}]],
#
# [[{'tower': [[3, 2], [], [1]], 'mov': [0, 2]}],
# [{'tower': [[3], [2], [1]], 'mov': [0, 1]}],
# [{'tower': [[3, 1], [2], []], 'mov': [2, 0]}],
# [{'tower': [[3], [2, 1], []], 'mov': [0, 1]}],
# [{'tower': [[3], [2, 1], []], 'mov': [0, 1]}],
# [{'tower': [[], [2, 1], [3]], 'mov': [0, 2]}],
# [{'tower': [[1], [2], [3]], 'mov': [1, 0]}],
# [{'tower': [[1], [], [3, 2]], 'mov': [1, 2]}],
# [{'tower': [[], [], [3, 2, 1]], 'mov': [0, 2]}]]]