-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
139 lines (111 loc) · 3.44 KB
/
solver.py
File metadata and controls
139 lines (111 loc) · 3.44 KB
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
# David Larson
#
# TO RUN:
# $ python3 solver.py puzzle1.txt
#
#
import sys
class Cell:
def __init__(self, x, y, val, posMoves):
self.x = x
self.y = y
self.val = val
self.posMoves = posMoves
def setPosMoves(self, moves):
self.posMoves = moves
class Puzzle:
def __init__(self, grid):
newGrid = []
for y, row in enumerate(grid):
currRow = []
for x, square in enumerate(row):
currRow.append(Cell(x, y, square.val, []))
newGrid.append(currRow)
self.grid = newGrid
def show(self):
for line in self.grid:
for digit in line:
print("|" + str(digit.val), end = '')
print("|")
print()
def isValid(self):
return self.rowsValid() and self.columnsValid() and self.boxesValid()
def rowsValid(self):
for row in self.grid:
if not checkArr([x.val for x in row]):
return False
return True
def columnsValid(self):
for i in range(9):
arr = [x[i] for x in self.grid]
if not checkArr([x.val for x in arr]):
return False
return True
def boxesValid(self):
boxes = [[[], [], []], [[], [], []], [[], [], []]]
for y in range(9):
for x in range(9):
# print("grid[" + str(y) + "][" + str(x) + "] goes in box [" + str(int(y/3)) + "][" + str(int(x/3)) + "]")
boxes[int(y/3)][int(x/3)].append(self.grid[y][x])
for row in boxes:
for arr in row:
if not checkArr([x.val for x in arr]):
return False
return True
def setSquare(self, x, y, val):
self.grid[x][y] = Cell(x, y, val, [])
def isFull(self):
for row in self.grid:
for digit in row:
if digit.val == 0:
return False
return True
def anydup(arr):
seen = set()
for x in arr:
if x != 0 and x in seen: return True
seen.add(x)
return False
def checkArr(arr):
for i in range(1, 10):
return not anydup(arr)
def getFirstBlank(puz):
for y, line in enumerate(puz.grid):
for x, digit in enumerate(line):
if digit.val == 0: return [digit.x, digit.y]
def solve(puz):
full = puz.isFull()
valid = puz.isValid()
if not valid:
return 0
else:
if full:
puz.show()
return 1
blank = getFirstBlank(puz)
newPuz = Puzzle(puz.grid)
for i in range(1, 10):
newPuz.setSquare(blank[1], blank[0], i)
# print(blank)
# newPuz.show()
val = solve(newPuz)
if val == 1: return 1
# no possibilities worked for this cell, so return 0
def main():
try:
file = open(sys.argv[1], 'r').readlines()
except IndexError:
print("Invalid arguments. Command should be in the following format:\n$ python3 solver.py <input.txt>")
quit()
grid = []
for y, line in enumerate(file):
row = [int(x) for x in list(line.rstrip())]
newRow = []
for x, square in enumerate(row):
newRow.append(Cell(x, y, square, []))
grid.append(newRow)
puz = Puzzle(grid)
# puz.show()
# puz.populatePossibleMoves()
solve(puz)
main()