Created
December 28, 2016 15:19
-
-
Save tvidenov/8d9cadc7bf36ceffaeb42aea006e356b to your computer and use it in GitHub Desktop.
Codingame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Glibc | |
public struct StderrOutputStream: OutputStreamType { | |
public mutating func write(string: String) { fputs(string, stderr) } | |
} | |
public var errStream = StderrOutputStream() | |
/** | |
* Auto-generated code below aims at helping you parse | |
* the standard input according to the problem statement. | |
* --- | |
* Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders. | |
**/ | |
let inputs = (readLine()!).characters.split{$0 == " "}.map(String.init) | |
let lightX = Int(inputs[0])! // the X position of the light of power | |
let lightY = Int(inputs[1])! // the Y position of the light of power | |
var initialTX = Int(inputs[2])! // Thor's starting X position | |
var initialTY = Int(inputs[3])! // Thor's starting Y position | |
var coordinate = "" | |
// game loop | |
while true { | |
let remainingTurns = Int(readLine()!)! // The remaining amount of turns Thor can move. Do not remove this line. | |
// Write an action using print("message...") | |
// To debug: debugPrint("Debug messages...", toStream: &errStream) | |
if initialTY == lightY { | |
if initialTX < lightX { | |
initialTX += 1 | |
coordinate = "E" | |
} else { | |
initialTX -= 1 | |
coordinate = "W" | |
} | |
} else if (initialTY > lightY) { | |
if initialTX == lightX { | |
initialTY -= 1 | |
coordinate = "N" | |
} else if (initialTX > lightX) { | |
coordinate = "NW" | |
initialTX -= 1 | |
initialTY -= 1 | |
} else { | |
coordinate = "NE" | |
initialTX += 1 | |
initialTY -= 1 | |
} | |
} else if (initialTY < lightY) { | |
if initialTX == lightX { | |
coordinate = "S" | |
initialTY += 1 | |
} else if (initialTX > lightX) { | |
coordinate = "SW" | |
initialTX -= 1 | |
initialTY += 1 | |
} else { | |
coordinate = "SE" | |
initialTX += 1 | |
initialTY += 1 | |
} | |
} | |
// A single line providing the move to be made: N NE E SE S SW W or NW | |
print(coordinate) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment