-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.js
executable file
·401 lines (345 loc) · 9.24 KB
/
solver.js
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
var assert = require('assert');
const UNOCCUPIED = ".";
var Board = function(width, height) {
this.width = width;
this.height = height;
this.data = [];
for (var i=0; i<width; i++) {
var q = [];
for (var j=0; j<height; j++) {
q.push(UNOCCUPIED);
}
this.data.push(q);
}
}
Board.prototype.occupied = function(x,y) {
if (x < 0 || x > this.width-1) return true;
if (y < 0 || y > this.height-1) return true;
if (this.data[x][y] !== UNOCCUPIED) {
return true;
}
return false;
}
Board.prototype.toString = function(packed) {
var str = "";
for (var y=0;y<this.height;y++) {
for (var x=0;x<this.width;x++) {
str += this.data[x][y] + (packed ? "" : " ");
}
str += "\n";
}
return str;
};
Board.prototype.toReallyPrettyString = function() {
var input = this.toString(true).trim().split("\n").map(x=>x.split(''));
var createChecker = function(character, originX, originY) {
return function(dx,dy) {
var x = originX + dx;
var y = originY + dy;
if (y < 0 || y >= input.length) {
return false;
}
if (x < 0 || x >= input[0].length) {
return false;
}
return input[y][x] == character;
}
}
var result = "";
input.forEach((row,y)=> {
var topString = "";
var bottomString = "";
row.forEach((cell,x)=>{
var checker = createChecker(cell, x, y);
var right = checker(1,0);
var up = checker(0,-1);
var left = checker(-1,0);
var down = checker(0,1);
var type =
(right ? 1 : 0) +
(up ? 2 : 0) +
(left ? 4 : 0) +
(down ? 8 : 0);
var glyph = "???\n???";
var lookup = [
" \n ", // ?
"┌──\n└──", // R ╶
"│ │\n└─┘", // U ╵
"│ └\n└──", // RU └
"──┐\n──┘", // L ╴
"───\n───", // R L ─
"┘ │\n──┘", // UL ┘
"┘ └\n───", // RUL ┴
"┌─┐\n│ │", // D ╷
"┌──\n│ ┌", // R D ┌
"│ │\n│ │", // U D │
"│ └\n│ ┌", // RU D ├
"──┐\n┐ │", // LD ┐
"───\n┐ ┌", // R LD ┬
"┘ │\n┐ │", // ULD ┤
"┘ └\n┐ ┌", // RULD ┼
];
glyph = lookup[type].split('\n');
topString += glyph[0];
bottomString += glyph[1];
});
result += topString + "\n" + bottomString + "\n";
});
return result;
}
Board.prototype.toPrettyString = function() {
var input = this.toString(true).trim().split("\n").map(x=>x.split(''));
var createChecker = function(character, originX, originY) {
return function(dx,dy) {
var x = originX + dx;
var y = originY + dy;
if (y < 0 || y >= input.length) {
return false;
}
if (x < 0 || x >= input[0].length) {
return false;
}
return input[y][x] == character;
}
}
var result = "";
input.forEach((row,y)=> {
row.forEach((cell,x)=>{
var checker = createChecker(cell, x, y);
var right = checker(1,0);
var up = checker(0,-1);
var left = checker(-1,0);
var down = checker(0,1);
var type =
(right ? 1 : 0) +
(up ? 2 : 0) +
(left ? 4 : 0) +
(down ? 8 : 0);
var character = "*";
var lookup = [
"?", //
"╶", // R
"╵", // U
"└", // RU
"╴", // L
"─", // R L
"┘", // UL
"┴", // RUL
"╷", // D
"┌", // R D
"│", // U D
"├", // RU D
"┐", // LD
"┬", // R LD
"┤", // ULD
"┼", // RULD
];
character = lookup[type];
result += character;
});
result += "\n";
});
return result;
}
Board.prototype.isSolved = function() {
var board = this;
return this.data.every(function(col,x) {
return col.every(function(cell, y) {
return board.occupied(x,y);
});
});
};
var Direction = function(x,y,name) {
this.x = x;
this.y = y;
this.name = name;
};
var UP = new Direction(0,-1, "up");
var DOWN = new Direction(0,1, "down");
var LEFT = new Direction(-1,0, "left");
var RIGHT = new Direction(1,0, "right");
UP.cw = RIGHT;
UP.ccw = LEFT;
RIGHT.cw = DOWN;
RIGHT.ccw = UP;
DOWN.cw = LEFT;
DOWN.ccw = RIGHT;
LEFT.cw = UP;
LEFT.ccw = DOWN;
var Piece = function(name, data, numDirections) {
this.name = name;
this.data = data.split('');
if (typeof numDirections == "undefined") {
numDirections = 4;
}
if (numDirections == 4) {
this.directions = [UP,DOWN,LEFT,RIGHT];
} else if (numDirections == 2) {
this.directions = [UP,LEFT];
} else if (numDirections == 1) {
this.directions = [UP];
} else {
throw new Error("Unsupported number of directions", numDirections);
}
}
Piece.prototype.tryPlaceAt = function(_char, _x,_y,rot,board) {
if (this.canPlaceAt(_x,_y,rot,board)) {
this.printAt(_char, _x,_y,rot,board);
return true;
} else {
return false;
}
};
Piece.prototype.canPlaceAt = function(_x,_y,rot,board) {
var direction = rot;
var cursor = [_x,_y];
if ( board.occupied( cursor[0], cursor[1] ) ) return false;
cursor[0] += direction.x;
cursor[1] += direction.y;
if ( board.occupied( cursor[0], cursor[1] ) ) return false;
var allNotOccupied = this.data.every(function(instruction) {
if (instruction == "L") {
direction = direction.ccw;
} else if (instruction == "R") {
direction = direction.cw;
} else if (instruction == "B") {
// move back
cursor[0] -= direction.x;
cursor[1] -= direction.y;
return true; // succeeds because no check required
} else if (instruction == "F") {
// stay the course, captain
} else {
throw new Error("Unknown instruction ", instruction);
}
cursor[0] += direction.x;
cursor[1] += direction.y;
var result = !board.occupied( cursor[0], cursor[1] );
return result;
});
return allNotOccupied;
}
Piece.prototype.printAt = function(_char, _x,_y,rot,board) {
var direction = rot;
var cursor = [_x,_y];
board.data[cursor[0]][cursor[1]] = _char;
cursor[0] += direction.x;
cursor[1] += direction.y;
board.data[cursor[0]][cursor[1]] = _char;
this.data.forEach(function(instruction) {
if (instruction == "L") {
direction = direction.ccw;
} else if (instruction == "R") {
direction = direction.cw;
} else if (instruction == "B") {
// move back
cursor[0] -= direction.x;
cursor[1] -= direction.y;
return true; // succeeds because no check required
} else if (instruction == "F") {
// stay the course, captain
} else {
throw new Error("Unknown instruction ", instruction);
}
cursor[0] += direction.x;
cursor[1] += direction.y;
board.data[cursor[0]][cursor[1]] = _char;
});
};
Piece.prototype.erase = function(_x,_y,rot,board) {
this.printAt(UNOCCUPIED, _x,_y,rot,board);
};
// 0 1
// 2 3
// 4 5
//
// 1 3 5
// 0 2 4
var zigL = new Piece("zigL", "LR", 2); // zigzag lh .:-
var zigR = new Piece("zigR", "RL", 2); // zigzag rh -:_
var tee = new Piece("tee", "BLBBBF"); // Tee piece |-
var stick = new Piece("stick", "FF", 2); // long piece ----
var bentL = new Piece("bentL", "FL"); // hockeystick --^
var bentR = new Piece("bentR", "FR"); // hockeystick --,
var square = new Piece("square", "RR"); // square ::
var Solver = function(board, pieces) {
this.board = board;
this.pieces = pieces;
this.moves = [];
this.colours = ["X", "*", "O", "+", "@", "E", "%"];
this.work = 0;
this.lastReported = new Date().getTime();
};
Solver.prototype.solve = function() {
var randomPieces = [];
this.pieces.forEach(p => {
randomPieces.splice(
Math.floor(Math.random() * randomPieces.length),
0,
p
);
});
this.pieces = randomPieces;
//console.log("Solving... Pieces left: " + this.pieces.length);
var piece = this.pieces.pop();
var directions = piece.directions;
if ( (new Date().getTime()) - this.lastReported > 1000) {
console.log("Work done: " + this.work / 1000);
this.lastReported = new Date().getTime();
}
for(var z=0; z<directions.length; z++) {
var direction = directions[z];
for (var x=0; x<this.board.width; x++) {
for (var y=0; y<this.board.height; y++) {
var colour = this.colours[this.moves.length % this.colours.length];
this.work++;
if (piece.tryPlaceAt(colour, x, y, direction, this.board )) {
//console.log("Placed " + piece.name + " at [" + x + "," + y + "] " + direction.name);
//console.log(b.toString());
this.moves.push([ piece.name, x, y, direction.name ]);
if (this.board.isSolved()) {
return true;
}
if (this.pieces.length > 0) {
if ( this.solve() ) {
return true;
}
} else {
console.log("no more pieces");
console.log(this.moves);
console.log();
console.log(this.board.toString());
throw new Error("Not enough pieces to solve this puzzle.");
}
this.work++;
piece.erase(x,y,direction,this.board);
var failedMove = this.moves.pop();
//console.log("--- OOPS, removing " + failedMove[0] + " (" + colour + ") --- ");
assert( // popped move should match what we put on
failedMove[0] == piece.name &&
failedMove[1] == x &&
failedMove[2] == y &&
failedMove[3] == direction.name
);
}
}
}
}
this.pieces.push(piece); // should never happen?
};
exports.pieces = {
zigL: zigL,
zigR: zigR,
tee: tee,
stick: stick,
bentL: bentL,
bentR: bentR,
square: square
};
exports.p = Piece;
exports.Board = Board;
exports.Solver = Solver;
exports.UP = UP;
exports.DOWN = DOWN;
exports.LEFT = LEFT;
exports.RIGHT = RIGHT;