-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.js
77 lines (70 loc) · 1.99 KB
/
day12.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
const fs = require("fs");
fs.readFile("./day12_input", "utf8", function(err, contents) {
const moons = contents.split("\n").map((m, i) => {
const [x, y, z] = m
.split(", ")
.map(coord => parseInt(coord.match(/-?\d+/)[0], 10));
return { i, x, y, z, v: { x: 0, y: 0, z: 0 } };
});
const perms = array =>
array.reduce(
(acc, val, i1) => [
...acc,
...new Array(array.length - 1 - i1)
.fill(0)
.map((v, i2) => [array[i1], array[i1 + 1 + i2]])
],
[]
);
let moonPairs = perms([0, 1, 2, 3]);
const l = moons.length;
const gravitate = ([m1, m2]) => {
const { x1, y1, z1, v1 } = { x1: m1.x, y1: m1.y, z1: m1.z, v1: m1.v };
const { x2, y2, z2, v2 } = { x2: m2.x, y2: m2.y, z2: m2.z, v2: m2.v };
const pushOrPull = (a1, a2) => (a1 > a2 ? -1 : a1 === a2 ? 0 : 1);
const newV1 = Object.keys(v1).map(
k =>
v1[k] +
(k === "x"
? pushOrPull(x1, x2)
: k === "y"
? pushOrPull(y1, y2)
: pushOrPull(z1, z2))
);
const newV2 = Object.keys(v2).map(
k =>
v2[k] +
(k === "x"
? pushOrPull(x2, x1)
: k === "y"
? pushOrPull(y2, y1)
: pushOrPull(z2, z1))
);
return [
{ x: newV1[0], y: newV1[1], z: newV1[2] },
{ x: newV2[0], y: newV2[1], z: newV2[2] }
];
};
const velocitiesReset = moons => {
const vels = moons.reduce((acc, m) => [...acc, m.v.x, m.v.y, m.v.z], []);
return vels.length === vels.filter(v => v === 0).length;
};
const run = true;
let t = 0;
while (run) {
moonPairs.forEach(pair => {
const [i1, i2] = pair;
const [v1, v2] = gravitate([moons[i1], moons[i2]]);
moons[i1].v = v1;
moons[i2].v = v2;
});
for (let i = 0; i < l; i++) {
moons[i].x += moons[i].v.x;
moons[i].y += moons[i].v.y;
moons[i].z += moons[i].v.z;
}
t += 1;
if (velocitiesReset(moons) && t > 1) break;
}
console.log(t * 2);
});