Sha256: 20c73c5c467f56a06c66260d60ae2390a42d3165cd940213af14b6505c8ac76c

Contents?: true

Size: 1.2 KB

Versions: 188

Compression:

Stored size: 1.2 KB

Contents

fun Orientation.turnLeft()  = Orientation.values()[Math.floorMod(ordinal - 1, Orientation.values().size)]
fun Orientation.turnRight() = Orientation.values()[Math.floorMod(ordinal + 1, Orientation.values().size)]

class Robot(var gridPosition: GridPosition = GridPosition(0, 0), var orientation: Orientation = Orientation.NORTH) {

    fun advance() {
        gridPosition = when (orientation) {
            Orientation.NORTH -> gridPosition.copy(y = gridPosition.y + 1)
            Orientation.EAST  -> gridPosition.copy(x = gridPosition.x + 1)
            Orientation.SOUTH -> gridPosition.copy(y = gridPosition.y - 1)
            Orientation.WEST  -> gridPosition.copy(x = gridPosition.x - 1)
        }
    }

    fun turnLeft() {
        orientation = orientation.turnLeft()
    }

    fun turnRight() {
        orientation = orientation.turnRight()
    }

    fun simulate(instructions: String) {
        for (instruction in instructions) {
            when (instruction) {
                'A' -> advance()
                'R' -> turnRight()
                'L' -> turnLeft()
                else -> throw IllegalArgumentException(String.format("Invalid instruction: '%s'", instruction))
            }
        }
    }

}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.97 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.96 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.95 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.94 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.93 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.92 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.91 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.90 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.89 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.88 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.87 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.86 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.85 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.84 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.83 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.82 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.81 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.80 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt
trackler-2.2.1.79 tracks/kotlin/exercises/robot-simulator/.meta/src/reference/kotlin/Robot.kt