Guest User

Untitled

a guest
Jul 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import math._
  2. import scala.util._
  3.  
  4. object Player extends App {
  5. def getDirection(input: String): (Int, Int) = {
  6. input match {
  7. case "U" => (0, -1)
  8. case "UR" => (1, -1)
  9. case "R" => (1, 0)
  10. case "DR" => (1, 1)
  11. case "D" => (0, 1)
  12. case "DL" => (-1, 1)
  13. case "L" => (-1, 0)
  14. case "UL" => (-1, -1)
  15. }
  16. }
  17.  
  18. def findNewRelativePositionOnAxis(direction: Int, min : Int, max : Int, current : Int) : Int = {
  19. direction match {
  20. case 1 => (max - current + 1 ) / 2
  21. case -1 => if(current == 1) -1 else (min - current - 1 ) / 2 //edge case for when the goal is at position 0
  22. case _ => 0
  23. }
  24. }
  25.  
  26. def loop(x: Int, y: Int, minX: Int, minY: Int, maxX: Int, maxY: Int): Nothing = {
  27. val goaldir = getDirection(readLine)
  28.  
  29. //Update min and max values to narrow down the search
  30. val newMaxX = if(goaldir._1 == -1) x else maxX
  31. val newMaxY = if(goaldir._2 == -1) y else maxY
  32. val newMinX = if(goaldir._1 == 1) x else minX
  33. val newMinY = if(goaldir._2 == 1) y else minY
  34.  
  35. //Compute the next position
  36. val newX = x + findNewRelativePositionOnAxis(goaldir._1, newMinX, newMaxX, x)
  37. val newY = y + findNewRelativePositionOnAxis(goaldir._2, newMinY, newMaxY, y)
  38.  
  39. //Send the result
  40. println(newX + " " + newY)
  41.  
  42. loop(newX, newY, newMinX, newMinY, newMaxX, newMaxY)
  43. }
  44.  
  45. // w: width of the building.
  46. // h: height of the building.
  47. val Array(width, height) = for(i <- readLine split " ") yield i.toInt
  48. val Array(x0, y0) = for(i <- readLine split " ") yield i.toInt
  49.  
  50. loop(x0, y0, 0, 0, width, height)
  51. }
Add Comment
Please, Sign In to add comment