-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsolve-contracts.js
504 lines (422 loc) · 14.9 KB
/
solve-contracts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import { getAllServers } from "getServers.js";
export async function main(ns) {
ns.disableLog("ALL");
// This script could run separately in a loop, howeverit is more RAM-efficient to call the script from a management script
//while (true) {
// get all available servers
const servers = getAllServers(ns)
const contracts = servers.flatMap((server) => {
const onServer = ns.ls(server, ".cct").map((contract) => {
const type = ns.codingcontract.getContractType(contract, server);
const data = ns.codingcontract.getData(contract, server);
const didSolve = solve(type, data, server, contract, ns);
return `${server} - ${contract} - ${type} - ${didSolve || "FAILED!"}`;
});
return onServer;
});
//ns.tprint("Found " + contracts.length + " contracts");
contracts.forEach((contract) => void ns.print(contract));
// sleep in case this script is run manually
//await ns.sleep(60000)
//}
return;
}
function solve(type, data, server, contract, ns) {
let solution = "";
//ns.tprint(type);
switch (type) {
case "Algorithmic Stock Trader I":
solution = maxProfit([1, data]);
break;
case "Algorithmic Stock Trader II":
solution = maxProfit([Math.ceil(data.length / 2), data]);
break;
case "Algorithmic Stock Trader III":
solution = maxProfit([2, data]);
break;
case "Algorithmic Stock Trader IV":
solution = maxProfit(data);
break;
case "Minimum Path Sum in a Triangle":
solution = solveTriangleSum(data, ns);
break;
case "Unique Paths in a Grid I":
solution = uniquePathsI(data);
break;
case "Unique Paths in a Grid II":
solution = uniquePathsII(data);
break;
case "Generate IP Addresses":
solution = generateIps(data);
break;
case "Find Largest Prime Factor":
solution = factor(data);
break;
case "Spiralize Matrix":
solution = spiral(data);
break;
case "Merge Overlapping Intervals":
solution = mergeOverlap(data);
break;
case "Array Jumping Game":
solution = solverArrayJumpingGame(data);
break;
case "Find All Valid Math Expressions":
//solution = await findAllValidMathExpressions(data, ns);
solution = solverWaysToExpress(data);
break;
case "Subarray with Maximum Sum":
solution = solverLargestSubset(data);
break;
case "Total Ways to Sum":
solution = solverWaysToSum(data);
break;
case "Sanitize Parentheses in Expression":
solution = removeInvalidParenthesis(data);
break;
default:
return false;
}
return (solution != "") ? ns.codingcontract.attempt(solution, contract, server, [true]) : "";
}
// Sanitize Parentheses in Expression
// method checks if character is parenthesis(open or closed)
function isParenthesis(c)
{
return ((c == '(') || (c == ')'));
}
// method returns true if string contains valid parenthesis
function isValidString(str)
{
let cnt = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == '(')
cnt++;
else if (str[i] == ')')
cnt--;
if (cnt < 0)
return false;
}
return (cnt == 0);
}
// method to remove invalid parenthesis
function removeInvalidParenthesis(str)
{
if (str.length==0)
return [];
// visit set to ignore already visited string
let visit = new Set();
// queue to maintain BFS
let q = [];
let temp;
let level = false;
let solutions = []
// pushing given string as starting node into queue
q.push(str);
visit.add(str);
while (q.length!=0)
{
str = q.shift();
if (isValidString(str))
{
solutions.push(str);
// If answer is found, make level true
// so that valid string of only that level
// are processed.
level = true;
}
if (level)
continue;
for (let i = 0; i < str.length; i++)
{
if (!isParenthesis(str[i]))
continue;
// Removing parenthesis from str and
// pushing into queue,if not visited already
temp = str.substring(0, i) + str.substring(i + 1);
if (!visit.has(temp))
{
q.push(temp);
visit.add(temp);
}
}
}
if (solutions.length == 0){
solutions.push("");
}
return solutions;
}
// Total Ways to Sum
function solverWaysToSum(arrayData){
var ways = [];
ways[0] = 1;
for (var a = 1; a <= arrayData; a++) {
ways[a] = 0;
}
for (var i = 1; i <= arrayData - 1; i++) {
for (var j = i; j <= arrayData; j++) {
ways[j] += ways[j - i];
}
}
return ways[arrayData];
}
// Subarray with Maximum Sum
function solverLargestSubset(arrayData) {
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;
}
// Find All Valid Math Expressions
function solverWaysToExpress(arrayData) {
//ns.tprint("solverWaysToExpress()");
//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);
}
// Array Jumping Game
function solverArrayJumpingGame(arrayData) {
let arrayJump = [0];
for (let n = 0; n < arrayData.length; n++) {
if (arrayJump[n] || !n) {
for (let p = n; p <= Math.min(n + arrayData[n], arrayData.length - 1); p++) {
arrayJump[p] = 1;
}
}
}
//tprint("Array Jumping Game: " + 0 + Boolean(arrayJump[arrayData.length - 1]));
return 0 + Boolean(arrayJump[arrayData.length - 1]);
}
//ALGORITHMIC STOCK TRADER
function maxProfit(arrayData) {
let i, j, k;
let maxTrades = arrayData[0];
let stockPrices = arrayData[1];
// WHY?
let tempStr = "[0";
for (i = 0; i < stockPrices.length; i++) {
tempStr += ",0";
}
tempStr += "]";
let tempArr = "[" + tempStr;
for (i = 0; i < maxTrades - 1; i++) {
tempArr += "," + tempStr;
}
tempArr += "]";
let highestProfit = JSON.parse(tempArr);
for (i = 0; i < maxTrades; i++) {
for (j = 0; j < stockPrices.length; j++) { // Buy / Start
for (k = j; k < stockPrices.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] + stockPrices[k] - stockPrices[j]);
} else if (i > 0 && j > 0) {
highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i - 1][k], highestProfit[i - 1][j - 1] + stockPrices[k] - stockPrices[j]);
} else if (i > 0 && k > 0) {
highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i - 1][k], highestProfit[i][k - 1], stockPrices[k] - stockPrices[j]);
} else if (j > 0 && k > 0) {
highestProfit[i][k] = Math.max(highestProfit[i][k], highestProfit[i][k - 1], stockPrices[k] - stockPrices[j]);
} else {
highestProfit[i][k] = Math.max(highestProfit[i][k], stockPrices[k] - stockPrices[j]);
}
}
}
}
return highestProfit[maxTrades - 1][stockPrices.length - 1];
}
//SMALLEST TRIANGLE SUM
function solveTriangleSum(arrayData, ns) {
let triangle = arrayData;
let nextArray;
let previousArray = triangle[0];
for (let i = 1; i < triangle.length; i++) {
nextArray = [];
for (let j = 0; j < triangle[i].length; j++) {
if (j == 0) {
nextArray.push(previousArray[j] + triangle[i][j]);
} else if (j == triangle[i].length - 1) {
nextArray.push(previousArray[j - 1] + triangle[i][j]);
} else {
nextArray.push(Math.min(previousArray[j], previousArray[j - 1]) + triangle[i][j]);
}
}
previousArray = nextArray;
}
return Math.min.apply(null, nextArray);
}
//UNIQUE PATHS IN A GRID
function uniquePathsI(grid) {
const rightMoves = grid[0] - 1;
const downMoves = grid[1] - 1;
return Math.round(factorialDivision(rightMoves + downMoves, rightMoves) / (factorial(downMoves)));
}
function factorial(n) {
return factorialDivision(n, 1);
}
function factorialDivision(n, d) {
if (n == 0 || n == 1 || n == d)
return 1;
return factorialDivision(n - 1, d) * n;
}
function uniquePathsII(grid, ignoreFirst = false, ignoreLast = false) {
const rightMoves = grid[0].length - 1;
const downMoves = grid.length - 1;
let totalPossiblePaths = Math.round(factorialDivision(rightMoves + downMoves, rightMoves) / (factorial(downMoves)));
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1 && (!ignoreFirst || (i != 0 || j != 0)) && (!ignoreLast || (i != grid.length - 1 || j != grid[i].length - 1))) {
const newArray = [];
for (let k = i; k < grid.length; k++) {
newArray.push(grid[k].slice(j, grid[i].length));
}
let removedPaths = uniquePathsII(newArray, true, ignoreLast);
removedPaths *= uniquePathsI([i + 1, j + 1]);
totalPossiblePaths -= removedPaths;
}
}
}
return totalPossiblePaths;
}
//GENERATE IP ADDRESSES
function generateIps(num) {
num = num.toString();
const length = num.length;
const ips = [];
for (let i = 1; i < length - 2; i++) {
for (let j = i + 1; j < length - 1; j++) {
for (let k = j + 1; k < length; k++) {
const ip = [
num.slice(0, i),
num.slice(i, j),
num.slice(j, k),
num.slice(k, num.length)
];
let isValid = true;
ip.forEach(seg => {
isValid = isValid && isValidIpSegment(seg);
});
if (isValid) ips.push(ip.join("."));
}
}
}
return ips;
}
function isValidIpSegment(segment) {
if (segment[0] == "0" && segment != "0") return false;
segment = Number(segment);
if (segment < 0 || segment > 255) return false;
return true;
}
//GREATEST FACTOR
function factor(num) {
for (let div = 2; div <= Math.sqrt(num); div++) {
if (num % div != 0) {
continue;
}
num = num / div;
div = 2;
}
return num;
}
//SPIRALIZE Matrix
function spiral(arr, accum = []) {
if (arr.length === 0 || arr[0].length === 0) {
return accum;
}
accum = accum.concat(arr.shift());
if (arr.length === 0 || arr[0].length === 0) {
return accum;
}
accum = accum.concat(column(arr, arr[0].length - 1));
if (arr.length === 0 || arr[0].length === 0) {
return accum;
}
accum = accum.concat(arr.pop().reverse());
if (arr.length === 0 || arr[0].length === 0) {
return accum;
}
accum = accum.concat(column(arr, 0).reverse());
if (arr.length === 0 || arr[0].length === 0) {
return accum;
}
return spiral(arr, accum);
}
function column(arr, index) {
const res = [];
for (let i = 0; i < arr.length; i++) {
const elm = arr[i].splice(index, 1)[0];
if (elm) {
res.push(elm);
}
}
return res;
}
// Merge Overlapping Intervals
function mergeOverlap(intervals) {
intervals.sort(([minA], [minB]) => minA - minB);
for (let i = 0; i < intervals.length; i++) {
for (let j = i + 1; j < intervals.length; j++) {
const [min, max] = intervals[i];
const [laterMin, laterMax] = intervals[j];
if (laterMin <= max) {
const newMax = laterMax > max ? laterMax : max;
const newInterval = [min, newMax];
intervals[i] = newInterval;
intervals.splice(j, 1);
j = i;
}
}
}
return intervals;
}