Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns */
- class Node {
- constructor(name, depth) {
- this.name = name;
- this.connections = [];
- this.depth = depth;
- }
- equals(other) {
- return this.name == other.name;
- }
- }
- export async function main(ns) {
- ns.disableLog('disableLog');
- ns.disableLog('sleep');
- ns.disableLog('scan');
- if(ns.args.length < 1) return;
- var root = new Node('home', 0);
- var allConnections = [root.name];
- var open = [root];
- while(open.length > 0) {
- var current = open.pop();
- var cons = ns.scan(current.name);
- for(var i in cons) {
- var c = cons[i];
- var n = new Node(c, current.depth+1);
- current.connections.push(n);
- if(!allConnections.includes(n.name)) {
- ns.print('new node ' + n.name);
- allConnections.push(n.name);
- open.push(n);
- }
- }
- await ns.sleep(1);
- }
- ns.tprint(Search(root, ns.args[0], 'home'));
- }
- function Search(node, target, path) {
- if(node.name == target) return path;
- for(var i in node.connections) {
- var c = node.connections[i];
- if(c.depth < node.depth) continue;
- var p = Search(c, target, path + '; connect ' + c.name);
- if(p == '') continue;
- return p;
- }
- return '';
- }
Add Comment
Please, Sign In to add comment