Sha256: a34e77e7ba40da92cafed6c77a038bfa2d36beb092968f997ab0d2c1c47421d5

Contents?: true

Size: 1.25 KB

Versions: 188

Compression:

Stored size: 1.25 KB

Contents

import Direction.RIGHT

enum class Direction(val dx: Int, val dy: Int) {
    UP   ( 0, -1),
    RIGHT( 1,  0),
    DOWN ( 0,  1),
    LEFT (-1,  0);

    fun turnRight() = Direction.values()[(ordinal + 1) % Direction.values().size]
}

data class Coordinate(val x: Int, val y: Int) {

    fun step(direction: Direction) = copy(x + direction.dx, y + direction.dy)

    fun isWithinGridOfSize(size: Int) = x in 0..size-1 && y in 0..size-1

}

object SpiralMatrix {

    fun ofSize(size: Int): Array<IntArray> {
        require(size >= 0) { "size must be a non-negative integer" }

        if (size == 0) return emptyArray()

        val result = Array(size, { IntArray(size, {0}) })
        val entryCount = Math.pow(size.toDouble(), 2.0).toInt()

        var coord = Coordinate(0, 0)
        var direction = RIGHT

        for (i in 0 until entryCount) {
            result[coord.y][coord.x] = i + 1

            val maybeNextCoord = coord.step(direction)
            if (maybeNextCoord.isWithinGridOfSize(size) && result[maybeNextCoord.y][maybeNextCoord.x] == 0) {
                coord = maybeNextCoord
            } else {
                direction = direction.turnRight()
                coord = coord.step(direction)
            }
        }

        return result
    }

}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.179 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.178 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.177 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.176 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.175 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.174 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.173 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.172 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.171 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.170 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.169 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.167 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.166 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.165 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.164 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.163 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.162 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.161 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt
trackler-2.2.1.160 tracks/kotlin/exercises/spiral-matrix/.meta/src/reference/kotlin/SpiralMatrix.kt