Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math._
- import scala.util._
- object Player extends App {
- def getDirection(input: String): (Int, Int) = {
- input match {
- case "U" => (0, -1)
- case "UR" => (1, -1)
- case "R" => (1, 0)
- case "DR" => (1, 1)
- case "D" => (0, 1)
- case "DL" => (-1, 1)
- case "L" => (-1, 0)
- case "UL" => (-1, -1)
- }
- }
- def findNewRelativePositionOnAxis(direction: Int, min : Int, max : Int, current : Int) : Int = {
- direction match {
- case 1 => (max - current + 1 ) / 2
- case -1 => if(current == 1) -1 else (min - current - 1 ) / 2 //edge case for when the goal is at position 0
- case _ => 0
- }
- }
- def loop(x: Int, y: Int, minX: Int, minY: Int, maxX: Int, maxY: Int): Nothing = {
- val goaldir = getDirection(readLine)
- //Update min and max values to narrow down the search
- val newMaxX = if(goaldir._1 == -1) x else maxX
- val newMaxY = if(goaldir._2 == -1) y else maxY
- val newMinX = if(goaldir._1 == 1) x else minX
- val newMinY = if(goaldir._2 == 1) y else minY
- //Compute the next position
- val newX = x + findNewRelativePositionOnAxis(goaldir._1, newMinX, newMaxX, x)
- val newY = y + findNewRelativePositionOnAxis(goaldir._2, newMinY, newMaxY, y)
- //Send the result
- println(newX + " " + newY)
- loop(newX, newY, newMinX, newMinY, newMaxX, newMaxY)
- }
- // w: width of the building.
- // h: height of the building.
- val Array(width, height) = for(i <- readLine split " ") yield i.toInt
- val Array(x0, y0) = for(i <- readLine split " ") yield i.toInt
- loop(x0, y0, 0, 0, width, height)
- }
Add Comment
Please, Sign In to add comment