-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolver.js
More file actions
320 lines (298 loc) · 8.33 KB
/
solver.js
File metadata and controls
320 lines (298 loc) · 8.33 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const levels = [
{
// 2x1 a
columns: [0, 1],
rows: [1],
islands: [0],
pirates: [1],
},
{
// 2x1 b
columns: [1, 0],
rows: [1],
islands: [1],
pirates: [0],
},
{
// 5x5 a1
columns: [2, 0, 1, 1, 1],
rows: [1, 1, 0, 2, 1],
islands: [6, 8, 15, 16, 24],
pirates: [3, 5, 17, 19, 20],
},
{
// 6x6 a1
columns: [0, 2, 0, 2, 1, 2],
rows: [1, 2, 1, 1, 1, 1],
islands: [1, 4, 10, 11, 15, 19, 33],
pirates: [5, 7, 9, 17, 21, 25, 34],
},
{
// 7x7 a1
columns: [2, 1, 0, 2, 1, 2, 1],
rows: [2, 1, 2, 1, 0, 2, 1],
islands: [5, 9, 12, 13, 14, 16, 33, 42, 44],
pirates: [4, 6, 8, 17, 19, 21, 35, 40, 45],
},
{
// 8x8 b19
columns: [2, 1, 1, 3, 1, 1, 1, 3],
rows: [1, 1, 3, 1, 1, 2, 1, 3],
islands: [7, 8, 10, 13, 25, 28, 31, 46, 50, 51, 52, 55, 56],
pirates: [6, 11, 16, 21, 23, 27, 33, 43, 47, 48, 58, 60, 63],
},
];
const none = null;
const unknown = '■';
const sea = '~';
const island = 'X';
const pirate = 'O';
function newBoard(level) {
const board = {
cells: Array(level.columns.length).fill(unknown)
.map(() => Array(level.rows.length).fill(unknown)),
setxy(x, y, val) {
this.cells[x][y] = val;
if (val === pirate) {
this.setSeaSurroundingPirate(x, y);
}
},
seti(i, val) {
const x = Math.floor(i / level.rows.length);
const y = i % level.columns.length;
this.cells[x][y] = val;
if (val === pirate) {
this.setSeaSurroundingPirate(x, y);
}
},
setSeaSurroundingPirate(x, y) {
const surroundingCells = [
[x - 1, y - 1],
[x - 1, y],
[x - 1, y + 1],
[x, y - 1],
[x, y + 1],
[x + 1, y - 1],
[x + 1, y],
[x + 1, y + 1],
];
let errorThatShouldNotHappenIfSolversAreCorrect = false;
surroundingCells.forEach(([xx, yy]) => {
if (yy < 0 || yy >= this.cells.length || xx < 0 || xx >= this.cells[yy].length) {
return;
}
if (this.cells[xx][yy] === pirate) {
errorThatShouldNotHappenIfSolversAreCorrect = true;
} else if (this.cells[xx][yy] !== island) {
this.cells[xx][yy] = sea;
}
if (errorThatShouldNotHappenIfSolversAreCorrect) {
throw Error(`pirate placed at (${xx},${yy}) surrounding existing pirate!`);
}
});
},
getNeighbour(x, y, direction) {
if (x < 0 || x >= this.cells[x].length || y < 0 || y >= this.cells.length) {
return none;
}
try {
switch (direction) {
case 'left':
return this.cells[x - 1][y];
case 'right':
return this.cells[x + 1][y];
case 'above':
return this.cells[x][y - 1];
case 'below':
return this.cells[x][y + 1];
default:
return none;
}
} catch (e) { // probably tried to get neighbour of edge cell
return none;
}
},
getUnknownCount() {
let count = 0;
this.cells.forEach((row) => {
row.forEach((cell) => {
if (cell === unknown) {
count += 1;
}
});
});
return count;
},
};
level.islands.forEach((i) => {
board.seti(i, island);
});
return board;
}
function getBoardConsoleDisplay(board, level) {
let str = ' ';
level.columns.forEach((col) => {
str += `${col} `;
});
str += '\n';
let i = 0;
board.cells.forEach((row) => {
str += `${level.rows[i]} `;
i += 1;
row.forEach((cell) => {
str += `${cell} `;
});
str += '\n';
});
return str;
}
function getLevelConsoleDisplay(level) {
let str = ' ';
level.columns.forEach((col) => {
str += `${col} `;
});
try {
let islandIndex = 0;
let pirateIndex = 0;
let rowIndex = 0;
for (let i = 0; i < level.columns.length * level.rows.length; i += 1) {
if (i % level.columns.length === 0) {
str += `\n ${level.rows[rowIndex]} `;
rowIndex += 1;
}
if (pirateIndex < level.pirates.length && level.pirates[pirateIndex] === i) {
str += `${pirate} `;
pirateIndex += 1;
} else if (islandIndex < level.islands.length && level.islands[islandIndex] === i) {
str += `${island} `;
islandIndex += 1;
} else {
str += `${sea} `;
}
}
} catch (e) {
// ignore error so that we can spot how far the concatenation went before failing
}
return str;
}
function getBoardAndLevelConsoleDisplay(board, level) {
const boardString = getBoardConsoleDisplay(board, level);
const levelString = getLevelConsoleDisplay(level);
const boardSplit = boardString.split('\n').filter(s => s !== '');
const levelSplit = levelString.split('\n').filter(s => s !== '');
let str = '';
for (let i = 0; i < boardSplit.length; i += 1) {
str += `${boardSplit[i]} ${levelSplit[i]}\n`;
}
return str;
}
function applySolver(level, board, solver) {
const unknownsBefore = board.getUnknownCount();
solver(board, level);
const unknownsAfter = board.getUnknownCount();
const solved = unknownsBefore - unknownsAfter;
console.log(`${solver.name} applied: (${solved} cells solved, ${unknownsAfter} left)`);
console.log(getBoardAndLevelConsoleDisplay(board, level));
}
function solveZeroes(board, level) {
level.rows.forEach((row, i) => {
if (row === 0) {
for (let x = 0; x < level.columns.length; x += 1) {
if (board.cells[i][x] !== island) {
board.setxy(i, x, sea);
}
}
}
});
level.columns.forEach((col, i) => {
if (col === 0) {
for (let y = 0; y < level.rows.length; y += 1) {
if (board.cells[y][i] !== island) {
board.setxy(y, i, sea);
}
}
}
});
}
function solveOpenSeas(board) {
const b = board;
for (let y = 0; y < b.cells.length; y += 1) {
for (let x = 0; x < b.cells[y].length; x += 1) {
if (b.cells[x][y] === unknown) {
const left = b.getNeighbour(x, y, 'left');
const right = b.getNeighbour(x, y, 'right');
const above = b.getNeighbour(x, y, 'above');
const below = b.getNeighbour(x, y, 'below');
if (left !== island && right !== island && above !== island && below !== island) {
b.cells[x][y] = sea;
}
}
}
}
}
function solvePerfectRemainingRows(board, level) {
level.rows.forEach((row, rowIndex) => {
let unknownCount = 0;
let pirateCount = 0;
board.cells[rowIndex].forEach((cell) => {
if (cell === unknown) {
unknownCount += 1;
} else if (cell === pirate) {
pirateCount += 1;
}
});
if (row === unknownCount + pirateCount) {
board.cells[rowIndex].forEach((cell, columnIndex) => {
if (cell === unknown) {
board.setxy(rowIndex, columnIndex, pirate);
}
});
} else if (row === pirateCount && unknownCount > 0) {
board.cells[rowIndex].forEach((cell, columnIndex) => {
if (cell === unknown) {
board.setxy(rowIndex, columnIndex, sea);
}
});
}
});
}
function solvePerfectRemainingColumns(board, level) {
level.columns.forEach((column, columnIndex) => {
let unknownCount = 0;
let pirateCount = 0;
level.rows.forEach((row, rowIndex) => {
if (board.cells[rowIndex][columnIndex] === unknown) {
unknownCount += 1;
} else if (board.cells[rowIndex][columnIndex] === pirate) {
pirateCount += 1;
}
});
if (column === unknownCount + pirateCount) {
level.rows.forEach((row, rowIndex) => {
if (board.cells[rowIndex][columnIndex] === unknown) {
board.setxy(rowIndex, columnIndex, pirate);
}
});
} else if (column === pirateCount && unknownCount > 0) {
level.rows.forEach((row, rowIndex) => {
if (board.cells[rowIndex][columnIndex] === unknown) {
board.setxy(rowIndex, columnIndex, sea);
}
});
}
});
}
function solve(level) {
const board = newBoard(level);
console.log(`initial: (${board.getUnknownCount()} unknowns)`);
console.log(getBoardAndLevelConsoleDisplay(board, level));
try {
applySolver(level, board, solveZeroes);
applySolver(level, board, solveOpenSeas);
applySolver(level, board, solvePerfectRemainingRows);
applySolver(level, board, solvePerfectRemainingColumns);
} catch (e) {
console.log(e);
}
}
solve(levels[3]);