-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc-2019-day06.js
54 lines (51 loc) · 1.33 KB
/
aoc-2019-day06.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
import aocLoader from 'aoc-loader'
aocLoader(2019, 6, '53616c7465645f5fa933f62e13c8adae8f04b04212c2b41f522aac916a7eb661323b1552217882666e607e10f24d8952'
).then(data => {
//'6WF)DRK', '2PT)PSM', 'H42)FN8', '1XR)LQD'
let mapOfLocalOrbits = data.split('\n')
day6P1(mapOfLocalOrbits)
day6P2(mapOfLocalOrbits)
})
/*
AAA)BBB, which means "BBB is in orbit around AAA".
map of the local orbits
\
\
|
|
AAA--> o o <--BBB
|
|
/
/
*/
export function day6P1(mapOfOrbits) {
let relationships = mapOfOrbits.map(record =>
createOrbitalRelationship(...record.split(')'))
)
let objectAndAroundMap = new Map()
relationships.forEach(r => {
objectAndAroundMap.set(r.obj, r.around)
})
let orbitCount = 0
objectAndAroundMap.forEach((v_around, k_obj) => {
let edgesToCOM = 0
const com = 'COM'
let current = k_obj
while (current !== com) {
let around = objectAndAroundMap.get(current)
edgesToCOM++
current = around
}
orbitCount += edgesToCOM
})
return orbitCount
}
export function createOrbitalRelationship(around, obj) {
let rel = {}
;(rel.obj = obj), (rel.around = around)
return rel
}
function day6P2(data) {
return data.length
}