-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
40 lines (32 loc) · 1.03 KB
/
main.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
const fs = require('fs')
const filename = 'input.txt'
const file = fs.readFileSync(filename).toString('utf8')
console.log('filename:', filename)
const orbits_list = file.trim().split('\n')
.map(line => line.split(')'))
.map(([orbits, object]) => ({
object, orbits
}))
const objects = {}
orbits_list.forEach(({object, orbits}) => objects[object] = orbits)
function objectOrbits(objects, object) {
const subOrbits = []
while (object !== 'COM') {
object = objects[object]
subOrbits.push(object)
}
return subOrbits
}
function findOrbitalTransfers(objects, objA, objB) {
const orbitsA = objectOrbits(objects, objA).reverse()
const orbitsB = objectOrbits(objects, objB).reverse()
while(orbitsA[1] === orbitsB[1]) {
orbitsA.shift()
orbitsB.shift()
}
return [...orbitsA.reverse(), ...orbitsB.slice(1)]
}
console.log('Part 1 =', Object.keys(objects)
.map(object => objectOrbits(objects, object).length)
.reduce((a, b) => a + b))
console.log('Part 2 =', findOrbitalTransfers(objects, 'YOU', 'SAN').length - 1)