-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14b.js
73 lines (59 loc) · 1.68 KB
/
14b.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
import {readInput} from "./lib.js";
const input = readInput(14).map(line => {
const [x, y, dx, dy] = line.match(/(-?\d+)/g)
return {
x: parseInt(x),
y: parseInt(y),
dx: parseInt(dx),
dy: parseInt(dy),
quadrant: null
}
});
const width = 101;
const height = 103;
(async () => {
for (let i = 0; i < Number.MAX_SAFE_INTEGER; i++) {
for (const robot of input) {
robot.x += robot.dx;
robot.y += robot.dy;
if (robot.x < 0) {
robot.x = width + robot.x;
}
if (robot.y < 0) {
robot.y = height + robot.y;
}
if (robot.x >= width) {
robot.x = robot.x - width;
}
if (robot.y >= height) {
robot.y = robot.y - height;
}
}
// Already submitted, too low.
if (i < 6376) {
continue;
}
// Already submitted, too high.
if (i > 15000) {
break;
}
if (detectFrame(input)) {
const grid = Array.from({length: height}, () => Array.from({length: width}, () => '.'));
for (const robot of input) {
grid[robot.y][robot.x] = '#';
}
console.log(grid.map(row => row.join('')).join('\n'));
console.error(i + 1);
await new Promise(resolve => setTimeout(resolve, 700));
}
}
})();
function detectFrame(robots) {
for (const robot of robots) {
const frameRobots = robots.filter(({y}) => robot.y === y);
if (frameRobots.length > 30) {
return true;
}
}
return false;
}