-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
295 lines (259 loc) · 8.69 KB
/
index.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
const body = document.body;
const grid = document.getElementById("grid");
const GRID_SIZE = 720;
const GRID_DIMENSION = 30;
let CRITICAL_ATTR = 3;
const DIMENSION = GRID_SIZE / GRID_DIMENSION;
let DEFAULT_OPTIONS = [];
let PRNG = new Math.seedrandom(Math.random());
class ImageOption {
static get DIMENSION() {
return DIMENSION;
}
constructor(rotation, image_src, socket_config) {
this.image_src = image_src;
this.rotation = rotation;
this.socket_config = socket_config;
this.options = new Array(4).fill(null).map(() => new Array());
}
formImage = (i, j) => {
const image = document.createElement("img");
image.src = this.image_src;
image.classList.add("image");
image.style = `width: ${ImageOption.DIMENSION}px; height: ${
ImageOption.DIMENSION
}px; transform: translate(${j * ImageOption.DIMENSION}px, ${
i * ImageOption.DIMENSION
}px) rotate(${this.rotation * 90}deg);`;
return image;
};
static analyze = () => {
for (let i = 0; i < DEFAULT_OPTIONS.length; ++i) {
for (let j = 0; j <= i; ++j) {
for (let d = 0; d < 4; ++d) {
if (
DEFAULT_OPTIONS[i].socket_config[d] ===
DEFAULT_OPTIONS[j].socket_config[(d + 2) % 4]
) {
DEFAULT_OPTIONS[i].options[d].push(j);
if (i !== j)
DEFAULT_OPTIONS[j].options[(d + 2) % 4].push(i);
}
}
}
}
};
}
const UNDEFINED_BLOCK = new ImageOption(0, "./svg/undefined.svg", []);
class Block {
constructor(i, j) {
this.options = new Array(DEFAULT_OPTIONS.length)
.fill(0)
.map((_, idx) => idx);
this.i = i;
this.j = j;
this.image = UNDEFINED_BLOCK.formImage(i, j);
this.collapsed = false;
this.selectedImageIdx = -1;
this.rendered = false;
}
intialize = (power = "strong") => {
if (power === "strong") {
grid.removeChild(this.image);
this.rendered = false;
this.image = UNDEFINED_BLOCK.formImage(this.i, this.j);
this.collapsed = false;
this.selectedImageIdx = -1;
}
this.options = new Array(DEFAULT_OPTIONS.length)
.fill(0)
.map((_, idx) => idx);
};
render = () => {
if (!this.rendered) grid.appendChild(this.image);
this.rendered = true;
};
setImage = () => {
this.selectedImageIdx =
this.options[Math.floor(PRNG() * this.options.length)];
if (this.options.length === 0) {
this.selectedImageIdx = -1;
throw "Zero Hogaya";
}
try {
grid.removeChild(this.image);
} catch (e) {
console.log("wow");
}
this.rendered = false;
this.collapsed = true;
this.image = DEFAULT_OPTIONS[this.selectedImageIdx].formImage(
this.i,
this.j
);
};
}
const intialize = (blocks, power) => {
for (const block of blocks) {
block.intialize(power);
}
};
const fetchImageDataJSON = async (sample) => {
const response = await fetch(`./data/${sample}/image_data.json`);
const data = await response.json();
return data;
};
const preloadImages = async () => {
for (const image of DEFAULT_OPTIONS) {
await new Promise((resolve, reject) => {
const img = image.formImage(0, 0);
img.addEventListener("load", () => resolve());
img.addEventListener("error", (err) => reject(err));
});
}
};
const invert = (num) => {
let temp = 0;
for (let i = 0; i < CRITICAL_ATTR; ++i) {
temp *= 10;
temp += num % 10;
num = Math.floor(num / 10);
}
return temp;
};
const rotateArray = (array, rotations) => {
const copy = [...array];
while (rotations-- > 0) {
copy.unshift(invert(copy.pop()));
copy[2] = invert(copy[2]);
}
return copy;
};
const directions = [
[-1, 0],
[0, 1],
[1, 0],
[0, -1],
];
const isValid = (i, j) => {
return 0 <= i && i < GRID_DIMENSION && 0 <= j && j < GRID_DIMENSION;
};
const main = async () => {
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
const JSONdata = await fetchImageDataJSON(params.sample);
const BASE_IMAGE_OPTIONS = JSONdata.images;
CRITICAL_ATTR = JSONdata.critical_attributes;
for (const image of BASE_IMAGE_OPTIONS) {
for (const rotation of image.rotations) {
DEFAULT_OPTIONS.push(
new ImageOption(
rotation,
image.image_src,
rotateArray(image.socket_config, rotation)
)
);
}
}
const blocks = new Array(GRID_DIMENSION * GRID_DIMENSION)
.fill(null)
.map(
(_, j) =>
new Block(Math.floor(j / GRID_DIMENSION), j % GRID_DIMENSION)
);
ImageOption.analyze();
await preloadImages();
const gridRender = async () => {
for (const block of blocks) {
block.render();
}
await new Promise((r) => setTimeout(r, 1));
};
const propagation = () => {
for (const block of blocks) {
if (block.collapsed) continue;
const valid_count = new Array(4)
.fill(null)
.map((_) => new Array(DEFAULT_OPTIONS.length).fill(false));
for (let d = 0; d < 4; ++d) {
const ni = block.i + directions[d][0],
nj = block.j + directions[d][1];
if (
isValid(ni, nj)
// blocks[ni * GRID_DIMENSION + nj].collapsed
) {
if (blocks[ni * GRID_DIMENSION + nj].collapsed)
DEFAULT_OPTIONS[
blocks[ni * GRID_DIMENSION + nj].selectedImageIdx
].options[(d + 2) % 4].forEach((x) => {
valid_count[d][x] = true;
});
else {
blocks[ni * GRID_DIMENSION + nj].options.forEach(
(y) => {
DEFAULT_OPTIONS[y].options[(d + 2) % 4].forEach(
(x) => {
valid_count[d][x] = true;
}
);
}
);
}
} else {
for (let x = 0; x < DEFAULT_OPTIONS.length; ++x)
valid_count[d][x]++;
}
}
block.options = [];
for (let idx = 0; idx < DEFAULT_OPTIONS.length; ++idx) {
if (
valid_count[0][idx] &&
valid_count[1][idx] &&
valid_count[2][idx] &&
valid_count[3][idx]
)
block.options.push(idx);
}
// if (block.options.length === 0) console.log(valid_count);
}
let sampleBlocks = blocks.filter((block) => !block.collapsed);
sampleBlocks.sort((a, b) => a.options.length - b.options.length);
sampleBlocks = sampleBlocks.filter(
(block) => sampleBlocks[0].options.length === block.options.length
);
const randomBlock =
sampleBlocks[Math.floor(PRNG() * sampleBlocks.length)];
try {
randomBlock.setImage();
} catch (e) {
console.error(e);
console.log(randomBlock);
return { success: false, Coord: [] };
}
return { success: true, Coord: [randomBlock.i, randomBlock.j] };
};
let propagations = 0;
let solved = false;
const solve = async () => {
if (propagations === GRID_DIMENSION * GRID_DIMENSION) return true;
const data = propagation();
if (!data.success) return false;
await gridRender();
propagations++;
const result = await solve();
if (result) return true;
propagations--;
blocks[data.Coord[0] * GRID_DIMENSION + data.Coord[1]].intialize();
intialize(blocks, "easy");
PRNG = new Math.seedrandom(`${PRNG()}`);
await gridRender();
return await solve();
};
while (!solved) {
gridRender();
solved = await solve();
if (!solved) intialize(blocks, "strong");
}
};
main();