Created
August 27, 2024 10:10
-
-
Save Harin-Kaklotar/db66bd1337bbf36ef568e0e6ed518d68 to your computer and use it in GitHub Desktop.
Covid Robot problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Command { | |
fun execute(robot: Robot) | |
} | |
class TurnLeft : Command { | |
override fun execute(robot: Robot) { | |
robot.rotateLeft() | |
} | |
} | |
class TurnRight : Command { | |
override fun execute(robot: Robot) { | |
robot.rotateRight() | |
} | |
} | |
class MoveForward : Command { | |
override fun execute(robot: Robot) { | |
robot.move() | |
} | |
} | |
class Robot( | |
private var x: Int, | |
private var y: Int, | |
private var direction: Char, | |
private val maxX: Int, | |
private val maxY: Int | |
) { | |
private val directions = listOf('N', 'E', 'S', 'W') | |
fun rotateLeft() { | |
val currentIdx = directions.indexOf(direction) | |
direction = directions[(currentIdx - 1 + directions.size) % directions.size] | |
} | |
fun rotateRight() { | |
val currentIdx = directions.indexOf(direction) | |
direction = directions[(currentIdx + 1) % directions.size] | |
} | |
fun move() { | |
when (direction) { | |
'N' -> if (y < maxY) y++ | |
'E' -> if (x < maxX) x++ | |
'S' -> if (y > 0) y-- | |
'W' -> if (x > 0) x-- | |
} | |
} | |
fun executeInstructions(commands: List<Command>) { | |
for (command in commands) { | |
command.execute(this) | |
} | |
} | |
fun getPosition(): String { | |
return "$x $y $direction" | |
} | |
} | |
class CommandInterpreter { | |
fun interpret(input: String): List<Command> { | |
return input.map { | |
when (it) { | |
'L' -> TurnLeft() | |
'R' -> TurnRight() | |
'M' -> MoveForward() | |
else -> throw InvalidCommandException("Invalid command: $it, only supported commands are \"L,R,M\"") | |
} | |
} | |
} | |
} | |
class InvalidCommandException(message: String) : Exception(message) | |
fun main() { | |
println("Please enter covid ward upper right coordinate separated by space : Example \"5 5\"") | |
val gridSize: List<Int> = readLine()!!.split(" ").map { it.toInt() } | |
val maxX = gridSize[0] | |
val maxY = gridSize[1] | |
println("Please enter robot's initial position: Example \"1 2 N\"") | |
val initialPosition: List<String> = readLine()!!.split(" ") | |
val x = initialPosition[0].toInt() | |
val y = initialPosition[1].toInt() | |
val direction = initialPosition[2].first() | |
println("Please enter command: Example \"LMLMLMLMM\" ") | |
val instructions: String = readLine()!! | |
val robot = Robot(x, y, direction, maxX, maxY) | |
val commandInterpreter = CommandInterpreter() | |
val commands = commandInterpreter.interpret(instructions) | |
robot.executeInstructions(commands) | |
println("Here is the final position of the robot") | |
println(robot.getPosition()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment