forked from hilben/sudoku-dimacs-creator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshowResult.py
executable file
·75 lines (59 loc) · 1.72 KB
/
showResult.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
#!/usr/bin/python
import sys
def pad(n, d):
count = d
s = n
while(count > 0):
s = '0' + s
count -= 1
return s
def main():
# Get value of n from argument
n = int(sys.argv[1])
n2 = n * n
size = len(str(n2)) # digits in n^2
# read result of MiniSAT
fname = sys.argv[2]
file = open(str(fname), "r")
data = file.read()
file.close()
# Skip 'SAT\n' and read the rest of the file
# Fails when 'UNSAT\n' is the content
if data == 'UNSAT\n':
print("No solution exists for the randomly generated initial state.")
sys.exit()
elif data == 'INDET\n':
print("Could not determine if a solution exists for the randomly generated initial state.")
sys.exit()
else:
print("Showing the solution:")
data = data[4:len(data)].split(' ')
# Extract only the valid values
cleaned = []
for s in data:
if s[0] != '-' and s!="0\n" and (len(s) == 3*size or len(s) == 2*size + 1):
#pad with 0 at the beginning to correct size
v = s
diff = 3*size - len(v)
if(diff > 0):
v = pad(v, diff)
cleaned.append(v)
# Create the n2 X n2 matrix
sudoku = []
for i in range(0, n2):
sudoku.append([])
# Put the valid values into a matrix
for c in cleaned:
unit = len(c)/3
row = int(c[0:unit])
col = int(c[unit:2*unit])
val = int(c[2*unit:])
if val <= n2 and len(sudoku[row-1]) < n2:
sudoku[row-1].insert(col-1, val)
# Show the matrix
for row in sudoku:
for val in row:
print(str(val) + "\t"),
print("\n"),
if __name__ == '__main__':
main()