-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
executable file
·70 lines (64 loc) · 1.75 KB
/
input.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
#!/usr/bin/env python
import sys
import getopt
def main(argv):
try:
if argv[1]:
writeKb(argv[1])
except Exception, e:
raise e
def writeKb(inputFile):
islands = []
try:
f = open(inputFile, 'r')
print 'Parsing file: {0}'.format(inputFile)
islandsDimensions = f.readline().split(' ')
nrRows = int(islandsDimensions[0])
nrCols = int(islandsDimensions[1])
for r in range(nrRows):
line = f.readline()
for c in range(nrCols):
try:
islands.append([[r+1,c+1], int(line[c])])
except Exception, e:
islands.append([[r+1,c+1], line[c]])
except Exception, e:
print 'Could not open file: {0}\n{1}'.format(inputFile, e)
return
inputFile = inputFile.split('/')[1].split('.')[0]
dbFile = inputFile + "_db.pl"
try:
f = open(dbFile, 'w')
print 'Writing database file: {0}'.format(dbFile)
prettyPrint = lambda x, comma: f.write('\t{0}{1}\n'.format(x, comma))
f.write('rows({0}).\ncolumns({1}).\n'.format(nrRows, nrCols))
f.write('grid([\n')
i = 1
for island in islands:
if i < len(islands):
prettyPrint(island, ',')
else:
prettyPrint(island, '')
i += 1
f.write(']).')
except Exception, e:
print 'Could not write file: {0}\n{1}'.format(dbFile, e)
finally:
pass
engineFile = inputFile + '_engine.pl'
try:
f = open(engineFile, 'w')
print 'Writing engine file: {0}'.format(engineFile)
f.write(':-consult(\'{0}\').\n'.format(dbFile))
f.write(':-consult(\'hashi.pl\').\n')
f.write(':-grid(Grid),\n')
f.write('islands(Grid,Islands),\n')
f.write('transform(Grid,ListMatrix),\n')
f.write('generate(ListMatrix, Solution),\n'),
f.write('test(Solution,Islands),\n')
f.write('writeSolution(Solution),\n')
f.write('halt.')
except Exception, e:
raise e
if __name__ == "__main__":
main(sys.argv[1:])