From 1ca00b3b73a9699e41f81e154cea32934c16de03 Mon Sep 17 00:00:00 2001 From: AlmightyMiau Date: Sat, 26 Oct 2024 13:06:14 -0700 Subject: First commit --- bitburnerFiles1026/route.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 bitburnerFiles1026/route.js (limited to 'bitburnerFiles1026/route.js') diff --git a/bitburnerFiles1026/route.js b/bitburnerFiles1026/route.js new file mode 100644 index 0000000..97d814a --- /dev/null +++ b/bitburnerFiles1026/route.js @@ -0,0 +1,57 @@ +const HOME = "home"; +let target; +export async function main(ns) { + let result = []; + let route = []; + let seen = []; + target = ns.args[0]; // The target server + let backdoor = ns.args[1]; // True for connecting to the server and backdooring + if (target === undefined) { + ns.tprint("No target server specified"); + ns.exit(); + } + if (buildRoute(ns, HOME, route, seen)) { + result = await printRoute(ns, route); + } +} +function buildRoute(ns, parent, route, seen) { + //first time we run we need to add the parent to the list of servers we've seen + if (!seen.includes(parent)) + seen.push(parent); + //add to route + route.push(parent); + const children = ns.scan(parent); + for (const child of children) { + if (seen.includes(child)) { + //already checked + continue; + } + seen.push(child); + if (child == target) { + //found add it to the route and finish recursion + route.push(child); + return true; + } + if (buildRoute(ns, child, route, seen)) { + //target found, finish recursion + return true; + } + else { + //target not found in this branch, remove from route + route.pop(); + } + } + //didn't find target in this route, reset route + route = []; + return false; +} +async function printRoute(ns, route) { + let result = []; + for (const node of route) { + result.push(node); + } + result.push('backdoor'); + // await navigator.clipboard.writeText(result); + ns.tprint(result); + return result; +} \ No newline at end of file -- cgit v1.2.3