-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_6.js
120 lines (96 loc) · 2.92 KB
/
day_6.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var fs = require('fs');
var input = fs.readFileSync('input_6.txt', 'utf-8');
var inputMap = input.trim().split('\n');
// var testmap = ['COM)B','B)C','C)D','D)E','E)F','B)G','G)H','D)I','E)J','J)K','K)L'];
// var testmap2 = ['COM)B','B)C','C)D','D)E','E)F','B)G','G)H','D)I','E)J','J)K','K)L','K)YOU','I)SAN'];
// getTotalOrbitCount(inputMap);
getDistanceToSanta(inputMap);
function getTotalOrbitCount(map) {
let objectMap = {};
map.forEach(function(orbit) {
let orbitingObject = orbit.split(')')[1];
let mass = orbit.split(')')[0];
objectMap[orbitingObject] = mass;
});
console.log(objectMap);
let total = 0;
Object.keys(objectMap).forEach(function(key) {
console.log(key, getTotalOrbits(key));
total += getTotalOrbits(key);
});
console.log('TOTAL ORBITS', total);
function getTotalOrbits(object) {
if (object === 'COM') return 0;
else return 1 + getTotalOrbits(objectMap[object]);
}
}
//This is an n-ary tree I have to traverse
function getDistanceToSanta(map) {
let objectMap = {};
map.forEach(function(orbit) {
let orbitingObject = orbit.split(')')[1];
let mass = orbit.split(')')[0];
objectMap[orbitingObject] = mass;
});
var treeMap = new Tree('COM');
let youNode = null;
getChildNodes(treeMap.root);
console.log('TREE MAP', JSON.stringify(treeMap));
console.log('DISTANCE', distance(treeMap.root, 'YOU', 'SAN'));
function getChildNodes(node) {
//Find all the values that have this key as a parent
Object.keys(objectMap).forEach(function(orbitingObject) {
if (objectMap[orbitingObject] === node.name) {
let newNode = new Node(orbitingObject);
if (orbitingObject === 'YOU') {
youNode = newNode;
}
node.children.push(newNode);
getChildNodes(newNode);
}
})
}
//https://www.geeksforgeeks.org/find-distance-between-two-nodes-of-a-binary-tree/
function pathToNode(root, path, k) {
if (!root) return false;
path.push(root.name);
if (root.name === k) {
return true;
}
let isCorrectPath = root.children.some(function(node) {
return pathToNode(node, path, k);
});
if (isCorrectPath) return isCorrectPath;
else {
path.pop();
return false;
}
}
function distance(root, name1, name2) {
if (root) {
var path1 = [];
pathToNode(root, path1, name1);
var path2 = [];
pathToNode(root, path2, name2);
let i = 0;
while (i < path1.length && i < path2.length) {
if (path1[i] !== path2[i]) {
break;
}
i = i + 1;
}
return path1.length + path2.length - 2 * i - 2;
}
else return 0;
}
}
//https://medium.com/@khushboo.taneja_61450/implementing-binary-search-tree-and-n-ary-tree-in-javascript-ba3e2081d345
function Node(name) {
this.name = name;
this.parent = null;
this.children = [];
}
function Tree(name) {
var node = new Node(name);
this.root = node;
}