Advertisement
Guest User

Grok Scala - Dallas - Thor - Doug

a guest
Feb 9th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.64 KB | None | 0 0
  1. import math._
  2. import scala.util._
  3.  
  4. /**
  5.  * Auto-generated code below aims at helping you parse
  6.  * the standard input according to the problem statement.
  7.  **/
  8. object Player {
  9.  
  10.     case class Pos(x: Int, y: Int) {
  11.         def compare(that: Pos): (Int, Int) = (x.compare(that.x), y.compare(that.y))
  12.        
  13.         def next(offset: (Int, Int)): Pos =
  14.                 new Pos(x + offset._1, y + offset._2)
  15.     }
  16.  
  17.     def path(light: Pos, thor: Pos): Iterator[String] = {
  18.         val xs = Array("W", "", "E")
  19.         val ys = Array("N", "", "S")
  20.  
  21.         def move(thor: => Pos): Stream[String] = {
  22.             val offset = light.compare(thor)
  23.             val result = ys(offset._2 + 1) + xs(offset._1 + 1)
  24.             if (result == "") Stream.empty
  25.             else result #:: move(thor.next(offset))
  26.         }
  27.        
  28.         move(thor).iterator
  29.     }
  30.    
  31.     def main(args: Array[String]) {
  32.         // lx: the X position of the light of power
  33.         // ly: the Y position of the light of power
  34.         // tx: Thor's starting X position
  35.         // ty: Thor's starting Y position
  36.         val Array(lx, ly, tx, ty) = for(i <- readLine split " ") yield i.toInt
  37.         val moves = path(Pos(lx, ly), Pos(tx, ty))
  38.  
  39.         // game loop
  40.         while(true) {
  41.             val e = readInt // The level of Thor's remaining energy, representing the number of moves he can still make.
  42.            
  43.             // Write an action using println
  44.             // To debug: Console.err.println("Debug messages...")
  45.            
  46.             println(moves.next) // A single line providing the move to be made: N NE E SE S SW W or NW
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement