-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
32 lines (27 loc) · 884 Bytes
/
part2.ts
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
import * as fs from 'fs';
const robots = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((line) => {
const [, px, py, vx, vy] = line.match(/p=(.+),(.+) v=(.+),(.+)/);
return {
position: { x: Number(px), y: Number(py) },
velocity: { x: Number(vx), y: Number(vy) },
};
});
const WIDTH = 101;
const HEIGHT = 103;
const TREE_SEARCH = '#'.repeat(10); // 10 robots in a line is already a weird case that representing a christmas tree
let result = 0;
while (++result) {
let map = Array.from({ length: HEIGHT }, () => Array(WIDTH).fill('.'));
robots.forEach((robot) => {
robot.position = {
x: (WIDTH + robot.position.x + robot.velocity.x) % WIDTH,
y: (HEIGHT + robot.position.y + robot.velocity.y) % HEIGHT,
};
map[robot.position.y][robot.position.x] = '#';
});
if (map.some((row) => row.join('').includes(TREE_SEARCH))) break;
}
console.log(result);