Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication
- fun main() {
- val startingNode = Node(0,0)
- val stringInput = readLine()!!
- if (stringInput.isEmpty()){
- return
- }
- val node = getGridSizeFromInput(stringInput)
- if(node==null){
- println("You entered wrong format of grid size")
- }else{
- val listOfCoordinates = parseInput(node,stringInput)
- val commands = ArrayList<String>()
- for (node in listOfCoordinates){
- commands.addAll(travel(startingNode, node))
- startingNode.x=node.x
- startingNode.y=node.y
- }
- println(convertCommandsToString(commands))
- }
- }
- fun getGridSizeFromInput(stringInput: String): Node?{
- val inputParsed = stringInput.split(" ")[0].split("x")
- return if(inputParsed.size==2){
- try {
- Node(inputParsed[0].toInt(),inputParsed[1].toInt())
- }catch (e: NumberFormatException){
- null
- }
- }else{
- null
- }
- }
- fun parseInput(node: Node, stringInput: String): ArrayList<Node>{
- val coordList = ArrayList<Node>()
- var input = stringInput
- val inputParsed = stringInput.split(" ")
- input = input.replace(inputParsed[0],"")
- input = input.replace(" ","")
- val parsedCoords = input.split(")(")
- for (i in parsedCoords){
- val iString = i.replace("(","").replace(")","")
- val coords = iString.split(",")
- if(coords[0].isNotEmpty() && coords[1].isNotEmpty()){
- try {
- if(coords[0].toInt()<node.x && coords[1].toInt()<node.y){
- coordList.add(Node(coords[0].toInt(), coords[1].toInt()))
- }
- }catch (e: NumberFormatException){
- println(e)
- }
- }
- }
- return coordList
- }
- fun travel(startNode: Node, endNode: Node): ArrayList<String>{
- val commands = ArrayList<String>()
- var startX = startNode.x
- var startY = startNode.y
- val endX = endNode.x
- val endY = endNode.y
- if(startX>endX){
- commands.add("W".repeat(startX-endX))
- startX=endX
- }else if(startX<endX){
- commands.add("E".repeat(endX-startX))
- startX=endX
- }
- if(startY>endY){
- commands.add("S".repeat(startY-endY))
- startY=endY
- }else if(startY<endY){
- commands.add("N".repeat(endY-startY))
- startY=endY
- }
- if(startX==endX && startY==endY){
- commands.add("D")
- }
- return commands
- }
- fun convertCommandsToString(commands: ArrayList<String>):String{
- var convertedCommands = ""
- for (command in commands){
- convertedCommands += command
- }
- return convertedCommands
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement