Skip to content

Instantly share code, notes, and snippets.

@traverse
Created December 27, 2021 23:53
Show Gist options
  • Save traverse/b37242b6be8d489ca4846e5c1973c841 to your computer and use it in GitHub Desktop.
Save traverse/b37242b6be8d489ca4846e5c1973c841 to your computer and use it in GitHub Desktop.
Bitburner hostname path finder/printer
/** @param {NS} ns **/
export async function main(ns) {
const { hostname } = ns.flags([
["hostname", ""],
])
const network = new Network(ns, "home")
if (hostname) {
printPath(ns, network, hostname)
}
}
function printPath(ns, network, hostname) {
if (network.hostname === hostname) {
let current = network
const result = []
while (current) {
result.push(current.hostname)
current = current.parent
}
ns.tprint(result.reverse().join('/'))
}
network.children.forEach(child => printPath(ns, child, hostname))
}
class Network {
#children = new Set()
#ns
#hostname
#parentNetwork = null
constructor(ns, hostname, parentNetwork = null) {
this.#ns = ns
this.#hostname = hostname
this.#parentNetwork = parentNetwork
this.#ns.scan(hostname)
.filter(childHostname => {
return childHostname !== this.#parentNetwork?.hostname
})
.forEach(childHostname => {
this.#children.add(new Network(this.#ns, childHostname, this))
})
}
get parent() {
return this.#parentNetwork
}
get hostname() {
return this.#hostname
}
get children() {
return this.#children
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment