Last active
August 21, 2022 22:07
-
-
Save eatnumber1/7c3208e3ddd0936f54af52cb32d76792 to your computer and use it in GitHub Desktop.
Bitburner utility function to create a generator that yields all hostnames.
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
/** | |
* Create a generator that yields all hostnames. | |
* | |
* @param {NS} ns | |
* @param {String} hostname the host to start traversal at | |
* @param {Set} seen used for internal recursion | |
* @yields {String} a hostname | |
*/ | |
export function* getAllHostnames(ns, hostname = "home", seen = new Set()) { | |
seen.add(hostname); | |
yield hostname; | |
for (const peer of ns.scan(hostname)) { | |
if (seen.has(peer)) continue; | |
yield* getAllHostnames(ns, peer, seen); | |
} | |
} |
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
// Example use of getAllHostnames | |
import {getAllHostnames} from "get_all_hostnames.js"; | |
/** @param {NS} ns */ | |
export async function main(ns) { | |
for (const host of getAllHostnames(ns)) { | |
ns.tprint(host); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment