-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgrid.js
144 lines (117 loc) · 3.91 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
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
class Grid {
constructor() {
this.grid = { '0,0': 1 };
this.reverseGrid = { 2: '0,0' };
this.current = '0,0';
this.step = 1;
}
getNeighbors(x, y) {
let neighbors = [];
let neighbor_coords = [
`${x + 1},${y}`,
`${x + 1},${y - 1}`,
`${x},${y - 1}`,
`${x - 1},${y - 1}`,
`${x - 1},${y}`,
`${x - 1},${y + 1}`,
`${x},${y + 1}`,
`${x + 1},${y + 1}`,
];
neighbor_coords.forEach(c => {
if (this.grid[c]) {
neighbors.push(this.grid[c]);
}
});
return neighbors;
}
getNeighborsSum(x, y) {
return this.getNeighbors(x, y).reduce((a, b) => a + b, 0);
}
fillSummedSpiral(distance) {
let x = 0,
y = 0;
let num = 1;
while (distance > 0) {
// Starting at '0,0', move `step` right and up, and `step + 1` left and down
for (let right = 0; right < this.step; right++) {
--distance;
x++;
let neighbors_sum = this.getNeighborsSum(x, y);
let next = `${x},${y}`;
this.grid[next] = neighbors_sum;
this.reverseGrid[neighbors_sum] = next;
this.current = next;
}
for (let up = 0; up < this.step; up++) {
--distance;
y--;
let next = `${x},${y}`;
let neighbors_sum = this.getNeighborsSum(x, y);
this.grid[next] = neighbors_sum;
this.reverseGrid[neighbors_sum] = next;
this.current = next;
}
for (let left = 0; left < this.step + 1; left++) {
--distance;
x--;
let next = `${x},${y}`;
let neighbors_sum = this.getNeighborsSum(x, y);
this.grid[next] = neighbors_sum;
this.reverseGrid[neighbors_sum] = next;
this.current = next;
}
for (let down = 0; down < this.step + 1; down++) {
--distance;
y++;
let neighbors_sum = this.getNeighborsSum(x, y);
let next = `${x},${y}`;
this.grid[next] = neighbors_sum;
this.reverseGrid[neighbors_sum] = next;
this.current = next;
}
this.step += 2;
}
}
fillSpiral(distance) {
let x = 0,
y = 0;
let num = 1;
while (distance > 0) {
// Starting at '0,0', move `step` right and up, and `step + 1` left and down
for (let right = 0; right < this.step; right++) {
--distance;
x++;
let next = `${x},${y}`;
this.grid[next] = ++num;
this.reverseGrid[num] = next;
this.current = next;
}
for (let up = 0; up < this.step; up++) {
--distance;
y--;
let next = `${x},${y}`;
this.grid[next] = ++num;
this.reverseGrid[num] = next;
this.current = next;
}
for (let left = 0; left < this.step + 1; left++) {
--distance;
x--;
let next = `${x},${y}`;
this.grid[next] = ++num;
this.reverseGrid[num] = next;
this.current = next;
}
for (let down = 0; down < this.step + 1; down++) {
--distance;
y++;
let next = `${x},${y}`;
this.grid[next] = ++num;
this.reverseGrid[num] = next;
this.current = next;
}
this.step += 2;
}
}
}
module.exports = Grid;