-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6-2.ts
56 lines (46 loc) · 1.17 KB
/
day6-2.ts
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
import * as fs from "fs"
const input = fs.readFileSync("input-day6.txt", "utf8")
type Node = {id: string, orbiting?: Node, orbitedBy: Node[]}
const nodes = new Map<string, Node>()
const orbits = input.split("\n")
.slice(0, -1)
.map(line => line.split(")"))
orbits.forEach(o => {
nodes.set(o[0], {id: o[0], orbitedBy: []})
nodes.set(o[1], {id: o[1], orbitedBy: []})
})
orbits.forEach(o => {
nodes.get(o[1]).orbiting = nodes.get(o[0])
nodes.get(o[0]).orbitedBy.push(nodes.get(o[1]))
})
function isRoot(o: Node) {
return !o.orbiting
}
const from = nodes.get("YOU")
const to = nodes.get("SAN")
function distanceDF(from: Node, to: Node): number {
if(from == to) {
return 0
}
return Math.min(
...[...from.orbitedBy, from.orbiting].map(o => {
return 1 + distanceDF(o, to)
})
)
}
function distanceBF(from: Node, to: Node): number {
const seen = new Set<string>()
let next = [from.orbiting]
let l = 0
while(true) {
next = next.reduce((acc, n) =>
acc.concat([...n.orbitedBy, n.orbiting]), [])
.filter(n => n && !seen.has(n.id))
next.forEach(n => seen.add(n.id))
l++
if(next.find(n => n.id === to.id)) {
return l-1
}
}
}
console.log(distanceBF(from, to))