-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.js
58 lines (55 loc) · 1.76 KB
/
day6.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
const fs = require("fs");
// fs.readFile("day6_input", "utf8", function(err, contents) {
// const orbits = contents.split("\n");
// let objMap = new Map();
// orbits.forEach(v => {
// const [center, obj] = v.split(")");
// !objMap.has(obj) && objMap.set(obj, center);
// });
// const countOrbits = (obj, map, count) =>
// map.has(obj) ? countOrbits(map.get(obj), map, count + 1) : count;
//
// console.log(
// [...objMap.keys()].reduce(
// (acc, obj) => acc + countOrbits(obj, objMap, 0),
// 0
// )
// );
// });
fs.readFile("day6_input", "utf8", function(err, contents) {
const orbits = contents.split("\n");
let objMap = new Map();
let youCenter, sanCenter;
orbits.forEach(v => {
const [center, obj] = v.split(")");
!objMap.has(obj) && objMap.set(obj, center);
if (obj === "YOU") youCenter = center;
if (obj === "SAN") sanCenter = center;
});
const countOrbits = (obj, trackedObj, map, path, transferring) => {
if (map.has(obj) && obj === trackedObj) {
return countOrbits(map.get(obj), trackedObj, map, [...path, obj], true);
} else if (transferring) {
if (obj !== "COM")
return countOrbits(map.get(obj), trackedObj, map, [...path, obj], true);
else return path;
} else return path;
};
const objKeys = [...objMap.keys()];
const youPath = objKeys
.reduce(
(acc, obj) => acc + countOrbits(obj, youCenter, objMap, [], false),
[]
)
.split(",");
const sanPath = objKeys
.reduce(
(acc, obj) => acc + countOrbits(obj, sanCenter, objMap, [], false),
[]
)
.split(",");
const firstCommonObj = youPath.filter(o => sanPath.indexOf(o) !== -1)[0];
console.log(
youPath.indexOf(firstCommonObj) + sanPath.indexOf(firstCommonObj)
);
});