challenge 2d-array

This commit is contained in:
Lorenzo Iovino 2024-01-23 16:38:36 +01:00
parent 10d13dc384
commit 069cfb45fe
7 changed files with 229 additions and 41 deletions

View file

@ -0,0 +1,23 @@
import {describe, it, skip} from 'node:test';
import * as assert from "node:assert";
import {findHighestHourglassSum} from "./2d-array";
import {Matrix} from "./matrix";
describe("2D Array", () => {
describe("findHighestHourglassSum", () => {
it("should return 0 if the matrix is empty", () => {
assert.equal(findHighestHourglassSum(new Matrix([])), 0);
})
it("should return 19 with this matrix", () => {
assert.equal(findHighestHourglassSum(
new Matrix([
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 2, 4, 4, 0],
[0, 0, 0, 2, 0, 0],
[0, 0, 1, 2, 4, 0]]
)), 19);
})
})
})

18
src/2d-array/2d-array.ts Normal file
View file

@ -0,0 +1,18 @@
import {Matrix} from "./matrix";
import {Hourglass} from "./hourglass";
export type TwoDimensionVector = Array<number[]>
export function findHighestHourglassSum(matrix: Matrix) {
const hourglasses: Hourglass[] = Hourglass.extractFromMatrix(matrix);
const sums: number[] = [];
if(hourglasses.length === 0) {
return 0;
}
for(let i = 0; i < hourglasses.length; i++) {
sums.push(hourglasses[i].sum());
}
return Math.max(...sums);
}

View file

@ -0,0 +1,76 @@
import {describe, it, skip} from 'node:test';
import * as assert from "node:assert";
import {Hourglass} from "./hourglass";
import {Matrix} from "./matrix";
import {TwoDimensionVector} from "./2d-array";
describe("2D Array", () => {
describe("extractFromMatrix", () => {
it("should extract the hourglasses matrices", () => {
const hourglasses = Hourglass.extractFromMatrix(new Matrix([
[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 2, 4, 4, 0],
[0, 0, 0, 2, 0, 0],
[0, 0, 1, 2, 4, 0]
]));
const matrixOfHourglasses: TwoDimensionVector[] = [];
hourglasses.forEach(hG => {
matrixOfHourglasses.push(hG.items);
})
assert.deepStrictEqual(matrixOfHourglasses, [
[[1, 1, 1], [0, 1, 0], [1, 1, 1]],
[[1, 1, 0], [0, 0, 0], [1, 1, 0]],
[[1, 0, 0], [0, 0, 0], [1, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 1, 0], [0, 1, 0], [0, 0, 2]],
[[1, 0, 0], [0, 1, 0], [0, 2, 4]],
[[0, 0, 0], [0, 0, 0], [2, 4, 4]],
[[0, 0, 0], [0, 0, 0], [4, 4, 0]],
[[1, 1, 1], [0, 0, 0], [0, 0, 0]],
[[1, 1, 0], [0, 2, 0], [0, 0, 2]],
[[1, 0, 0], [0, 4, 0], [0, 2, 0]],
[[0, 0, 0], [0, 4, 0], [2, 0, 0]],
[[0, 0, 2], [0, 0, 0], [0, 0, 1]],
[[0, 2, 4], [0, 0, 0], [0, 1, 2]],
[[2, 4, 4], [0, 2, 0], [1, 2, 4]],
[[4, 4, 0], [0, 0, 0], [2, 4, 0]],
])
})
it("should return a single hourglass", () => {
const hourglasses = Hourglass.extractFromMatrix(
(new Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]
])));
assert.equal(hourglasses.length, 1)
assert.deepStrictEqual(hourglasses[0].items, [[1, 1, 1], [0, 1, 0], [1, 1, 1]])
})
it("should return zero hourglass", () => {
assert.deepStrictEqual(Hourglass.extractFromMatrix(
new Matrix([])), [])
})
it("should return zero hourglass", () => {
assert.deepStrictEqual(Hourglass.extractFromMatrix(
new Matrix([[1, 2], [3, 4]])), [])
})
});
describe("sum", () => {
it("should return the sum of every item in an hourglass", () => {
assert.equal(new Hourglass([[1, 1, 1], [1, 1, 1], [1, 1, 1]]).sum(),7);
})
})
describe("hourglassify", () => {
it("should convert a 3x3 matrix in an hourglass", () => {
assert.deepStrictEqual(Hourglass.hourglassify([[1, 1, 1], [1, 1, 1], [1, 1, 1]]),[[1, 1, 1], [0, 1, 0], [1, 1, 1]]);
})
})
});

56
src/2d-array/hourglass.ts Normal file
View file

@ -0,0 +1,56 @@
import {TwoDimensionVector} from "./2d-array";
import {Matrix} from "./matrix";
/**
* An hourglass is a matrix of numbers with this shape:
* a b c x x x
* 0 d 0 0 x 0
* e f g x x x
*/
export class Hourglass {
items: TwoDimensionVector = []
constructor(items: TwoDimensionVector) {
this.items = Hourglass.hourglassify(items);
}
static hourglassify(items: TwoDimensionVector): TwoDimensionVector {
items[1][0] = 0;
items[1][2] = 0;
return items;
}
sum(): number {
let sum = 0;
this.items.forEach(item => {
item.forEach(num => {
sum += num;
})
})
return sum;
}
static extractFromMatrix(matrix: Matrix): Hourglass[] {
if (matrix.items.length >= 3) {
const hourglasses: Hourglass[] = [];
let hourglass: Hourglass;
const width = matrix.items[0].length;
const height = matrix.items.length;
const maxRightShift = width - 3;
const maxTopShift = height - 3;
for(let tS = 0; tS <= maxTopShift; tS++) {
for (let rS = 0; rS <= maxRightShift; rS++) {
hourglasses.push(new Hourglass([
[matrix.items[tS][rS], matrix.items[tS][rS + 1], matrix.items[tS][rS + 2]],
[matrix.items[tS + 1][rS], matrix.items[tS + 1][rS + 1], matrix.items[tS + 1][rS + 2]],
[matrix.items[tS + 2][rS], matrix.items[tS + 2][rS + 1], matrix.items[tS + 2][rS + 2]]]));
}
}
return hourglasses;
} else {
return [];
}
}
}

View file

@ -0,0 +1,4 @@
import {describe, it, skip} from 'node:test';
describe("Matrix", () => {
skip();
})

9
src/2d-array/matrix.ts Normal file
View file

@ -0,0 +1,9 @@
import {TwoDimensionVector} from "./2d-array";
export class Matrix {
items: TwoDimensionVector = []
constructor(items: TwoDimensionVector) {
this.items = items;
}
}

View file

@ -2,6 +2,7 @@ import {describe, it} from 'node:test';
import * as assert from "node:assert";
import {countA, countInstanceOfCharInAString} from "./repeated-string";
describe("Repeated String", () => {
describe("countA", () => {
it("should return 0 if the s string is empty", (t) => {
assert.equal(countA("", 0), 0);
@ -49,3 +50,4 @@ describe("countInstanceOfCharInAString", () => {
assert.equal(countInstanceOfCharInAString("a", "aaa"), 3);
})
})
});