From 2b2af797a736526d7e98ae916865be60e8cbae09 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Fri, 8 Apr 2022 00:39:45 -0400 Subject: [PATCH] fmt and lint --- src/NetscriptFunctions/Singularity.ts | 1 - src/data/codingcontracttypes.ts | 73 ++++++++++++++------------- src/utils/Heap.ts | 35 +++++-------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index cc5f065c3..038052f4e 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -6,7 +6,6 @@ import { startWorkerScript } from "../NetscriptWorker"; import { Augmentation } from "../Augmentation/Augmentation"; import { Augmentations } from "../Augmentation/Augmentations"; import { augmentationExists, installAugmentations } from "../Augmentation/AugmentationHelpers"; -import { prestigeAugmentation } from "../Prestige"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; import { killWorkerScript } from "../Netscript/killWorkerScript"; import { CONSTANTS } from "../Constants"; diff --git a/src/data/codingcontracttypes.ts b/src/data/codingcontracttypes.ts index 7a587371a..bbdc17ba3 100644 --- a/src/data/codingcontracttypes.ts +++ b/src/data/codingcontracttypes.ts @@ -801,7 +801,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ desc: (data: number[][]): string => { return [ "You are located in the top-left corner of the following grid:\n\n", - `  [${data.map(line => "[" + line + "]").join(",\n   ")}]\n\n`, + `  [${data.map((line) => "[" + line + "]").join(",\n   ")}]\n\n`, "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.\n\n", @@ -831,20 +831,18 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ const minPathLength = dstY + dstX; // Math.abs(dstY - srcY) + Math.abs(dstX - srcX) const grid: number[][] = new Array(height); - for(let y = 0; y < height; y++) - grid[y] = new Array(width).fill(0); + for (let y = 0; y < height; y++) grid[y] = new Array(width).fill(0); - for(let y = 0; y < height; y++) { - for(let x = 0; x < width; x++) { - if(y == 0 && x == 0) continue; // Don't block start - if(y == dstY && x == dstX) continue; // Don't block destination + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + if (y == 0 && x == 0) continue; // Don't block start + if (y == dstY && x == dstX) continue; // Don't block destination // Generate more obstacles the farther a position is from start and destination. // Raw distance factor peaks at 50% at half-way mark. Rescale to 40% max. // Obstacle chance range of [15%, 40%] produces ~78% solvable puzzles - const distanceFactor = Math.min(y + x, dstY - y + dstX - x) / minPathLength * 0.8; - if (Math.random() < Math.max(0.15, distanceFactor)) - grid[y][x] = 1; + const distanceFactor = (Math.min(y + x, dstY - y + dstX - x) / minPathLength) * 0.8; + if (Math.random() < Math.max(0.15, distanceFactor)) grid[y][x] = 1; } } @@ -860,7 +858,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ //const prev: [[number, number] | undefined][] = new Array(height); const queue = new MinHeap<[number, number]>(); - for(let y = 0; y < height; y++) { + for (let y = 0; y < height; y++) { distance[y] = new Array(width).fill(Infinity) as [number]; //prev[y] = new Array(width).fill(undefined) as [undefined]; } @@ -871,10 +869,10 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ // List in-bounds and passable neighbors function* neighbors(y: number, x: number): Generator<[number, number]> { - if(validPosition(y - 1, x)) yield [y - 1, x]; // Up - if(validPosition(y + 1, x)) yield [y + 1, x]; // Down - if(validPosition(y, x - 1)) yield [y, x - 1]; // Left - if(validPosition(y, x + 1)) yield [y, x + 1]; // Right + if (validPosition(y - 1, x)) yield [y - 1, x]; // Up + if (validPosition(y + 1, x)) yield [y + 1, x]; // Down + if (validPosition(y, x - 1)) yield [y, x - 1]; // Left + if (validPosition(y, x + 1)) yield [y, x + 1]; // Right } // Prepare starting point @@ -882,15 +880,16 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ queue.push([0, 0], 0); // Take next-nearest position and expand potential paths from there - while(queue.size > 0) { + while (queue.size > 0) { const [y, x] = queue.pop() as [number, number]; - for(const [yN, xN] of neighbors(y, x)) { + for (const [yN, xN] of neighbors(y, x)) { const d = distance[y][x] + 1; - if(d < distance[yN][xN]) { - if(distance[yN][xN] == Infinity) // Not reached previously + if (d < distance[yN][xN]) { + if (distance[yN][xN] == Infinity) + // Not reached previously queue.push([yN, xN], d); - else // Found a shorter path - queue.changeWeight(([yQ, xQ]) => yQ == yN && xQ == xN, d); + // Found a shorter path + else queue.changeWeight(([yQ, xQ]) => yQ == yN && xQ == xN, d); //prev[yN][xN] = [y, x]; distance[yN][xN] = d; } @@ -898,27 +897,33 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ } // No path at all? - if(distance[dstY][dstX] == Infinity) - return ans == ""; + if (distance[dstY][dstX] == Infinity) return ans == ""; // There is a solution, require that the answer path is as short as the shortest // path we found - if(ans.length > distance[dstY][dstX]) - return false; + if (ans.length > distance[dstY][dstX]) return false; // Further verify that the answer path is a valid path let ansX = 0; let ansY = 0; - for(const direction of ans) { - switch(direction) { - case "U": ansY -= 1; break; - case "D": ansY += 1; break; - case "L": ansX -= 1; break; - case "R": ansX += 1; break; - default: return false; // Invalid character + for (const direction of ans) { + switch (direction) { + case "U": + ansY -= 1; + break; + case "D": + ansY += 1; + break; + case "L": + ansX -= 1; + break; + case "R": + ansX += 1; + break; + default: + return false; // Invalid character } - if(!validPosition(ansY, ansX)) - return false; + if (!validPosition(ansY, ansX)) return false; } // Path was valid, finally verify that the answer path brought us to the end coordinates diff --git a/src/utils/Heap.ts b/src/utils/Heap.ts index ea31ed7bc..634da4fe4 100644 --- a/src/utils/Heap.ts +++ b/src/utils/Heap.ts @@ -28,16 +28,14 @@ abstract class BinHeap { /** Get the value of the root-most element of the heap, without changing the heap. */ public peek(): T | undefined { - if(this.data.length == 0) - return undefined; + if (this.data.length == 0) return undefined; return this.data[0][1]; } /** Remove the root-most element of the heap and return the removed element's value. */ public pop(): T | undefined { - if(this.data.length == 0) - return undefined; + if (this.data.length == 0) return undefined; const value = this.data[0][1]; @@ -52,9 +50,8 @@ abstract class BinHeap { /** Change the weight of an element in the heap. */ public changeWeight(predicate: (value: T) => boolean, weight: number): void { // Find first element with matching value, if any - const i = this.data.findIndex(e => predicate(e[1])); - if(i == -1) - return; + const i = this.data.findIndex((e) => predicate(e[1])); + if (i == -1) return; // Update that element's weight this.data[i][0] = weight; @@ -62,22 +59,22 @@ abstract class BinHeap { // And re-heapify if needed const p = Math.floor((i - 1) / 2); - if(!this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) // Needs to shift root-wards? + if (!this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) + // Needs to shift root-wards? this.heapifyUp(i); - else // Try shifting deeper - this.heapifyDown(i); + // Try shifting deeper + else this.heapifyDown(i); } /** Restore heap condition, starting at index i and traveling towards root. */ protected heapifyUp(i: number): void { // Swap the new element up towards root until it reaches root position or // settles under under a suitable parent - while(i > 0) { + while (i > 0) { const p = Math.floor((i - 1) / 2); // Reached heap-ordered state already? - if(this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) - break; + if (this.heapOrderABeforeB(this.data[p][0], this.data[i][0])) break; // Swap const tmp = this.data[p]; @@ -93,20 +90,17 @@ abstract class BinHeap { protected heapifyDown(i: number): void { // Swap the shifted element down in the heap until it either reaches the // bottom layer or is in correct order relative to it's children - while(i < this.data.length) { + while (i < this.data.length) { const l = i * 2 + 1; const r = i * 2 + 2; let toSwap = i; // Find which one of element i and it's children should be closest to root - if(l < this.data.length && this.heapOrderABeforeB(this.data[l][0], this.data[toSwap][0])) - toSwap = l; - if(r < this.data.length && this.heapOrderABeforeB(this.data[r][0], this.data[toSwap][0])) - toSwap = r; + if (l < this.data.length && this.heapOrderABeforeB(this.data[l][0], this.data[toSwap][0])) toSwap = l; + if (r < this.data.length && this.heapOrderABeforeB(this.data[r][0], this.data[toSwap][0])) toSwap = r; // Already in order? - if(i == toSwap) - break; + if (i == toSwap) break; // Not in order. Swap child that should be closest to root up to 'i' and repeat const tmp = this.data[toSwap]; @@ -124,7 +118,6 @@ abstract class BinHeap { protected abstract heapOrderABeforeB(weightA: number, weightB: number): boolean; } - /** Binary max-heap. */ export class MaxHeap extends BinHeap { heapOrderABeforeB(weightA: number, weightB: number): boolean {