Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function HammingEncode(ns, value) {
- await ns.sleep(1000);
- // encoding following Hammings rule
- function HammingSumOfParity(_lengthOfDBits) {
- // will calculate the needed amount of parityBits 'without' the "overall"-Parity (that math took me 4 Days to get it working)
- return _lengthOfDBits < 3 || _lengthOfDBits == 0 // oh and of course using ternary operators, it's a pretty neat function
- ? _lengthOfDBits == 0
- ? 0
- : _lengthOfDBits + 1
- : // the following math will only work, if the length is greater equal 3, otherwise it's "kind of" broken :D
- Math.ceil(Math.log2(_lengthOfDBits * 2)) <=
- Math.ceil(Math.log2(1 + _lengthOfDBits + Math.ceil(Math.log2(_lengthOfDBits))))
- ? Math.ceil(Math.log2(_lengthOfDBits) + 1)
- : Math.ceil(Math.log2(_lengthOfDBits));
- }
- const _data = value.toString(2).split(""); // first, change into binary string, then create array with 1 bit per index
- const _sumParity = HammingSumOfParity(_data.length); // get the sum of needed parity bits (for later use in encoding)
- const count = (arr, val) =>
- arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
- // function count for specific entries in the array, for later use
- const _build = ["x", "x", ..._data.splice(0, 1)]; // init the "pre-build"
- for (let i = 2; i < _sumParity; i++) {
- // add new paritybits and the corresponding data bits (pre-building array)
- _build.push("x", ..._data.splice(0, Math.pow(2, i) - 1));
- }
- // now the "calculation"... get the paritybits ('x') working
- for (const index of _build.reduce(function (a, e, i) {
- if (e == "x") a.push(i);
- return a;
- }, [])) {
- // that reduce will result in an array of index numbers where the "x" is placed
- const _tempcount = index + 1; // set the "stepsize" for the parityBit
- const _temparray = []; // temporary array to store the extracted bits
- const _tempdata = [..._build]; // only work with a copy of the _build
- while (_tempdata[index] !== undefined) {
- // as long as there are bits on the starting index, do "cut"
- const _temp = _tempdata.splice(index, _tempcount * 2); // cut stepsize*2 bits, then...
- _temparray.push(..._temp.splice(0, _tempcount)); // ... cut the result again and keep the first half
- }
- _temparray.splice(0, 1); // remove first bit, which is the parity one
- _build[index] = (count(_temparray, "1") % 2).toString(); // count with remainder of 2 and"toString" to store the parityBit
- } // parity done, now the "overall"-parity is set
- _build.unshift((count(_build, "1") % 2).toString()); // has to be done as last element
- return _build.join(""); // return the _build as string
- }
- async function HammingDecode(ns, _data) {
- await ns.sleep(1000);
- //check for altered bit and decode
- const _build = _data.split(""); // ye, an array for working, again
- const _testArray = []; //for the "truthtable". if any is false, the data has an altered bit, will check for and fix it
- const _sumParity = Math.ceil(Math.log2(_data.length)); // sum of parity for later use
- const count = (arr, val) =>
- arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
- // the count.... again ;)
- let _overallParity = _build.splice(0, 1).join(""); // store first index, for checking in next step and fix the _build properly later on
- _testArray.push(_overallParity == (count(_build, "1") % 2).toString() ? true : false); // first check with the overall parity bit
- for (let i = 0; i < _sumParity; i++) {
- // for the rest of the remaining parity bits we also "check"
- const _tempIndex = Math.pow(2, i) - 1; // get the parityBits Index
- const _tempStep = _tempIndex + 1; // set the stepsize
- const _tempData = [..._build]; // get a "copy" of the build-data for working
- const _tempArray = []; // init empty array for "testing"
- while (_tempData[_tempIndex] != undefined) {
- // extract from the copied data until the "starting" index is undefined
- const _temp = [..._tempData.splice(_tempIndex, _tempStep * 2)]; // extract 2*stepsize
- _tempArray.push(..._temp.splice(0, _tempStep)); // and cut again for keeping first half
- }
- const _tempParity = _tempArray.shift(); // and again save the first index separated for checking with the rest of the data
- _testArray.push(_tempParity == (count(_tempArray, "1") % 2).toString() ? true : false);
- // is the _tempParity the calculated data? push answer into the 'truthtable'
- }
- let _fixIndex = 0; // init the "fixing" index and start with 0
- for (let i = 1; i < _sumParity + 1; i++) {
- // simple binary adding for every boolean in the _testArray, starting from 2nd index of it
- _fixIndex += _testArray[i] ? 0 : Math.pow(2, i) / 2;
- }
- _build.unshift(_overallParity); // now we need the "overall" parity back in it's place
- // try fix the actual encoded binary string if there is an error
- if (_fixIndex > 0 && _testArray[0] == false) {
- // if the overall is false and the sum of calculated values is greater equal 0, fix the corresponding hamming-bit
- _build[_fixIndex] = _build[_fixIndex] == "0" ? "1" : "0";
- } else if (_testArray[0] == false) {
- // otherwise, if the the overall_parity is the only wrong, fix that one
- _overallParity = _overallParity == "0" ? "1" : "0";
- } else if (_testArray[0] == true && _testArray.some((truth) => truth == false)) {
- return 0; // uhm, there's some strange going on... 2 bits are altered? How? This should not happen 👀
- }
- // oof.. halfway through... we fixed an possible altered bit, now "extract" the parity-bits from the _build
- for (let i = _sumParity; i >= 0; i--) {
- // start from the last parity down the 2nd index one
- _build.splice(Math.pow(2, i), 1);
- }
- _build.splice(0, 1); // remove the overall parity bit and we have our binary value
- return parseInt(_build.join(""), 2); // parse the integer with redux 2 and we're done!
- }
- //n[][]
- /** @param {NS} ns */
- async function solverWaysToSumII(ns, arrayData) {
- await ns.sleep(1000);
- let n = arrayData[0];
- let s = arrayData[1];
- let ways = [1];
- ways.length = n + 1;
- ways.fill(0,1);
- for (let i = 0; i < s.length; i++) {
- for (let j = s[i]; j <= n; j++) {
- ways[j] += ways[j - s[i]];
- }
- }
- return ways[n];
- }
- async function sanitizeParentheses(ns, input) {
- await ns.sleep(1000);
- var solutions = new Set();
- // Returns true and adds to solutions set if a string contains valid parentheses, false otherwise
- var checkValidity = (str) => {
- var nestLevel = 0;
- for (var c of str) {
- if (c == "(") nestLevel++;
- else if (c == ")") nestLevel--;
- if (nestLevel < 0) return false;
- }
- if (nestLevel == 0) solutions.add(str);
- return nestLevel == 0;
- };
- // Does a breadth first search to check all nodes at the target depth
- var getNodesAtDepth = (str, targetDepth, curDepth = 0) => {
- if (curDepth == targetDepth)
- checkValidity(str);
- else
- for (var i = 0; i < str.length; i++)
- if (str[i] == "(" || str[i] == ")")
- getNodesAtDepth(str.slice(0, i) + str.slice(i + 1), targetDepth, curDepth + 1);
- }
- // Start from the top level and expand down until we find at least one solution
- var targetDepth = 0;
- while (solutions.size == 0 && targetDepth < input.length - 1) {
- getNodesAtDepth(input, targetDepth++);
- }
- // If no solutions were found, return [""]
- if (solutions.size == 0) solutions.add("");
- return `[${[...solutions].join(", ")}]`;
- }
- let matrix = [];
- let start;
- let end;
- async function solverShortestPath(ns, data){
- await ns.sleep(1000);
- matrix = [...data];
- start = [0, 0];
- end = [matrix.length -1, matrix[0].length -1];
- let path = await findWay(start, end);
- //await ns.sleep(1000);
- let sPath = "";
- if (path != undefined){
- for (var i=1; i < path.length; i++) {
- sPath += await getDirectionMoved(path[i-1], path[i]);
- }
- }
- return sPath;
- }
- async function getDirectionMoved(current, newCoord){
- let tCoord = [];
- tCoord[0] = current[0] - newCoord[0];
- tCoord[1] = current[1] - newCoord[1];
- if (tCoord[0] == -1) return "D";
- if (tCoord[0] == 1) return "U";
- if (tCoord[1] == -1) return "R";
- if (tCoord[1] == 1) return "L";
- }
- async function findWay(position, end){
- var queue = [];
- matrix[position[0]][position[1]] = 1;
- queue.push([position]); // store a path, not just a position
- while (queue.length > 0) {
- var path = queue.shift(); // get the path out of the queue
- var pos = path[path.length-1]; // ... and then the last position from it
- var direction = [
- [pos[0] + 1, pos[1]],
- [pos[0], pos[1] + 1],
- [pos[0] - 1, pos[1]],
- [pos[0], pos[1] - 1]
- ];
- for (var i = 0; i < direction.length; i++) {
- // Perform this check first:
- if (direction[i][0] == end[0] && direction[i][1] == end[1]) {
- // return the path that led to the find
- return path.concat([end]);
- }
- if (direction[i][0] < 0 || direction[i][0] >= matrix.length
- || direction[i][1] < 0 || direction[i][1] >= matrix[0].length
- || matrix[direction[i][0]][direction[i][1]] != 0) {
- continue;
- }
- matrix[direction[i][0]][direction[i][1]] = 1;
- // extend and push the path on the queue
- queue.push(path.concat([direction[i]]));
- }
- }
- }
- // autocontract.ns v20190515 by /u/hyperpandiculation
- async function solverArrayJumpingGame(ns, arrayData) {
- await ns.sleep(1000);
- let arrayJump = [1];
- for (let n = 0; n < arrayData.length; n++) {
- if (arrayJump[n]) {
- for (let p = n; p <= Math.min(n + arrayData[n], arrayData.length-1); p++) { // fixed off-by-one error
- arrayJump[p] = 1;
- }
- }
- }
- return 0 + Boolean(arrayJump[arrayData.length - 1]); // thanks /u/Kalumniatoris
- }
- async function solverArrayJumpingGameII(ns, arrayData){
- await ns.sleep(1000);
- let n = arrayData.length;
- let reach = 0;
- let jumps = 0;
- let lastJump = -1;
- while (reach < n - 1) {
- let jumpedFrom = -1;
- for (let i = reach; i > lastJump; i--) {
- if (i + arrayData[i] > reach) {
- reach = i + arrayData[i];
- jumpedFrom = i;
- }
- }
- if (jumpedFrom === -1) {
- jumps = 0;
- break;
- }
- lastJump = jumpedFrom;
- jumps++;
- }
- return jumps
- }
- async function solverGenerateIPs(ns, arrayData) {
- await ns.sleep(1000);
- let i, j, k, l;
- let arrayDigits = [];
- let arrayDelim = [];
- for (i = 0; i < arrayData.length; i++) {
- arrayDigits[i] = arrayData.substring(i, i + 1);
- arrayDelim[i] = "";
- }
- let validCandidates = [];
- for (i = 0; i < arrayData.length - 3; i++) {
- for (j = i + 1; j < arrayData.length - 2; j++) {
- for (k = j + 1; k < arrayData.length - 1; k++) {
- let arrayDelimScratch = JSON.parse(JSON.stringify(arrayDelim));
- arrayDelimScratch[i] = ".";
- arrayDelimScratch[j] = ".";
- arrayDelimScratch[k] = ".";
- let candidateAddress = "";
- for (l = 0; l < arrayData.length; l++) {
- candidateAddress = candidateAddress + arrayDigits[l] + arrayDelimScratch[l];
- }
- let isValid = 1;
- for (l = 0; l < 4; l++) {
- let tempOctet = candidateAddress.split(".")[l];
- if (tempOctet.slice(0, 1) === "0") { isValid = 0; }
- if (parseInt(tempOctet) > 255) { isValid = 0; }
- }
- if (isValid) {
- validCandidates[validCandidates.length] = candidateAddress;
- }
- }
- }
- }
- let tempStr = JSON.stringify(validCandidates);
- return tempStr.replace(/\"/g, '');
- }
- async function solverLargestPrime(ns, arrayData) {
- await ns.sleep(1000);
- let primeFound = 0;
- while (!primeFound) {
- primeFound = 1;
- for (let i = 2; i < Math.sqrt(arrayData); i++) {
- if (!Boolean((arrayData / i) - Math.floor(arrayData / i))) {
- arrayData = arrayData / i;
- primeFound = 0;
- }
- }
- }
- return arrayData;
- }
- async function solverLargestSubset(ns, arrayData) {
- await ns.sleep(1000);
- let highestSubset = arrayData[0];
- for (let i = 0; i < arrayData.length; i++) {
- for (let j = i; j < arrayData.length; j++) {
- let tempSubset = 0;
- for (let k = i; k <= j; k++) {
- tempSubset += arrayData[k];
- }
- if (highestSubset < tempSubset) {
- highestSubset = tempSubset;
- }
- }
- }
- return highestSubset;
- }
- async function solverMergeRanges(ns, arrayData) {
- await ns.sleep(1000);
- let i, j, k;
- let rangeMax = 0;
- let rangeMin = 999;
- let outputRanges = [];
- for (i = 0; i < arrayData.length; i++) {
- rangeMin = Math.min(rangeMin, arrayData[i][0]);
- rangeMax = Math.max(rangeMax, arrayData[i][1]);
- }
- let activeRange = 0;
- let startRange, inRange;
- for (i = rangeMin; i <= rangeMax + 1; i++) {
- inRange = 0;
- for (j = 0; j < arrayData.length; j++) {
- if (i >= arrayData[j][0] && i < arrayData[j][1]) {
- inRange = 1;
- if (activeRange === 0) {
- activeRange = 1;
- startRange = i;
- }
- }
- }
- if (activeRange === 1 && inRange === 0) {
- activeRange = 0;
- outputRanges[outputRanges.length] = [startRange, i];
- }
- }
- return JSON.stringify(outputRanges);
- }
- async function solverSpiralizeMatrix(ns, arrayData) {
- await ns.sleep(1000);
- let i, j;
- let arrayY = arrayData.length;
- let arrayX = arrayData[0].length;
- let loopCount = Math.ceil(arrayX / 2) + 1;
- let marginData = [0, 1, 1, 0];
- let resultData = [];
- let lastWaypoint = [0, 0];
- resultData[0] = arrayData[0][0];
- for (i = 0; i < loopCount; i++) {
- if (marginData[0] + marginData[2] <= arrayY && marginData[1] + marginData[3] <= arrayX) {
- for (j = lastWaypoint[1] + 1; j <= arrayX - marginData[1]; j++) {
- resultData[resultData.length] = arrayData[lastWaypoint[0]][j];
- }
- lastWaypoint = [0 + marginData[0], arrayX - marginData[1]];
- marginData[0] += 1;
- }
- if (marginData[0] + marginData[2] <= arrayY && marginData[1] + marginData[3] <= arrayX) {
- for (j = lastWaypoint[0] + 1; j <= arrayY - marginData[2]; j++) {
- resultData[resultData.length] = arrayData[j][lastWaypoint[1]];
- }
- lastWaypoint = [arrayY - marginData[2], arrayX - marginData[1]];
- marginData[1] += 1;
- }
- if (marginData[0] + marginData[2] <= arrayY && marginData[1] + marginData[3] <= arrayX) {
- for (j = lastWaypoint[1] - 1; j >= 0 + marginData[3]; j--) {
- resultData[resultData.length] = arrayData[lastWaypoint[0]][j];
- }
- lastWaypoint = [arrayY - marginData[2], 0 + marginData[3]];
- marginData[2] += 1;
- }
- if (marginData[0] + marginData[2] <= arrayY && marginData[1] + marginData[3] <= arrayX) {
- for (j = lastWaypoint[0] - 1; j >= 0 + marginData[0]; j--) {
- resultData[resultData.length] = arrayData[j][lastWaypoint[1]];
- }
- lastWaypoint = [0 + marginData[0], 0 + marginData[3]];
- marginData[3] += 1;
- }
- }
- return JSON.stringify(resultData);
- }
- async function solverStockTrader(ns, arrayData) {
- await ns.sleep(1000);
- let i, j, k;
- let tempStr = "[0";
- for (i = 0; i < arrayData[1].length; i++) {
- tempStr += ",0";
- }
- tempStr += "]";
- let tempArr = "[" + tempStr;
- for (i = 0; i < arrayData[0] - 1; i++) {
- tempArr += "," + tempStr;
- }
- tempArr += "]";
- let highestProfit = JSON.parse(tempArr);
- for (i = 0; i < arrayData[0]; i++) {
- for (j = 0; j < arrayData[1].length; j++) { // Buy / Start
- for (k = j; k < arrayData[1].length; k++) { // Sell / End
- if (i > 0 && j > 0 && k > 0) {
- highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i - 1][k], highestProfit[i][k - 1], highestProfit[i - 1][j - 1] + arrayData[1][k] - arrayData[1][j]);
- } else if (i > 0 && j > 0) {
- highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i - 1][k], highestProfit[i - 1][j - 1] + arrayData[1][k] - arrayData[1][j]);
- } else if (i > 0 && k > 0) {
- highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i - 1][k], highestProfit[i][k - 1], arrayData[1][k] - arrayData[1][j]);
- } else if (j > 0 && k > 0) {
- highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i][k - 1], arrayData[1][k] - arrayData[1][j]);
- } else {
- highestProfit[i][k] = Math.max(highestProfit[i][k], arrayData[1][k] - arrayData[1][j]);
- }
- }
- }
- }
- return highestProfit[arrayData[0] - 1][arrayData[1].length - 1];
- }
- async function solverTrianglePath(ns, arrayData) {
- await ns.sleep(1000);
- let i, j;
- for (i = 1; i < arrayData.length; i++) {
- for (j = 0; j < arrayData[i].length; j++) {
- arrayData[i][j] += Math.min(arrayData[i - 1][Math.max(0, j - 1)], arrayData[i - 1][Math.min(j, arrayData[i - 1].length - 1)]);
- }
- }
- let finalRow = arrayData[arrayData.length - 1];
- let finalMinimum = 999;
- for (i = 0; i < finalRow.length; i++) {
- finalMinimum = Math.min(finalMinimum, finalRow[i]);
- }
- return finalMinimum;
- }
- async function solverUniquePaths(ns, arrayData) {
- await ns.sleep(1000);
- //let precalcFactorial = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000];
- //if (arrayData === undefined || arrayData === null) {
- // return 1;
- //}
- //let factN = precalcFactorial[arrayData[0] + arrayData[1] - 2];
- //let factK = precalcFactorial[arrayData[0] - 1];
- //let factNK = precalcFactorial[arrayData[1] - 1];
- //
- let k = arrayData[0] - 1; // k
- let ak = arrayData[1] - 1; // n-k
- let n = k + ak; // n = k + (n-k);
- // n choose k = n!/[(k)!(n-k)!] = n! / k! / (n-k)!
- let i;
- let factN = 1,
- factAK = 1;
- for (i = n; i > k; i--) { // n!/k! = n * n-1 * n-2 ... k+1
- factN = factN * i;
- }
- for (i = ak; i > 1; i--) {
- factAK = factAK * i;
- }
- return (factN / factAK);
- }
- async function solverUniquePathsII(ns, arrayData) {
- await ns.sleep(1000);
- let i, j, k;
- let pathsTo = [];
- for (i = 0; i < arrayData.length; i++) {
- pathsTo[i] = [];
- for (j = 0; j < arrayData[0].length; j++) {
- pathsTo[i][j] = 0;
- }
- }
- pathsTo[0][0] = 1;
- for (i = 0; i < arrayData.length; i++) {
- for (j = 0; j < arrayData[0].length; j++) {
- if (i > 0 && j > 0 && !arrayData[i][j]) {
- pathsTo[i][j] = pathsTo[i][j - 1] + pathsTo[i - 1][j];
- } else if (i > 0 && !arrayData[i][j]) {
- pathsTo[i][j] = pathsTo[i - 1][j];
- } else if (j > 0 && !arrayData[i][j]) {
- pathsTo[i][j] = pathsTo[i][j - 1];
- } else if (i === 0 && j === 0 && !arrayData[i][j]) {
- pathsTo[0][0] = 1;
- } else {
- pathsTo[i][j] = 0;
- }
- }
- }
- return pathsTo[pathsTo.length - 1][pathsTo[0].length - 1];
- }
- async function solverWaysToExpress(ns, arrayData) {
- await ns.sleep(1000);
- let i, j, k;
- let operatorList = ["", "+", "-", "*"];
- let validExpressions = [];
- let tempPermutations = Math.pow(4, (arrayData[0].length - 1));
- for (i = 0; i < tempPermutations; i++) {
- if (!Boolean(i % 100000)) {
- ns.tprint(i + "/" + tempPermutations + ", " + validExpressions.length + " found.");
- await ns.sleep(100);
- }
- let arraySummands = [];
- let candidateExpression = arrayData[0].substr(0, 1);
- arraySummands[0] = parseInt(arrayData[0].substr(0, 1));
- for (j = 1; j < arrayData[0].length; j++) {
- candidateExpression += operatorList[(i >> ((j - 1) * 2)) % 4] + arrayData[0].substr(j, 1);
- let rollingOperator = operatorList[(i >> ((j - 1) * 2)) % 4];
- let rollingOperand = parseInt(arrayData[0].substr(j, 1));
- switch (rollingOperator) {
- case "":
- rollingOperand = rollingOperand * (arraySummands[arraySummands.length - 1] / Math.abs(arraySummands[arraySummands.length - 1]));
- arraySummands[arraySummands.length - 1] = arraySummands[arraySummands.length - 1] * 10 + rollingOperand;
- break;
- case "+":
- arraySummands[arraySummands.length] = rollingOperand;
- break;
- case "-":
- arraySummands[arraySummands.length] = 0 - rollingOperand;
- break;
- case "*":
- while (j < arrayData[0].length - 1 && ((i >> (j * 2)) % 4) === 0) {
- j += 1;
- candidateExpression += arrayData[0].substr(j, 1);
- rollingOperand = rollingOperand * 10 + parseInt(arrayData[0].substr(j, 1));
- }
- arraySummands[arraySummands.length - 1] = arraySummands[arraySummands.length - 1] * rollingOperand;
- break;
- }
- }
- let rollingTotal = arraySummands.reduce(function(a, b) { return a + b; });
- //if(arrayData[1] == eval(candidateExpression)){
- if (arrayData[1] === rollingTotal) {
- validExpressions[validExpressions.length] = candidateExpression;
- }
- }
- return JSON.stringify(validExpressions);
- }
- async function solverWaysToSum(ns, arrayData) {
- await ns.sleep(1000);
- let precalcPartitions = [0, 0, 1, 2, 4, 6, 10, 14, 21, 29, 41, 55, 76, 100, 134, 175, 230, 296, 384, 489, 626, 791, 1001, 1254, 1574, 1957, 2435, 3009, 3717, 4564, 5603, 6841, 8348, 10142, 12309, 14882, 17976, 21636, 26014, 31184, 37337, 44582, 53173, 63260, 75174, 89133, 105557, 124753, 147272, 173524, 204225, 239942, 281588, 329930, 386154, 451275, 526822, 614153, 715219, 831819, 966466, 1121504, 1300155, 1505498, 1741629, 2012557, 2323519, 2679688, 3087734, 3554344, 4087967, 4697204, 5392782, 6185688, 7089499, 8118263, 9289090, 10619862, 12132163, 13848649, 15796475, 18004326, 20506254, 23338468, 26543659, 30167356, 34262961, 38887672, 44108108, 49995924, 56634172, 64112358, 72533806, 82010176, 92669719, 104651418, 118114303, 133230929, 150198135, 169229874, 190569291, 214481125, 241265378, 271248949, 304801364, 342325708, 384276335, 431149388, 483502843, 541946239, 607163745, 679903202, 761002155, 851376627, 952050664, 1064144450, 1188908247, 1327710075, 1482074142, 1653668664, 1844349559, 2056148050, 2291320911, 2552338240, 2841940499, 3163127351, 3519222691, 3913864294, 4351078599, 4835271869, 5371315399, 5964539503, 6620830888, 7346629511, 8149040694, 9035836075, 10015581679, 11097645015, 12292341830, 13610949894, 15065878134, 16670689207, 18440293319, 20390982756, 22540654444, 24908858008, 27517052598, 30388671977, 33549419496, 37027355199];
- return precalcPartitions[arrayData];
- }
- async function solve(inputData, outputData, listFile, listServer, inputType, ns){
- let outputResult = ns.codingcontract.attempt(outputData, listFile, listServer, { returnReward: true});
- ns.tprint([listServer,
- listFile,
- inputType,
- outputData,
- outputResult
- ]);
- if (outputResult == "") {
- ns.tprint("Failed Solving!!! Data for debug: " + JSON.stringify(inputData), outputData);
- }
- }
- /** @param {NS} ns */
- export async function main(ns) {
- let listServers = ["home"];
- let listIndex = 0;
- while (listIndex < listServers.length) {
- let listScan = ns.scan(listServers[listIndex], true);
- for (let i = 0; i < listScan.length; i++) {
- if (listServers.indexOf(listScan[i]) === -1) {
- listServers[listServers.length] = listScan[i];
- }
- }
- listIndex += 1;
- }
- ns.tprint("Completed server probe; now solving contracts");
- while (true) {
- await ns.sleep(1000);
- listIndex = (listIndex + 1) % listServers.length;
- let listFiles = ns.ls(listServers[listIndex], ".cct");
- for (let z = 0; z < listFiles.length; z++) {
- let inputData = ns.codingcontract.getData(listFiles[z], listServers[listIndex]);
- let inputType = ns.codingcontract.getContractType(listFiles[z], listServers[listIndex]);
- let outputData;
- let outputResult = null;
- switch (inputType) {
- case "Algorithmic Stock Trader I":
- if(inputData.length > 1){
- outputData = await solverStockTrader(ns, [1, inputData]);
- } else {
- outputData = 0;
- }
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Algorithmic Stock Trader II":
- if(inputData.length > 1){
- outputData = await solverStockTrader(ns, [Math.floor(inputData.length / 2), inputData]);
- } else {
- outputData = 0;
- }
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Algorithmic Stock Trader III":
- if(inputData.length > 1){
- outputData = await solverStockTrader(ns, [2, inputData]);
- } else {
- outputData = 0;
- }
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Algorithmic Stock Trader IV":
- outputData = await solverStockTrader(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Array Jumping Game":
- outputData = await solverArrayJumpingGame(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Array Jumping Game II":
- outputData = await solverArrayJumpingGameII(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Find All Valid Math Expressions":
- outputData = await solverWaysToExpress(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Find Largest Prime Factor":
- outputData = await solverLargestPrime(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Generate IP Addresses":
- outputData = await solverGenerateIPs(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Merge Overlapping Intervals":
- outputData = await solverMergeRanges(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Minimum Path Sum in a Triangle":
- outputData = await solverTrianglePath(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Spiralize Matrix":
- outputData = await solverSpiralizeMatrix(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Subarray with Maximum Sum":
- outputData = await solverLargestSubset(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Total Ways to Sum":
- outputData = await solverWaysToSum(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Total Ways to Sum II":
- outputData = await solverWaysToSumII(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Unique Paths in a Grid I":
- outputData = await solverUniquePaths(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Unique Paths in a Grid II":
- outputData = await solverUniquePathsII(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Sanitize Parentheses in Expression":
- outputData = await sanitizeParentheses(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "HammingCodes: Integer to encoded Binary":
- outputData = await HammingEncode(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "HammingCodes: Encoded Binary to Integer":
- outputData = await HammingDecode(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- case "Shortest Path in a Grid":
- outputData = await solverShortestPath(ns, inputData);
- await solve(inputData, outputData, listFiles[z], listServers[listIndex], inputType, ns);
- break;
- default:
- ns.tprint([listServers[listIndex],
- listFiles[z],
- inputType,
- "NO SOLVER YET"
- ]);
- break;
- }
- }
- }
- }
- ///Shortest Path in a Grid solverShortestPath
Advertisement
Add Comment
Please, Sign In to add comment