-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.js
279 lines (226 loc) · 7.15 KB
/
executor.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
"use strict";
const vm = require("vm");
const fs = require("fs");
const getInterior = require("./helpers/get-interior");
const createObjectFromPoint = require("./helpers/create-object-from-point");
const getLeastUsedColor = require("./helpers/get-least-used-color");
const selectObjectsOfColor = require("./helpers/select-objects-of-color");
const getNumberOfNonBackgroundSquares = require("./helpers/get-number-of-non-background-squares");
const colors = {
"black": 0,
"blue": 1,
"red": 2,
"green": 3,
"yellow": 4,
"gray": 5,
"pink": 6,
"orange": 7,
"light blue": 8,
"dark red": 9
}
const checkColorParameter = (color) => {
if (!Object.keys(colors).includes(color)) {
throw new Error("Invalid color parameter");
}
}
// Get all objects in the grid. An object is a connected blob of non background
// color squares.
// Returns a 2D array containing objects and the points belonging to them
// in the format:
// [ [[y, x], [y, x]], ... ]
const getAllObjects = (grid, backgroundColor = 0) => {
const objects = [];
for (const [y, row] of grid.entries()) {
for (const [x, color] of row.entries()) {
if (color !== backgroundColor) {
// This square is not of background color. If it does not belong to an
// object yet, form a new object from it by recursively grouping it with
// all non background color squares that are connected to it.
let alreadyAssigned = false;
for (const coords of objects) {
for (const [y2, x2] of coords) {
if (y === y2 && x === x2) {
alreadyAssigned = true;
break;
}
}
if (alreadyAssigned) break;
}
if (!alreadyAssigned) {
objects.push(createObjectFromPoint(y, x, grid, backgroundColor));
}
}
}
}
return objects;
}
// Return null if placeholder is not a real placeholder to simplify logic
// (so this function can be called and its result only used if truthy)
const getValueOfPlaceholder = (grid, placeholder) => {
if (placeholder === "$least-used-color") {
return getLeastUsedColor(grid);
}
if (placeholder === "$numberOfNonBackgroundSquaresInGrid") {
return getNumberOfNonBackgroundSquares(grid);
}
}
// Given a selector object, returns the targeted objects on the grid
const select = (selector, grid) => {
// Background color is black by default
const backgroundColor = 0;
// Replace placeholder tokens in selector object properties with the real
// values. Thus the code below only has to deal with literal values.
for (const key of Object.keys(selector)) {
const realValue = getValueOfPlaceholder(grid, selector[key]);
if (realValue) {
selector[key] = realValue;
}
}
// Create initial objects array, which holds the coordinates of all objects
// of non background color in the grid
let objects = getAllObjects(grid, backgroundColor)
console.log(
`Found ${objects.length} object${objects.length !== 1 ? "s" : ""} in the grid.`
);
if (selector.color) {
objects = selectObjectsOfColor(objects, grid, selector.color);
}
// selector.part: only supports "interior" for now
if (selector.part) {
if (!["interior"].includes(selector.part)) {
throw new Error("Invalid selector.part");
}
if (selector.part === "interior") {
objects = objects.map(x => getInterior(x, grid, backgroundColor));
}
}
// selector.contains-most-squares-of-color: selects the object with the most
// squares of the given color
if (selector["contains-most-squares-of-color"]) {
const c = selector["contains-most-squares-of-color"];
const counts = [];
for (const [i, points] of objects.entries()) {
counts[i] = 0;
for (const [y, x] of points) {
if (grid[y][x] === c) {
counts[i] += 1;
}
}
}
const index = counts.indexOf(Math.max(...counts));
objects = [objects[index]]
}
console.log(
`After running selector, ${objects.length} object${objects.length !== 1 ? "s" : ""} selected.`
);
return objects;
}
module.exports = (pathToProgram, grid, outputHtml = null) => {
// Changes the color of targeted squares
const paint = (selector, color) => {
checkColorParameter(color);
const colorIndex = colors[color];
const selectedObjects = select(selector, grid);
for (const coords of selectedObjects) {
for (const [y, x] of coords) {
grid[y][x] = colorIndex;
}
}
}
// Crops the grid to display only the selected objects.
// Crop coordinates are the maximum and minimum X and Y of all the points in
// the objects.
const crop = (selector) => {
const selectedObjects = select(selector, grid);
let minY = null;
let maxY = null;
let minX = null;
let maxX = null;
for (const obj of selectedObjects) {
for (const [y, x] of obj) {
if (minY === null || y < minY) minY = y;
if (maxY === null || y > maxY) maxY = y;
if (minX === null || x < minX) minX = x;
if (maxX === null || x > maxX) maxX = x;
}
}
const newGrid = [];
for (const [y, row] of grid.entries()) {
if (y >= minY && y <= maxY) {
const newRow = row.slice(minX, maxX + 1);
newGrid.push(newRow);
}
}
grid = newGrid;
}
// Scales the grid up or down
const scaleGrid = (upOrDown, factor) => {
if (upOrDown !== "up" && upOrDown !== "down") {
throw new Error("Invalid upOrDown");
}
if (upOrDown === "down") {
throw new Error("Unsupported upOrDown = down");
}
// Factor supports placeholder tokens
const realValue = getValueOfPlaceholder(grid, factor);
if (realValue) {
factor = realValue;
}
const newGrid = [];
if (upOrDown === "up") {
for (const row of grid) {
const newRow = [];
for (const color of row) {
for (let i = 0; i < factor; i++) {
newRow.push(color);
}
}
for (let i = 0; i < factor; i++) {
newGrid.push(newRow);
}
}
}
grid = newGrid;
}
const outline = (selector, color) => {
checkColorParameter(color);
const selectedObjects = select(selector, grid);
for (const points of selectedObjects) {
for (const [y, x] of points) {
let neighbors = [
[y, x + 1],
[y, x - 1],
[y + 1, x],
[y - 1, x],
[y + 1, x + 1],
[y + 1, x - 1],
[y - 1, x + 1],
[y - 1, x - 1]
];
// Remove neighbors out of bounds
neighbors.filter(
([ny, nx]) => ny >= 0 && nx >= 0 && ny < grid.length && nx < grid[0].length
);
// Remove neighbors of non background color
neighbors.filter(
([ny, nx]) => grid[ny][nx] === 0 // TODO: do not hardcode background color
);
for (const [ny, nx] of neighbors) {
grid[ny][nx] = colors[color];
}
}
}
}
const script = fs.readFileSync(pathToProgram, "utf8");
vm.runInNewContext(script, {
paint,
crop,
scaleGrid,
outline
});
if (outputHtml) {
const gridToCanvas = require("./grid-to-canvas");
gridToCanvas(grid, "output");
}
return grid;
};