Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.74 KB | None | 0 0
  1. package com.example.myapplication
  2.  
  3. fun main() {
  4.     val startingNode = Node(0,0)
  5.     val stringInput = readLine()!!
  6.     if (stringInput.isEmpty()){
  7.         return
  8.     }
  9.     val node = getGridSizeFromInput(stringInput)
  10.     if(node==null){
  11.         println("You entered wrong format of grid size")
  12.     }else{
  13.         val listOfCoordinates = parseInput(node,stringInput)
  14.         val commands = ArrayList<String>()
  15.         for (node in listOfCoordinates){
  16.             commands.addAll(travel(startingNode, node))
  17.             startingNode.x=node.x
  18.             startingNode.y=node.y
  19.         }
  20.         println(convertCommandsToString(commands))
  21.     }
  22. }
  23.  
  24. fun getGridSizeFromInput(stringInput: String): Node?{
  25.     val inputParsed = stringInput.split(" ")[0].split("x")
  26.     return if(inputParsed.size==2){
  27.         try {
  28.             Node(inputParsed[0].toInt(),inputParsed[1].toInt())
  29.         }catch (e: NumberFormatException){
  30.             null
  31.         }
  32.     }else{
  33.         null
  34.     }
  35. }
  36.  
  37. fun parseInput(node: Node, stringInput: String): ArrayList<Node>{
  38.     val coordList = ArrayList<Node>()
  39.     var input = stringInput
  40.     val inputParsed = stringInput.split(" ")
  41.     input = input.replace(inputParsed[0],"")
  42.     input = input.replace(" ","")
  43.     val parsedCoords = input.split(")(")
  44.     for (i in parsedCoords){
  45.         val iString = i.replace("(","").replace(")","")
  46.         val coords = iString.split(",")
  47.         if(coords[0].isNotEmpty() && coords[1].isNotEmpty()){
  48.             try {
  49.                 if(coords[0].toInt()<node.x && coords[1].toInt()<node.y){
  50.                     coordList.add(Node(coords[0].toInt(), coords[1].toInt()))
  51.                 }
  52.             }catch (e: NumberFormatException){
  53.                 println(e)
  54.             }
  55.         }
  56.     }
  57.     return coordList
  58. }
  59.  
  60. fun travel(startNode: Node, endNode: Node): ArrayList<String>{
  61.     val commands = ArrayList<String>()
  62.     var startX = startNode.x
  63.     var startY = startNode.y
  64.     val endX = endNode.x
  65.     val endY = endNode.y
  66.         if(startX>endX){
  67.             commands.add("W".repeat(startX-endX))
  68.             startX=endX
  69.         }else if(startX<endX){
  70.             commands.add("E".repeat(endX-startX))
  71.             startX=endX
  72.         }
  73.         if(startY>endY){
  74.             commands.add("S".repeat(startY-endY))
  75.             startY=endY
  76.         }else if(startY<endY){
  77.             commands.add("N".repeat(endY-startY))
  78.             startY=endY
  79.         }
  80.         if(startX==endX && startY==endY){
  81.             commands.add("D")
  82.         }
  83.     return commands
  84. }
  85.  
  86. fun convertCommandsToString(commands: ArrayList<String>):String{
  87.     var convertedCommands = ""
  88.     for (command in commands){
  89.         convertedCommands += command
  90.     }
  91.     return convertedCommands
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement