fmt and lint

This commit is contained in:
Olivier Gagnon 2022-04-08 00:39:45 -04:00
parent d956e47246
commit 2b2af797a7
3 changed files with 53 additions and 56 deletions

@ -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";

@ -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,8 +831,7 @@ 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++) {
@ -842,9 +841,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
// 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;
}
}
@ -887,10 +885,11 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
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 (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
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

@ -28,16 +28,14 @@ abstract class BinHeap<T> {
/** 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<T> {
/** 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,10 +59,11 @@ abstract class BinHeap<T> {
// 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. */
@ -76,8 +74,7 @@ abstract class BinHeap<T> {
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];
@ -99,14 +96,11 @@ abstract class BinHeap<T> {
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<T> {
protected abstract heapOrderABeforeB(weightA: number, weightB: number): boolean;
}
/** Binary max-heap. */
export class MaxHeap<T> extends BinHeap<T> {
heapOrderABeforeB(weightA: number, weightB: number): boolean {