Sha256: 1f94d7c6050421ee7edba681824e398b7b705895febbe0a3b15c033b4c324445

Contents?: true

Size: 977 Bytes

Versions: 42

Compression:

Stored size: 977 Bytes

Contents

class SpiralMatrix {

    static ofSize(size: number) {
        const spiral = Array(size).fill(Array<number>()).map(() => Array<number>(size).fill(0))

        const totalNumbers = size ** 2
        let currentNumber = 1
        let topLeft = 0
        let bottomRight = size - 1

        while (currentNumber <= totalNumbers) {
            for (let x = topLeft; x <= bottomRight; x++) {
                spiral[topLeft][x] = currentNumber++
            }
            for (let y = topLeft + 1; y <= bottomRight; y++) {
                spiral[y][bottomRight] = currentNumber++
            }
            for (let x = bottomRight - 1; x >= topLeft; x--) {
                spiral[bottomRight][x] = currentNumber++
            }
            for (let y = bottomRight - 1; y >= topLeft + 1; y--) {
                spiral[y][topLeft] = currentNumber++
            }
            topLeft++
            bottomRight--
        }

        return spiral
    }

}

export default SpiralMatrix

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/spiral-matrix/spiral-matrix.example.ts