-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
107 lines (81 loc) · 2.33 KB
/
utils.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
import re
'''
Alter configs from plain text to coded list
values in config will be
-1 = cube is clear OR
or j = cube over it
AND x = cube under it
or -1 = cube is on table
'''
def create_config(objects, state_in_text):
config = list()
# initialize all cubes with -1
for i in range(len(objects)):
config.append([-1, -1])
for text in state_in_text:
tokens = re.split('[ ]', text)
if tokens[0] == 'ON':
index1, index2 = objects.index(tokens[1]), objects.index(tokens[2])
config[index2][0] = index1
config[index1][1] = index2
return tuple(map(tuple, config))
def parse_file(file):
# read objects till find the line with init
while True:
line = file.readline()
if "objects" in line:
break
objects = re.split("[ \n]", line)
while True:
line = file.readline()
if ":INIT" not in line:
objects.extend(re.split("[ \n)]", line))
else:
break
# trim objects
objects.remove("(:objects")
while '' in objects:
objects.remove('')
while ')' in objects:
objects.remove(')')
# read initial state till find line with goal
init = re.split('[()\n]', line)
while True:
line = file.readline()
if ":goal" not in line:
init.extend(re.split('[()\n]', line))
else:
break
# trim init
while '' in init:
init.remove('')
for text in init:
if text.isspace():
init.remove(text)
init.remove(":INIT ")
init.remove('HANDEMPTY')
# read goal state till find line with EOF
goal = re.split('[()\n]', line)
while True:
line = file.readline()
if not line:
break
else:
goal.extend(re.split('[()\n]', line))
# trim goal
goal.remove(':goal ')
goal.remove('AND ')
while '' in goal:
goal.remove('')
for text in goal:
if text.isspace():
goal.remove(text)
begin_config = create_config(objects, init)
goal_config = create_config(objects, goal)
return objects, begin_config, goal_config
def write_in_file(moves, solution_file, ):
with open(solution_file, "w+") as f:
i = 1
for move in moves:
f.write(str(i) + ". " + move + "\n")
i += 1