-
Notifications
You must be signed in to change notification settings - Fork 16
/
grid.js
80 lines (64 loc) · 2.09 KB
/
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
class Grid {
constructor() {
// Store grid in lookup table to allow for "infinite" room.
this.grid = { '1,1': 20151125 };
}
// Note: this is slow / memory heavy for large row / col values!
fillTo({ row, column } = {}) {
let prev_value = this.grid['1,1'];
const final_cell_key = `${column},${row}`;
// The first code is 20151125.
// After that, each code is generated by taking the previous one,
// multiplying it by 252533, and then keeping the remainder from
// dividing that value by 33554393
let x = 1,
y = 2;
while (this.grid[final_cell_key] == null) {
while (y > 0) {
let current_cell = `${x},${y}`;
let new_value = (prev_value * 252533) % 33554393;
this.grid[current_cell] = new_value;
prev_value = new_value;
y--;
x++;
}
y = x;
x = 1;
}
return this.grid[final_cell_key];
}
getValueAt({ row, column } = {}) {
let prev_value = 20151125;
// The first code is 20151125.
// After that, each code is generated by taking the previous one,
// multiplying it by 252533, and then keeping the remainder from
// dividing that value by 33554393
let x = 1,
y = 2;
while (true) {
while (y > 0) {
let new_value = (prev_value * 252533) % 33554393;
if (x === column && y === row) {
return new_value;
}
prev_value = new_value;
y--;
x++;
}
y = x;
x = 1;
}
}
printArray({ row, column }) {
let grid = Array(row)
.fill()
.map(r => Array(column).fill());
for (let y = 1; y <= row; y++) {
for (let x = 1; x <= column; x++) {
grid[y - 1][x - 1] = this.grid[`${x},${y}`];
}
}
console.log(grid);
}
}
module.exports = Grid;