-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.js
50 lines (40 loc) · 1.26 KB
/
part1.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
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString().trim();
const robots = [];
const GRID_WIDTH = 101;
const GRID_HEIGHT = 103;
input.split("\n").forEach((line) => {
const regex = /-?\d+/g;
const values = line.match(regex).map((x) => parseInt(x));
robots.push({
id: robots.length,
position: [values[0], values[1]],
velocity: [values[2], values[3]],
});
});
function tick() {
robots.forEach((robot) => {
let x = (robot.position[0] + robot.velocity[0]) % GRID_WIDTH;
let y = (robot.position[1] + robot.velocity[1]) % GRID_HEIGHT;
if (x < 0) x = GRID_WIDTH + x;
if (y < 0) y = GRID_HEIGHT + y;
robot.position = [x, y];
});
}
for (let i = 0; i < 100; i++) {
tick();
}
let quadrants = [0, 0, 0, 0];
let halfWidth = Math.floor(GRID_WIDTH / 2);
let halfHeight = Math.floor(GRID_HEIGHT / 2);
robots.forEach((robot) => {
let [x, y] = robot.position;
if (x < halfWidth && y < halfHeight) quadrants[0]++;
if (x > halfWidth && y < halfHeight) quadrants[1]++;
if (x > halfWidth && y > halfHeight) quadrants[2]++;
if (x < halfWidth && y > halfHeight) quadrants[3]++;
});
let result = quadrants.reduce((acc, num) => acc * num, 1);
console.log(result);