Created
February 8, 2015 02:06
-
-
Save ravgoundar/74d774f74318cd5762bb to your computer and use it in GitHub Desktop.
CodinGame - Easy - Power of Thor (JavaScript)
This file contains 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
var inputs = readline().split(' '); | |
var LX = parseInt(inputs[0]); | |
var LY = parseInt(inputs[1]); | |
var TX = parseInt(inputs[2]); | |
var TY = parseInt(inputs[3]); | |
while (true) { | |
var E = parseInt(readline()); | |
if (TY == LY) { | |
if (TX < LX) { | |
print('E'); | |
TX++; | |
} else { | |
print('W') | |
TX--; | |
} | |
} else if (TY > LY) { | |
if (TX == LX) { | |
print('N'); | |
printErr(TY); | |
TY--; | |
} else if (TX > LX) { | |
print('NW'); | |
TX--; | |
TY--; | |
} else { | |
print('NE'); | |
TX++; | |
TY--; | |
} | |
} else if (TY < LY) { | |
if (TX == LX) { | |
print('S'); | |
TY++; | |
} else if (TX > LX) { | |
print('SW'); | |
TX--; | |
TY++; | |
} else { | |
print('SE'); | |
TX++; | |
TY++; | |
} | |
} | |
} |
@RayRayG Lol dude, I don't mean to be rude, but this is a code golf challenge and you're supposed to solve it in the least amount of characters.
Example 1 (91 characters, but with cheating, cause it uses hardcoded values):
o={7:'W',5:'S'},a=+readline()[0];for(i=0;;i++)print(a>3?o[a]:a?i>13?'E':'SE':i>16?'W':'SW')
Example 2 (173 characters, but without cheating and without hardcoding anything, and could be further improved):
c=readline().split(' ').map(Number);while(1)(c[3]>c[1]?(d='N',c[3]--):(c[3]<c[1]?(d='S',c[3]++):d=''))+(c[2]>c[0]?(d+='W',c[2]--):(c[2]<c[0]?(d+='E',c[2]++):d+='')),print(d)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is so that when it is doing the IF check it is making sure that the new coordinate of Thor is still greater/less than the light. If it did not add one to the position when he moved then it would always be a passing case, and Thor would move off the field.