From 069cfb45feb133bb9eb9ca69e849f4a4f3f8a1e6 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Tue, 23 Jan 2024 16:38:36 +0100 Subject: [PATCH] challenge 2d-array --- src/2d-array/2d-array.spec.ts | 23 ++++++ src/2d-array/2d-array.ts | 18 +++++ src/2d-array/hourglass.spec.ts | 76 +++++++++++++++++++ src/2d-array/hourglass.ts | 56 ++++++++++++++ src/2d-array/matrix.spec.ts | 4 + src/2d-array/matrix.ts | 9 +++ src/repeated-string/repeated-string.spec.ts | 84 +++++++++++---------- 7 files changed, 229 insertions(+), 41 deletions(-) create mode 100644 src/2d-array/2d-array.spec.ts create mode 100644 src/2d-array/2d-array.ts create mode 100644 src/2d-array/hourglass.spec.ts create mode 100644 src/2d-array/hourglass.ts create mode 100644 src/2d-array/matrix.spec.ts create mode 100644 src/2d-array/matrix.ts diff --git a/src/2d-array/2d-array.spec.ts b/src/2d-array/2d-array.spec.ts new file mode 100644 index 0000000..801728f --- /dev/null +++ b/src/2d-array/2d-array.spec.ts @@ -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); + }) + }) +}) \ No newline at end of file diff --git a/src/2d-array/2d-array.ts b/src/2d-array/2d-array.ts new file mode 100644 index 0000000..4dc1107 --- /dev/null +++ b/src/2d-array/2d-array.ts @@ -0,0 +1,18 @@ +import {Matrix} from "./matrix"; +import {Hourglass} from "./hourglass"; + +export type TwoDimensionVector = Array + +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); +} \ No newline at end of file diff --git a/src/2d-array/hourglass.spec.ts b/src/2d-array/hourglass.spec.ts new file mode 100644 index 0000000..db1ef00 --- /dev/null +++ b/src/2d-array/hourglass.spec.ts @@ -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]]); + }) + }) +}); \ No newline at end of file diff --git a/src/2d-array/hourglass.ts b/src/2d-array/hourglass.ts new file mode 100644 index 0000000..2c3ff0c --- /dev/null +++ b/src/2d-array/hourglass.ts @@ -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 []; + } + } +} diff --git a/src/2d-array/matrix.spec.ts b/src/2d-array/matrix.spec.ts new file mode 100644 index 0000000..e9d5864 --- /dev/null +++ b/src/2d-array/matrix.spec.ts @@ -0,0 +1,4 @@ +import {describe, it, skip} from 'node:test'; +describe("Matrix", () => { + skip(); +}) diff --git a/src/2d-array/matrix.ts b/src/2d-array/matrix.ts new file mode 100644 index 0000000..a5f4e9f --- /dev/null +++ b/src/2d-array/matrix.ts @@ -0,0 +1,9 @@ +import {TwoDimensionVector} from "./2d-array"; + +export class Matrix { + items: TwoDimensionVector = [] + + constructor(items: TwoDimensionVector) { + this.items = items; + } +} \ No newline at end of file diff --git a/src/repeated-string/repeated-string.spec.ts b/src/repeated-string/repeated-string.spec.ts index cc3ce20..2d55e42 100644 --- a/src/repeated-string/repeated-string.spec.ts +++ b/src/repeated-string/repeated-string.spec.ts @@ -2,50 +2,52 @@ import {describe, it} from 'node:test'; import * as assert from "node:assert"; import {countA, countInstanceOfCharInAString} from "./repeated-string"; -describe("countA", () => { - it("should return 0 if the s string is empty" , (t) => { - assert.equal(countA("", 0), 0); +describe("Repeated String", () => { + describe("countA", () => { + it("should return 0 if the s string is empty", (t) => { + assert.equal(countA("", 0), 0); + }) + + it("should return 0 if the n is zero", (t) => { + assert.equal(countA("", 0), 0); + }) + + it("should return 1 if the string is 'a' and n is 1", (t) => { + assert.equal(countA("a", 1), 1); + }) + + it("should return 2 if the string is 'a' and n is 2", (t) => { + assert.equal(countA("a", 2), 2); + }) + + it("should return 1 if the string is 'acab' and n is 2", (t) => { + assert.equal(countA("acab", 2), 1); + }) + + it("should return 4 if the string is 'abcac' and n is 10", (t) => { + assert.equal(countA("abcac", 10), 4); + }) + + it("should return 2 if the string is 'ababa' and n is 3", (t) => { + assert.equal(countA("ababa", 3), 2) + }); }) - it("should return 0 if the n is zero" , (t) => { - assert.equal(countA("", 0), 0); - }) + describe("countInstanceOfCharInAString", () => { + it("should return 0 if the s string is empty", (t) => { + assert.equal(countInstanceOfCharInAString("a", ""), 0); + }) - it("should return 1 if the string is 'a' and n is 1", (t) => { - assert.equal(countA("a", 1), 1); - }) + it("should return 0 if the char is empty", (t) => { + assert.equal(countInstanceOfCharInAString("", "hola"), 0); + }) - it("should return 2 if the string is 'a' and n is 2", (t) => { - assert.equal(countA("a", 2), 2); - }) + it("should return 0 if the char is empty and the string is empty", (t) => { + assert.equal(countInstanceOfCharInAString("", ""), 0); + }) - it("should return 1 if the string is 'acab' and n is 2", (t) => { - assert.equal(countA("acab", 2), 1); + it("should return 3 if the char is 'a' and the string is 'aaa'", (t) => { + assert.equal(countInstanceOfCharInAString("a", "aaa"), 3); + }) }) - - it("should return 4 if the string is 'abcac' and n is 10", (t) => { - assert.equal(countA("abcac", 10), 4); - }) - - it("should return 2 if the string is 'ababa' and n is 3", (t) => { - assert.equal(countA("ababa", 3), 2) - }); -}) - -describe("countInstanceOfCharInAString", () => { - it("should return 0 if the s string is empty" , (t) => { - assert.equal(countInstanceOfCharInAString("a", ""), 0); - }) - - it("should return 0 if the char is empty" , (t) => { - assert.equal(countInstanceOfCharInAString("", "hola"), 0); - }) - - it("should return 0 if the char is empty and the string is empty" , (t) => { - assert.equal(countInstanceOfCharInAString("", ""), 0); - }) - - it("should return 3 if the char is 'a' and the string is 'aaa'" , (t) => { - assert.equal(countInstanceOfCharInAString("a", "aaa"), 3); - }) -}) +}); \ No newline at end of file