-
Notifications
You must be signed in to change notification settings - Fork 0
/
kamertje_verhuren.py
251 lines (208 loc) · 8.04 KB
/
kamertje_verhuren.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
CORNER = '\u254B'
VERT_WALL = '\u2503'
HOR_WALL = '\u2501'
NO_WALL = '\u00B7'
def parseCoordLine(line):
return map(int, line.split(' '))
def parsePuzzleLine(line, length):
for i, char in enumerate(line):
try:
yield int(char)
except ValueError:
yield None
for j in range(i, length-1):
yield None
def wallChar(wallDatum, char):
if wallDatum is None:
return ' '
elif wallDatum:
return char
else:
return NO_WALL
def cellChar(cellDatum):
if cellDatum is None:
return ' '
else:
return str(cellDatum)
def topRow(data):
for item in data:
yield CORNER
yield wallChar(item, HOR_WALL)
yield CORNER
def cellRow(leftWallData, cellData):
for i, cell in enumerate(cellData):
yield wallChar(leftWallData[i], VERT_WALL)
yield cellChar(cell)
yield wallChar(leftWallData[i+1], VERT_WALL)
class Wall:
'''
Access helper for the wall table.
A wall is either True, False or None (unknown)
'''
def __init__(self, puzzle, row, col, leftWall):
self.puzzle = puzzle
self.row = row
self.col = col
self.leftWall = leftWall
def getValue(self):
if (self.leftWall):
return self.puzzle.leftWalls[self.row][self.col]
else:
return self.puzzle.topWalls[self.row][self.col]
def setValue(self, newValue):
if (self.leftWall):
self.puzzle.leftWalls[self.row][self.col] = newValue
else:
self.puzzle.topWalls[self.row][self.col] = newValue
class SomethingWithWalls:
def __init__(self, walls):
self.walls = walls
def undecidedWalls(self):
return [wall for wall in self.walls if wall.getValue() is None]
def realWalls(self):
return [wall for wall in self.walls if wall.getValue() is True]
def notWalls(self):
return [wall for wall in self.walls if wall.getValue() is False]
class Corner(SomethingWithWalls):
'''The corner is referenced as the corner in the top-left corner of the corresponding cell'''
def __init__(self, puzzle, row, col):
walls = []
if row > 0:
# above
walls.append(Wall(puzzle, row=row-1, col=col, leftWall=True))
if row < puzzle.height:
# below
walls.append(Wall(puzzle, row=row, col=col, leftWall=True))
if col > 0:
# left
walls.append(Wall(puzzle, row=row, col=col-1, leftWall=False))
if col < puzzle.width:
# right
walls.append(Wall(puzzle, row=row, col=col, leftWall=False))
super().__init__( walls)
def isFulfilled(self):
return len(self.undecidedWalls()) == 0
class Cell(SomethingWithWalls):
def __init__(self, puzzle, row, col):
self.puzzle = puzzle
self.row = row
self.col = col
walls = [
#Top
Wall(self.puzzle, row=self.row, col=self.col, leftWall=False),
#Right
Wall(self.puzzle, row=self.row, col=self.col+1, leftWall=True),
#Bottom
Wall(self.puzzle, row=self.row+1, col=self.col, leftWall=False),
#Left
Wall(self.puzzle, row=self.row, col=self.col, leftWall=True)
]
super().__init__( walls)
def getValue(self):
return self.puzzle.rows[self.row][self.col]
def fulfilled(self):
if self.getValue() is None:
return True
else:
return len(self.undecidedWalls()) == 0
def __str__(self):
return '({},{}): {}'.format(self.row, self.col, [w.getValue() for w in self.walls])
class KamertjeVerhuren:
@staticmethod
def parse(stream):
rows = [] # array of arrays of letters. Space is empty
topWalls = None
leftWalls = None
for line in stream:
if not line.startswith('#'):
line = line.strip('\n')
if line in ['size', 'puzzle', 'top', 'left']:
section = line
elif section == 'size':
width, height = parseCoordLine(line)
elif section == 'puzzle':
rows.append(list(parsePuzzleLine(line, width)))
elif section == 'top':
if topWalls is None:
topWalls = [[None for _ in range(width)] for _ in range(height + 1)]
row, col = parseCoordLine(line)
topWalls[row][col] = True
elif section == 'left':
if leftWalls is None:
leftWalls = [[None for _ in range(width + 1)] for _ in range(height)]
row, col = parseCoordLine(line)
leftWalls[row][col] = True
return KamertjeVerhuren(width, height, rows, topWalls, leftWalls)
def __init__(self, width, height, rows, topWalls, leftWalls):
self.width = width
self.height = height
self.rows = rows
self.topWalls = topWalls
self.leftWalls = leftWalls
self.allCells = [Cell(self, row, col) for row in range(self.height) for col in range(self.width)]
self.allCorners = [Corner(self, row, col) for row in range(self.height+1) for col in range(self.width+1)]
self.cleanUp()
def cellUnknownsAllWalls():
changes = 0
for cell in self.allCells:
if cell.getValue() is not None and cell.getValue() == len(cell.undecidedWalls()) + len(cell.realWalls()):
for wall in cell.undecidedWalls():
wall.setValue(True)
changes += 1
return changes
def cellUnknownsAllEmpties():
changes = 0
for cell in self.allCells:
if cell.getValue() is not None and cell.getValue() == len(cell.realWalls()):
for wall in cell.undecidedWalls():
wall.setValue(False)
changes += 1
return changes
def cornerWallInMeansWallOut():
changes = 0
for corner in self.allCorners:
if len(corner.realWalls()) == 1 and len(corner.undecidedWalls()) == 1:
corner.undecidedWalls()[0].setValue(True)
changes += 1
return changes
def cornerNoWallInMeansNoWallOut():
changes = 0
for corner in self.allCorners:
if len(corner.realWalls()) == 0 and len(corner.undecidedWalls()) == 1:
corner.undecidedWalls()[0].setValue(False)
changes += 1
return changes
def cornerMaxTwoWalls():
changes = 0
for corner in self.allCorners:
if len(corner.realWalls()) == 2:
for wall in corner.undecidedWalls():
wall.setValue(False)
changes += 1
return changes
self.rules = [
cellUnknownsAllWalls,
cellUnknownsAllEmpties,
cornerWallInMeansWallOut,
cornerNoWallInMeansNoWallOut,
cornerMaxTwoWalls
]
def cleanUp(self):
'''Some checks to do after each applied rule, to get rid of data that we no longer need to check each time '''
self.allCells = [cell for cell in self.allCells if not cell.fulfilled()]
def solved(self):
self.cleanUp()
#todo: also check validity
return len(self.allCells) == 0
def corners(self):
'''Iterates the corners of the puzzle, and for each corner gives the state of its connected walls'''
pass
def getState(self):
ans = []
for i in range(self.height):
ans.append(''.join(topRow(self.topWalls[i])))
ans.append(''.join(cellRow(self.leftWalls[i], self.rows[i])))
ans.append(''.join(topRow(self.topWalls[i+1])))
return '\n'.join(ans)
def getStats(self):
return '{} cells to fulfil:{}'.format(len(self.allCells), [(c.row, c.col, c.getValue()) for c in self.allCells])