init commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
export function caesarEncrypt(text, shift) {
|
||||
text = text.split("");
|
||||
var alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
|
||||
var output = "";
|
||||
|
||||
text.forEach(function(c) {
|
||||
if(c == " ") {
|
||||
output += " ";
|
||||
} else {
|
||||
var letter = alphabet.indexOf(c) - shift;
|
||||
if(letter < 0) {
|
||||
letter = alphabet.length + letter;
|
||||
}
|
||||
output += alphabet[letter];
|
||||
}
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
var text = ns.args[0];
|
||||
var shift = ns.args[1];
|
||||
|
||||
ns.writePort(1, encrypt(text, shift));
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct.
|
||||
|
||||
Given the following string containing only digits, return an array with all possible valid IP address combinations that can be created from the string:
|
||||
|
||||
Note that an octet cannot begin with a '0' unless the number itself is exactly '0'. For example, '192.168.010.1' is not a valid IP.
|
||||
|
||||
Examples:
|
||||
|
||||
25525511135 -> ["255.255.11.135", "255.255.111.35"]
|
||||
1938718066 -> ["193.87.180.66"]
|
||||
*/
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidIP(ip) {
|
||||
var octets = ip.split(".");
|
||||
if(octets.length != 4) return false;
|
||||
|
||||
for(var i = 0; i < octets.length; i++) {
|
||||
var length = octets[i].length;
|
||||
var value = parseInt(octets[i]);
|
||||
if(length == 0 || length > 3 || octets[i].charAt(0) == '0' || value > 255 || value < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidOctet(octet) {
|
||||
if(octet.length <= 0 || octet.length > 3) return false;
|
||||
|
||||
var octetValue = parseInt(octet);
|
||||
if(octetValue > 255 || octetValue < 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function generateOctet(ips, octets, input, sliceRange) {
|
||||
var octets1 = [...octets, input.slice(0, sliceRange)];
|
||||
if(isValidOctet(octets1[octets1.length - 1])) {
|
||||
generateIPAddress(ips, octets1, input.slice(sliceRange, input.length));
|
||||
}
|
||||
}
|
||||
|
||||
function generateIPAddress(ips, octets, input) {
|
||||
if(input.length == 0) {
|
||||
var ip = octets.join(".");
|
||||
if(isValidIP(ip)) {
|
||||
if(ips.indexOf(ip) == -1) {
|
||||
ips.push(ip);
|
||||
return ips;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
generateOctet(ips, octets, input, 3);
|
||||
generateOctet(ips, octets, input, 2);
|
||||
generateOctet(ips, octets, input, 1);
|
||||
}
|
||||
|
||||
return ips;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function test(ns, ips, expected) {
|
||||
if(ips.length != expected.length) {
|
||||
ns.tprint("test: generated and expected lengths differ!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for(var i = 0; i < ips.length; i++) {
|
||||
if(ips[i] != expected[i]) {
|
||||
ns.tprint(ips[i] + " != " + expected[i]);
|
||||
ns.tprint("test: values differ!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function testClient(ns) {
|
||||
var testInput1 = "25525511135";
|
||||
var testExpected1 = ["255.255.11.135", "255.255.111.35"];
|
||||
var testInput2 = "1938718066";
|
||||
var testExpected2 = ["193.87.180.66"];
|
||||
var testResult1 = generateIPAddress(ns, [], [], testInput1);
|
||||
var testResult2 = generateIPAddress(ns, [], [], testInput2);
|
||||
|
||||
ns.tprint("Result 1: " + testResult1);
|
||||
ns.tprint("Result 2: " + testResult2);
|
||||
|
||||
ns.tprint(test(ns, testResult1.reverse(), testExpected1));
|
||||
ns.tprint(test(ns, testResult2.reverse(), testExpected2));
|
||||
}
|
||||
|
||||
export function generateIPAddresses(input) {
|
||||
return generateIPAddress([], [], input.toString())
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
if(ns.args.length > 0) {
|
||||
var arg = ns.args[0];
|
||||
if(arg == "--test" || arg == "-t") {
|
||||
testClient(ns);
|
||||
} else if(ns.args.length == 2 && (arg == "--print" || arg == "-p")) {
|
||||
var input = ns.args[1].toString();
|
||||
ns.tprint(generateIPAddress([], [], input));
|
||||
} else {
|
||||
var input = arg.toString();
|
||||
return generateIPAddress([], [], input)
|
||||
}
|
||||
} else {
|
||||
ns.tprint("Usage error: Require an input to generate IP addresses or the --test|-t flag.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk begins with a length L, encoded as a single ASCII digit from 1 to 9, followed by the chunk data, which is either:
|
||||
|
||||
1. Exactly L characters, which are to be copied directly into the uncompressed data.
|
||||
2. A reference to an earlier part of the uncompressed data. To do this, the length is followed by a second ASCII digit X: each of the L output characters is a copy of the character X places before it in the uncompressed data.
|
||||
|
||||
For both chunk types, a length of 0 instead means the chunk ends immediately, and the next character is the start of a new chunk. The two chunk types alternate, starting with type 1, and the final chunk may be of either type.
|
||||
|
||||
You are given the following input string:
|
||||
gC1IS2Jj1IS2J2d6yS2JJJJyS2JJ2JJ2JJ2kfMDKgmikfMDKgkfMD0roQqsCroQQQQQhmMi6Gi
|
||||
Encode it using Lempel-Ziv encoding with the minimum possible output length.
|
||||
|
||||
Examples (some have other possible encodings of minimal length):
|
||||
abracadabra -> 7abracad47
|
||||
mississippi -> 4miss433ppi
|
||||
aAAaAAaAaAA -> 3aAA53035
|
||||
2718281828 -> 627182844
|
||||
abcdefghijk -> 9abcdefghi02jk
|
||||
aaaaaaaaaaaa -> 3aaa91
|
||||
aaaaaaaaaaaaa -> 1a91031
|
||||
aaaaaaaaaaaaaa -> 1a91041
|
||||
*/
|
||||
|
||||
export function compress(input) {
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
/*
|
||||
var input = ns.args[0];
|
||||
if(!input) {
|
||||
ns.tprint("Enter a string to compress!");
|
||||
return;
|
||||
}
|
||||
*/
|
||||
var input = "abracadabra";
|
||||
|
||||
var compressed_string = compress(input);
|
||||
ns.tprint("Compressed: " + compressed_string);
|
||||
if(compressed_string == "7abracad4") {
|
||||
ns.tprint("Compression Successful!");
|
||||
} else {
|
||||
ns.tprint("Compression Failed!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk begins with a length L, encoded as a single ASCII digit from 1 to 9, followed by the chunk data, which is either:
|
||||
|
||||
1. Exactly L characters, which are to be copied directly into the uncompressed data.
|
||||
2. A reference to an earlier part of the uncompressed data. To do this, the length is followed by a second ASCII digit X: each of the L output characters is a copy of the character X places before it in the uncompressed data.
|
||||
|
||||
For both chunk types, a length of 0 instead means the chunk ends immediately, and the next character is the start of a new chunk. The two chunk types alternate, starting with type 1, and the final chunk may be of either type.
|
||||
|
||||
You are given the following LZ-encoded string:
|
||||
2wg127MSbUJRP473Ak5681h74844rfOrky995VcHUV511Q854FDL4
|
||||
Decode it and output the original string.
|
||||
|
||||
Example: decoding '5aaabb450723abb' chunk-by-chunk
|
||||
|
||||
5aaabb -> aaabb
|
||||
5aaabb45 -> aaabbaaab
|
||||
5aaabb450 -> aaabbaaab
|
||||
5aaabb45072 -> aaabbaaababababa
|
||||
5aaabb450723abb -> aaabbaaababababaabb
|
||||
*/
|
||||
|
||||
/** @param {NS} ns */
|
||||
export function lzDecompress(ns, lz) {
|
||||
var output = "";
|
||||
var type = 0;
|
||||
for(var i = 0; i < lz.length;) {
|
||||
var length = Number.parseInt(lz[i]);
|
||||
|
||||
if(length == 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(length == 0) {
|
||||
ns.tprint("didn't continue!");
|
||||
}
|
||||
|
||||
i++;
|
||||
if(!isNaN(parseInt(lz[i]) && i != 1) && type == 1) { // Type 2 Chunk
|
||||
var v = Number.parseInt(lz[i]);
|
||||
var j = output.length - v;
|
||||
|
||||
for(var counter = 0; counter < length; counter++) {
|
||||
output += output[j];
|
||||
j = output.length - v;
|
||||
}
|
||||
i++;
|
||||
type = 0;
|
||||
} else { // Type 1 Chunk
|
||||
var j = i;
|
||||
for(; j < i + length; j++) {
|
||||
output += lz[j];
|
||||
}
|
||||
i = j;
|
||||
type = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export async function main(ns) {
|
||||
var lz = ns.args[0];
|
||||
ns.tprint(lzDecompress(ns, lz));
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Shortest Path in a Grid
|
||||
You are located in the top-left corner of the following grid:
|
||||
|
||||
[[0,0,0,0,0,0,1,0,0],
|
||||
[0,0,0,0,1,0,1,0,0],
|
||||
[0,0,1,0,0,0,0,0,1],
|
||||
[0,0,0,1,1,0,0,1,0],
|
||||
[0,0,0,0,0,0,0,0,1],
|
||||
[0,0,0,1,0,0,0,0,1],
|
||||
[1,1,0,0,0,1,0,0,0]]
|
||||
|
||||
You are trying to find the shortest path to the bottom-right corner of the grid, but there are obstacles on the grid that you cannot move onto. These obstacles are denoted by '1', while empty spaces are denoted by 0.
|
||||
|
||||
Determine the shortest path from start to finish, if one exists. The answer should be given as a string of UDLR characters, indicating the moves along the path
|
||||
|
||||
NOTE: If there are multiple equally short paths, any of them is accepted as answer. If there is no path, the answer should be an empty string.
|
||||
NOTE: The data returned for this contract is an 2D array of numbers representing the grid.
|
||||
|
||||
Examples:
|
||||
|
||||
[[0,1,0,0,0],
|
||||
[0,0,0,1,0]]
|
||||
|
||||
Answer: 'DRRURRD'
|
||||
|
||||
[[0,1],
|
||||
[1,0]]
|
||||
*/
|
||||
let grid;
|
||||
let dest;
|
||||
let solutions;
|
||||
|
||||
function isValidPosition(x, y) {
|
||||
return grid[x][y] == 0;
|
||||
}
|
||||
|
||||
function solve(ns, solution, x, y) {
|
||||
if([x, y] == dest) {
|
||||
solutions.push(solution);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
grid = [
|
||||
[0, 1, 0, 0, 0],
|
||||
[0, 0, 0, 1, 0]
|
||||
];
|
||||
dest = [grid.length - 1, grid[grid.length - 1].length - 1];
|
||||
|
||||
solve(ns, "", 0, 0);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export function caesarEncrypt(text, shift) {
|
||||
text = text.split("");
|
||||
var alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
|
||||
var output = "";
|
||||
|
||||
text.forEach(function(c) {
|
||||
if(c == " ") {
|
||||
output += " ";
|
||||
} else {
|
||||
var letter = alphabet.indexOf(c) - shift;
|
||||
if(letter < 0) {
|
||||
letter = alphabet.length + letter;
|
||||
}
|
||||
output += alphabet[letter];
|
||||
}
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
var text = ns.args[0];
|
||||
var shift = ns.args[1];
|
||||
|
||||
ns.writePort(1, encrypt(text, shift));
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct.
|
||||
|
||||
Given the following string containing only digits, return an array with all possible valid IP address combinations that can be created from the string:
|
||||
|
||||
Note that an octet cannot begin with a '0' unless the number itself is exactly '0'. For example, '192.168.010.1' is not a valid IP.
|
||||
|
||||
Examples:
|
||||
|
||||
25525511135 -> ["255.255.11.135", "255.255.111.35"]
|
||||
1938718066 -> ["193.87.180.66"]
|
||||
*/
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidIP(ip) {
|
||||
var octets = ip.split(".");
|
||||
if(octets.length != 4) return false;
|
||||
|
||||
for(var i = 0; i < octets.length; i++) {
|
||||
var length = octets[i].length;
|
||||
var value = parseInt(octets[i]);
|
||||
if(length == 0 || length > 3 || octets[i].charAt(0) == '0' || value > 255 || value < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidOctet(octet) {
|
||||
if(octet.length <= 0 || octet.length > 3) return false;
|
||||
|
||||
var octetValue = parseInt(octet);
|
||||
if(octetValue > 255 || octetValue < 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function generateOctet(ips, octets, input, sliceRange) {
|
||||
var octets1 = [...octets, input.slice(0, sliceRange)];
|
||||
if(isValidOctet(octets1[octets1.length - 1])) {
|
||||
generateIPAddress(ips, octets1, input.slice(sliceRange, input.length));
|
||||
}
|
||||
}
|
||||
|
||||
function generateIPAddress(ips, octets, input) {
|
||||
if(input.length == 0) {
|
||||
var ip = octets.join(".");
|
||||
if(isValidIP(ip)) {
|
||||
if(ips.indexOf(ip) == -1) {
|
||||
ips.push(ip);
|
||||
return ips;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
generateOctet(ips, octets, input, 3);
|
||||
generateOctet(ips, octets, input, 2);
|
||||
generateOctet(ips, octets, input, 1);
|
||||
}
|
||||
|
||||
return ips;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function test(ns, ips, expected) {
|
||||
if(ips.length != expected.length) {
|
||||
ns.tprint("test: generated and expected lengths differ!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for(var i = 0; i < ips.length; i++) {
|
||||
if(ips[i] != expected[i]) {
|
||||
ns.tprint(ips[i] + " != " + expected[i]);
|
||||
ns.tprint("test: values differ!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function testClient(ns) {
|
||||
var testInput1 = "25525511135";
|
||||
var testExpected1 = ["255.255.11.135", "255.255.111.35"];
|
||||
var testInput2 = "1938718066";
|
||||
var testExpected2 = ["193.87.180.66"];
|
||||
var testResult1 = generateIPAddress(ns, [], [], testInput1);
|
||||
var testResult2 = generateIPAddress(ns, [], [], testInput2);
|
||||
|
||||
ns.tprint("Result 1: " + testResult1);
|
||||
ns.tprint("Result 2: " + testResult2);
|
||||
|
||||
ns.tprint(test(ns, testResult1.reverse(), testExpected1));
|
||||
ns.tprint(test(ns, testResult2.reverse(), testExpected2));
|
||||
}
|
||||
|
||||
export function generateIPAddresses(input) {
|
||||
return generateIPAddress([], [], input.toString())
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
if(ns.args.length > 0) {
|
||||
var arg = ns.args[0];
|
||||
if(arg == "--test" || arg == "-t") {
|
||||
testClient(ns);
|
||||
} else if(ns.args.length == 2 && (arg == "--print" || arg == "-p")) {
|
||||
var input = ns.args[1].toString();
|
||||
ns.tprint(generateIPAddress([], [], input));
|
||||
} else {
|
||||
var input = arg.toString();
|
||||
return generateIPAddress([], [], input)
|
||||
}
|
||||
} else {
|
||||
ns.tprint("Usage error: Require an input to generate IP addresses or the --test|-t flag.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk begins with a length L, encoded as a single ASCII digit from 1 to 9, followed by the chunk data, which is either:
|
||||
|
||||
1. Exactly L characters, which are to be copied directly into the uncompressed data.
|
||||
2. A reference to an earlier part of the uncompressed data. To do this, the length is followed by a second ASCII digit X: each of the L output characters is a copy of the character X places before it in the uncompressed data.
|
||||
|
||||
For both chunk types, a length of 0 instead means the chunk ends immediately, and the next character is the start of a new chunk. The two chunk types alternate, starting with type 1, and the final chunk may be of either type.
|
||||
|
||||
You are given the following input string:
|
||||
gC1IS2Jj1IS2J2d6yS2JJJJyS2JJ2JJ2JJ2kfMDKgmikfMDKgkfMD0roQqsCroQQQQQhmMi6Gi
|
||||
Encode it using Lempel-Ziv encoding with the minimum possible output length.
|
||||
|
||||
Examples (some have other possible encodings of minimal length):
|
||||
abracadabra -> 7abracad47
|
||||
mississippi -> 4miss433ppi
|
||||
aAAaAAaAaAA -> 3aAA53035
|
||||
2718281828 -> 627182844
|
||||
abcdefghijk -> 9abcdefghi02jk
|
||||
aaaaaaaaaaaa -> 3aaa91
|
||||
aaaaaaaaaaaaa -> 1a91031
|
||||
aaaaaaaaaaaaaa -> 1a91041
|
||||
*/
|
||||
|
||||
export function compress(input) {
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
/*
|
||||
var input = ns.args[0];
|
||||
if(!input) {
|
||||
ns.tprint("Enter a string to compress!");
|
||||
return;
|
||||
}
|
||||
*/
|
||||
var input = "abracadabra";
|
||||
|
||||
var compressed_string = compress(input);
|
||||
ns.tprint("Compressed: " + compressed_string);
|
||||
if(compressed_string == "7abracad4") {
|
||||
ns.tprint("Compression Successful!");
|
||||
} else {
|
||||
ns.tprint("Compression Failed!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk begins with a length L, encoded as a single ASCII digit from 1 to 9, followed by the chunk data, which is either:
|
||||
|
||||
1. Exactly L characters, which are to be copied directly into the uncompressed data.
|
||||
2. A reference to an earlier part of the uncompressed data. To do this, the length is followed by a second ASCII digit X: each of the L output characters is a copy of the character X places before it in the uncompressed data.
|
||||
|
||||
For both chunk types, a length of 0 instead means the chunk ends immediately, and the next character is the start of a new chunk. The two chunk types alternate, starting with type 1, and the final chunk may be of either type.
|
||||
|
||||
You are given the following LZ-encoded string:
|
||||
2wg127MSbUJRP473Ak5681h74844rfOrky995VcHUV511Q854FDL4
|
||||
Decode it and output the original string.
|
||||
|
||||
Example: decoding '5aaabb450723abb' chunk-by-chunk
|
||||
|
||||
5aaabb -> aaabb
|
||||
5aaabb45 -> aaabbaaab
|
||||
5aaabb450 -> aaabbaaab
|
||||
5aaabb45072 -> aaabbaaababababa
|
||||
5aaabb450723abb -> aaabbaaababababaabb
|
||||
*/
|
||||
|
||||
/** @param {NS} ns */
|
||||
export function lzDecompress(ns, lz) {
|
||||
var output = "";
|
||||
var type = 0;
|
||||
for(var i = 0; i < lz.length;) {
|
||||
var length = Number.parseInt(lz[i]);
|
||||
|
||||
if(length == 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(length == 0) {
|
||||
ns.tprint("didn't continue!");
|
||||
}
|
||||
|
||||
i++;
|
||||
if(!isNaN(parseInt(lz[i]) && i != 1) && type == 1) { // Type 2 Chunk
|
||||
var v = Number.parseInt(lz[i]);
|
||||
var j = output.length - v;
|
||||
|
||||
for(var counter = 0; counter < length; counter++) {
|
||||
output += output[j];
|
||||
j = output.length - v;
|
||||
}
|
||||
i++;
|
||||
type = 0;
|
||||
} else { // Type 1 Chunk
|
||||
var j = i;
|
||||
for(; j < i + length; j++) {
|
||||
output += lz[j];
|
||||
}
|
||||
i = j;
|
||||
type = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export async function main(ns) {
|
||||
var lz = ns.args[0];
|
||||
ns.tprint(lzDecompress(ns, lz));
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Shortest Path in a Grid
|
||||
You are located in the top-left corner of the following grid:
|
||||
|
||||
[[0,0,0,0,0,0,1,0,0],
|
||||
[0,0,0,0,1,0,1,0,0],
|
||||
[0,0,1,0,0,0,0,0,1],
|
||||
[0,0,0,1,1,0,0,1,0],
|
||||
[0,0,0,0,0,0,0,0,1],
|
||||
[0,0,0,1,0,0,0,0,1],
|
||||
[1,1,0,0,0,1,0,0,0]]
|
||||
|
||||
You are trying to find the shortest path to the bottom-right corner of the grid, but there are obstacles on the grid that you cannot move onto. These obstacles are denoted by '1', while empty spaces are denoted by 0.
|
||||
|
||||
Determine the shortest path from start to finish, if one exists. The answer should be given as a string of UDLR characters, indicating the moves along the path
|
||||
|
||||
NOTE: If there are multiple equally short paths, any of them is accepted as answer. If there is no path, the answer should be an empty string.
|
||||
NOTE: The data returned for this contract is an 2D array of numbers representing the grid.
|
||||
|
||||
Examples:
|
||||
|
||||
[[0,1,0,0,0],
|
||||
[0,0,0,1,0]]
|
||||
|
||||
Answer: 'DRRURRD'
|
||||
|
||||
[[0,1],
|
||||
[1,0]]
|
||||
*/
|
||||
let grid;
|
||||
let dest;
|
||||
let solutions;
|
||||
|
||||
function isValidPosition(x, y) {
|
||||
return grid[x][y] == 0;
|
||||
}
|
||||
|
||||
function solve(ns, solution, x, y) {
|
||||
if([x, y] == dest) {
|
||||
solutions.push(solution);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
grid = [
|
||||
[0, 1, 0, 0, 0],
|
||||
[0, 0, 0, 1, 0]
|
||||
];
|
||||
dest = [grid.length - 1, grid[grid.length - 1].length - 1];
|
||||
|
||||
solve(ns, "", 0, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user