Last active
August 21, 2022 21:14
-
-
Save eatnumber1/6a24fb53f08524965875a0bed8d55885 to your computer and use it in GitHub Desktop.
Bitburner CLI tool to print the path to a given host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pathto finds the path of servers between a source and destination. | |
* | |
* @param {NS} ns | |
* @param {String} dest the hostname to try to find a path to | |
* @param {String} src the hostname to start at | |
* @param {Array} tosrc used for internal recursion | |
* @param {Set} seen used for internal recursion | |
* @returns {Array} the path from src to dest, or null if no path exists | |
*/ | |
function pathto(ns, dest, src = ns.getHostname(), tosrc = [src], seen = new Set()) { | |
seen.add(src); | |
if (dest == src) return tosrc; | |
for (const peer of ns.scan(src)) { | |
if (seen.has(peer)) continue; | |
tosrc.push(peer); | |
var path = pathto(ns, dest, peer, tosrc, seen); | |
if (path != null) return path; | |
// No path via this peer. | |
tosrc.pop(); | |
} | |
// No path from this src | |
return null; | |
} | |
/** @param {NS} ns */ | |
export async function main(ns) { | |
if (ns.args.length < 1 || ns.args.length > 2) { | |
ns.tprint("Usage: pathto.js dest [src]"); | |
ns.exit(); | |
} | |
const dest = ns.args[0]; | |
var src = undefined; | |
if (ns.args.length > 1) src = ns.args[1]; | |
ns.tprint(pathto(ns, dest, src)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment