Draco18s

goto.js

Oct 9th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /** @param {NS} ns */
  2. class Node {
  3. constructor(name, depth) {
  4. this.name = name;
  5. this.connections = [];
  6. this.depth = depth;
  7. }
  8.  
  9. equals(other) {
  10. return this.name == other.name;
  11. }
  12. }
  13.  
  14. export async function main(ns) {
  15. ns.disableLog('disableLog');
  16. ns.disableLog('sleep');
  17. ns.disableLog('scan');
  18. if(ns.args.length < 1) return;
  19. var root = new Node('home', 0);
  20. var allConnections = [root.name];
  21. var open = [root];
  22. while(open.length > 0) {
  23. var current = open.pop();
  24. var cons = ns.scan(current.name);
  25. for(var i in cons) {
  26. var c = cons[i];
  27. var n = new Node(c, current.depth+1);
  28. current.connections.push(n);
  29. if(!allConnections.includes(n.name)) {
  30. ns.print('new node ' + n.name);
  31. allConnections.push(n.name);
  32. open.push(n);
  33. }
  34. }
  35. await ns.sleep(1);
  36. }
  37. ns.tprint(Search(root, ns.args[0], 'home'));
  38. }
  39.  
  40. function Search(node, target, path) {
  41. if(node.name == target) return path;
  42. for(var i in node.connections) {
  43. var c = node.connections[i];
  44. if(c.depth < node.depth) continue;
  45. var p = Search(c, target, path + '; connect ' + c.name);
  46. if(p == '') continue;
  47. return p;
  48. }
  49. return '';
  50. }
Add Comment
Please, Sign In to add comment