-
Notifications
You must be signed in to change notification settings - Fork 17
/
infinite-grid.js
416 lines (361 loc) · 10.2 KB
/
infinite-grid.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const Heap = require('heap');
/**
* @typedef {String} GridId - Two numbers separated by a comma.
* @example "10,5"
*/
/**
* @typedef {Object} InfiniteGridConstructorOptions
* @property {?Function<x, y>} defaultFactory - Defaults to returning 0 for new coords
* @property {?Object} string_map - Map grid values to strings.
* @property {?String|any[][]} load - Initial grid to load. Can be a "2D" string (string with new lines), or a "2D" array.
* @property {?Function<v>} parseAs - When `load` is defined, this parses the cell in the split string. Defaults to `String`.
*/
class InfiniteGrid {
/**
* @param {InfiniteGridConstructorOptions} options
*/
constructor({ defaultFactory = (x, y) => 0, string_map = {}, load: loadStr, parseAs } = {}) {
this.defaultFactory = defaultFactory.bind(this);
this.string_map = string_map;
this.grid = new Map();
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
if (loadStr) {
this.load(loadStr, parseAs);
}
}
/**
* @param {Number} x
* @param {Number} y
* @returns {GridId}
*/
static toId(x, y) {
return `${x},${y}`;
}
/**
* @param {GridId} id
* @param {Boolean} [return_as_object=false]
* @returns {{x: Number, y: Number} | [Number, Number]}
*/
static toCoords(id, return_as_object = false) {
let [x, y] = id.split(',');
x = parseInt(x, 10);
y = parseInt(y, 10);
return return_as_object ? { x, y } : [x, y];
}
/**
* @param {String} two_dimensional_string
* @returns {any[][]}
*/
static split(two_dimensional_string) {
return two_dimensional_string.split('\n').map((row) => row.split(''));
}
static moveInDirection(x, y, direction) {
switch (direction) {
case 'N':
return [x, y - 1];
case 'W':
return [x - 1, y];
case 'E':
return [x + 1, y];
case 'S':
return [x, y + 1];
case 'NW':
return [x - 1, y - 1];
case 'NE':
return [x + 1, y - 1];
case 'SW':
return [x - 1, y + 1];
case 'SE':
return [x + 1, y + 1];
default:
throw new Error(
'Invalid direction for moveInDirection. Valid directions are N, W, E, S, NW, NE, SW, SE'
);
}
}
reset() {
this.grid = new Map();
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
return this;
}
/**
* @param {String|any[][]} input
*/
load(input, parseAs = String) {
this.reset();
let grid = input;
if (typeof input === 'string') {
grid = InfiniteGrid.split(input);
}
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
this.set(x, y, parseAs(grid[y][x]));
}
}
}
getRow(x, y, include_self = false) {
const self_id = InfiniteGrid.toId(x, y);
let cell_ids = Array(Math.abs(this.max_x - this.min_x) + 1)
.fill()
.map((_, i) => InfiniteGrid.toId(this.min_x + i, y));
if (!include_self) {
cell_ids = cell_ids.filter((id) => id !== self_id);
}
return cell_ids.map((id) => this.grid.get(id));
}
getCol(x, y, include_self = false) {
const self_id = InfiniteGrid.toId(x, y);
let cell_ids = Array(Math.abs(this.max_y - this.min_y) + 1)
.fill()
.map((_, i) => InfiniteGrid.toId(x, this.min_y + i));
if (!include_self) {
cell_ids = cell_ids.filter((id) => id !== self_id);
}
return cell_ids.map((id) => this.grid.get(id));
}
/**
* @todo The "wrap around" only really makes sense in a rectangular grid.
* Try to code in the cases where we have some cols/rows that are larger than others.
*/
getNeighbor(x, y, direction, { wrap_around = false } = {}) {
if (!this.inBounds(x, y)) {
return;
}
const coord = InfiniteGrid.moveInDirection(x, y, direction);
const [new_x, new_y] = coord;
if (this.inBounds(new_x, new_y)) {
return [this.get(new_x, new_y), coord];
} else if (wrap_around) {
if (this.inBounds(new_x) && !this.inBounds(undefined, new_y)) {
if (direction === 'N') {
// Wrap to bottom
return [this.get(new_x, this.max_y), [new_x, this.max_y]];
} else {
// Wrap to top
return [this.get(new_x, this.min_y), [new_x, this.min_y]];
}
} else if (!this.inBounds(new_x) && this.inBounds(undefined, new_y)) {
if (direction === 'E') {
// Wrap to left
return [this.get(this.min_x, new_y), [this.min_x, new_y]];
} else {
// Wrap to right
return [this.get(this.max_x, new_y), [this.max_x, new_y]];
}
}
}
}
/**
* @param {Number} x
* @param {Number} y
* @param {Boolean} [diagonals=false]
* @returns {Map} Return a map with optional keys N, W, E, S (and NW, NE, SW, SE if `diagonals` is true) if those neighbors are within the bounds of the map.
*/
neighbors(x, y, diagonals = false) {
const neighboring_cells = new Map();
if (!this.inBounds(x, y)) {
return neighboring_cells;
}
const neighbors_lookup = [
['N', [x, y - 1]],
['W', [x - 1, y]],
['E', [x + 1, y]],
['S', [x, y + 1]],
];
if (diagonals) {
neighbors_lookup.push(
['NW', [x - 1, y - 1]],
['NE', [x + 1, y - 1]],
['SW', [x - 1, y + 1]],
['SE', [x + 1, y + 1]]
);
}
for (let [key, coord] of neighbors_lookup) {
let [cx, cy] = coord;
if (this.inBounds(cx, cy)) {
neighboring_cells.set(key, {
id: InfiniteGrid.toId(cx, cy),
coord,
value: this.get(cx, cy),
});
}
}
return neighboring_cells;
}
/**
* @param {Number} x
* @param {Number} y
* @param {any} value
*/
set(x, y, value) {
if (typeof x !== 'number' || typeof y !== 'number') {
throw new Error(`x and y must be numbers, got (${typeof x})${x} and (${typeof y})${y}`);
}
if (x < this.min_x) this.min_x = x;
if (x > this.max_x) this.max_x = x;
if (y < this.min_y) this.min_y = y;
if (y > this.max_y) this.max_y = y;
const id = InfiniteGrid.toId(x, y);
this.grid.set(id, value);
}
/**
* @param {Number} x
* @param {Number} y
* @returns {any}
*/
get(x, y) {
const id = InfiniteGrid.toId(x, y);
if (!this.grid.has(id)) {
this.set(x, y, this.defaultFactory(x, y));
}
return this.grid.get(id);
}
/**
* @param {RegExp|any} value
* @param {Boolean} [as_coords] - When true, the 2nd element of each array element returned is an Array of `[x, y]` number values, otherwise are string IDs.
* @returns {Array<[any,GridId|Coord]>} - Returns an Array, the first value matching the cell found, and the 2nd the coords or ID.
*/
findAll(value, as_coords = true) {
const found = [];
for (let [id, cell] of this.grid) {
const check = value instanceof RegExp ? value.test(cell) : value === cell;
if (check) {
found.push([cell, as_coords ? InfiniteGrid.toCoords(id) : id]);
}
}
return found;
}
inBounds(x, y) {
if (x !== undefined && y !== undefined) {
return x >= this.min_x && x <= this.max_x && y >= this.min_y && y <= this.max_y;
} else if (x !== undefined && y === undefined) {
return x >= this.min_x && x <= this.max_x;
} else if (x === undefined && y !== undefined) {
return y >= this.min_y && y <= this.max_y;
}
}
clone({ empty = false } = {}) {
const infinite_grid_clone = new InfiniteGrid();
const new_map = new Map();
if (!empty) {
for (let [key, val] of this.grid) {
new_map.set(key, typeof val === 'object' ? JSON.parse(JSON.stringify(val)) : val);
}
}
infinite_grid_clone.defaultFactory = this.defaultFactory.bind(this);
infinite_grid_clone.string_map = JSON.parse(JSON.stringify(this.string_map));
infinite_grid_clone.grid = new_map;
infinite_grid_clone.max_x = this.max_x;
infinite_grid_clone.min_x = this.min_x;
infinite_grid_clone.max_y = this.max_y;
infinite_grid_clone.min_y = this.min_y;
return infinite_grid_clone;
}
sum() {
let sum = 0;
for (let value of this.grid.values()) {
sum += value;
}
return sum;
}
resize() {
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
for (let id of this.grid.keys()) {
let [x, y] = InfiniteGrid.toCoords(id);
if (x < this.min_x) this.min_x = x;
if (x > this.max_x) this.max_x = x;
if (y < this.min_y) this.min_y = y;
if (y > this.max_y) this.max_y = y;
}
}
buildDijkstrasFrontier(from_x, from_y) {
const from_id = InfiniteGrid.toId(from_x, from_y);
// Sort our frontier by its priority, so we pick nodes to visit that have the lowest cost.
const frontier = new Heap((node_a, node_b) => node_a.priority - node_b.priority);
frontier.push({ id: from_id, priority: 0 });
const came_from = new Map([[from_id, null]]);
const cost_so_far = new Map([[from_id, 0]]);
while (!frontier.empty()) {
const current = frontier.pop();
const [current_x, current_y] = InfiniteGrid.toCoords(current.id);
for (let next of this.neighbors(current_x, current_y).values()) {
const new_cost = cost_so_far.get(current.id) + next.value;
if (!cost_so_far.has(next.id) || new_cost < cost_so_far.get(next.id)) {
cost_so_far.set(next.id, new_cost);
frontier.push({ id: next.id, priority: new_cost });
came_from.set(next.id, current.id);
}
}
}
return came_from;
}
getShortestWeightedPath(from_x, from_y, to_x, to_y, { include_from = true } = {}) {
const from_id = InfiniteGrid.toId(from_x, from_y);
const to_id = InfiniteGrid.toId(to_x, to_y);
const came_from = this.buildDijkstrasFrontier(from_x, from_y);
let current = to_id;
let path = [];
while (current !== from_id) {
path.push(current);
current = came_from.get(current);
}
if (include_from) {
path.push(from_id);
}
path.reverse();
return path;
}
toGrid() {
let grid = [];
for (let y = this.min_y; y <= this.max_y; y++) {
let row = [];
for (let x = this.min_x; x <= this.max_x; x++) {
let cell = this.get(x, y);
row.push(cell);
}
grid.push(row);
}
return grid;
}
toJSON() {
return this.toGrid();
}
toString() {
let grid = this.toGrid();
let rows = '';
for (let y = 0; y < grid.length; y++) {
let row = '';
for (let x = 0; x < grid[y].length; x++) {
let cell = grid[y][x];
let cell_string = cell in this.string_map ? this.string_map[cell] : String(cell);
row += cell_string;
}
rows += rows.length ? '\n' + row : row;
}
return rows;
}
*[Symbol.iterator]() {
yield* this.grid.entries();
}
entries() {
return this.grid.entries();
}
values() {
return this.grid.values();
}
keys() {
return this.grid.keys();
}
}
module.exports = {
InfiniteGrid,
};