1
1
from queue import PriorityQueue
2
2
import math
3
3
import time
4
+ import sys
4
5
5
6
6
7
def widthsToTickMarks (widths ):
@@ -23,31 +24,26 @@ def widthsToMeasurableDistances(widths):
23
24
return measurableDistances
24
25
25
26
26
- def getChildren (widths , maxChildren ):
27
+ def getChildren (widths , maxChildren , mds ):
27
28
children = []
28
29
29
- mds = widthsToMeasurableDistances (widths )
30
-
31
- # can be made more efficient
32
- # might need to make this go higher
33
30
for md in range (1 , maxChildren ):
34
31
if md not in mds :
35
32
children .append (widths + [md ])
36
33
37
34
return children
38
35
39
36
40
- def isValid (widths ):
41
- return max (widthsToMeasurableDistances ( widths ) .values ()) <= 1
37
+ def isValid (mds ):
38
+ return max (mds .values ()) <= 1
42
39
43
40
44
41
def getCost (widths ):
45
42
return sum (widths )
46
43
47
44
48
- def getHeuristic (widths , order ):
45
+ def getHeuristic (widths , order , mds ):
49
46
unusedWidths = []
50
- mds = widthsToMeasurableDistances (widths )
51
47
md = 1
52
48
while len (unusedWidths ) + len (widths ) < order - 1 :
53
49
if md not in mds :
@@ -61,25 +57,26 @@ def search(order):
61
57
queue = PriorityQueue ()
62
58
maxChildren = math .floor (order * 1.5 )
63
59
while True :
64
- queue .put ((0 , []))
60
+ queue .put ((0 , [], {} ))
65
61
while not queue .empty ():
66
- f , widths = queue .get ()
62
+ f , widths , mds = queue .get ()
67
63
if len (widths ) == order - 1 :
68
64
return widths
69
65
70
- children = getChildren (widths , maxChildren )
66
+ children = getChildren (widths , maxChildren , mds )
71
67
for child in children :
72
- if not isValid (child ):
68
+ mds = widthsToMeasurableDistances (child )
69
+ if not isValid (mds ):
73
70
continue
74
71
g = getCost (child )
75
- h = getHeuristic (child , order )
76
- queue .put ((g + h , child ))
72
+ h = getHeuristic (child , order , mds )
73
+ queue .put ((g + h , child , mds ))
77
74
print ("Solution not found with maxChildren = " + str (maxChildren ));
78
75
maxChildren += 1
79
76
80
77
81
78
def main ():
82
- order = int (input () )
79
+ order = int (sys . argv [ 1 ] )
83
80
start = time .time ()
84
81
print (widthsToTickMarks (search (order )))
85
82
print ("Time: " + str (time .time () - start ))
0 commit comments