-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14a.js
55 lines (46 loc) · 1.29 KB
/
14a.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
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;
const seconds = 100;
for (let i = 0; i < seconds; 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;
}
}
}
const scores = input
.filter(({x, y}) => x !== Math.floor(width / 2) && y !== Math.floor(height / 2))
.map((robot) => {
const { x, y } = robot;
robot.quadrant = `${Math.round(x / width)},${Math.round(y / height)}`;
return robot;
})
.filter((robot) => robot.quadrant)
.reduce((acc, robot) => {
acc[robot.quadrant] = (acc[robot.quadrant] || 0) + 1;
return acc;
}, {});
console.log(scores);
console.log(Object.values(scores).reduce((acc, score) => acc * score, 1));