Power of Thor - Episode 1 – CodinGame solution in C
Compare Thor's coordinates with the light's: add N or S if the Y values differ, then E or W if the X values differ, print the combined direction, and update Thor's position yourself because the game never sends it again. Moving diagonally while both axes differ is the shortest path, so you always arrive within the allowed number of turns.
The problem
Thor stands on a 40 x 18 grid and must reach a fixed light. The first line gives the light's X and Y, then Thor's starting X and Y. The origin is the top-left corner, so Y increases as you go down.
Each turn the game sends the number of moves Thor has left, and you answer with one of eight directions: N, NE, E, SE, S, SW, W or NW. Thor loses if he walks off the map or runs out of moves before reaching the light.
Approach, step by step
- Read the initial positions onceOnly the first line contains coordinates. Store the light position and Thor's position (or just the difference between them) in variables you will update yourself.
- Choose the vertical partIf Thor is above the light (smaller Y) the vertical part is S, if he is below it is N, and if the Y values match there is no vertical part.
- Choose the horizontal partSame logic on X: E when the light is to the right, W when it is to the left, nothing when they are aligned.
- Print and updateConcatenate the two parts, vertical first, print the result, then move Thor's stored coordinates by one step in each direction used. Without that update Thor will overshoot because your code still thinks he is at the start.
Complexity
O(1) per turnO(1)Each turn is two comparisons and two increments. The total number of turns is max(|dx|, |dy|), the Chebyshev distance between Thor and the light.
Solution code
Power of Thor - Episode 1 – C
Keeps the four coordinates and updates Thor's X and Y after each move. It does not read the remaining-turns value; CodinGame does not require you to consume it, but reading it with scanf is a cleaner habit.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
// Coordinates of the light and Thor
int lX, lY, tX, tY;
scanf("%d%d%d%d", &lX, &lY, &tX, &tY);
for (;;) {
if (tY < lY) {
printf("S");
tY++;
}
else if (tY > lY) {
printf("N");
tY--;
}
if (tX < lX) {
printf("E");
tX++;
}
else if (tX > lX) {
printf("W");
tX--;
}
printf("\n");
}
return EXIT_SUCCESS;
}
Power of Thor - Episode 1 – Java
Stores only the differences light - Thor, so two variables are enough and every comparison is against 0. A neat trick that halves the state to maintain.
import java.util.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
/* We could store the coordinates of Thor and its target (4 vars)
Or like here, store the difference target - Thor (2 vars)
It is more efficient for comparisons and simpler*/
int diffX = in.nextInt();
int diffY = in.nextInt();
diffX -= in.nextInt();
diffY -= in.nextInt();
while (true) {
if (diffY > 0) {
System.out.print("S");
diffY--;
}
else if (diffY < 0) {
System.out.print("N");
diffY++;
}
if (diffX > 0) {
System.out.print("E");
diffX--;
}
else if (diffX < 0) {
System.out.print("W");
diffX++;
}
System.out.print("\n");
}
}
}
Power of Thor - Episode 1 – Python 3
Builds the answer from two strings that may be empty and prints them with sep="" so that a pure vertical or horizontal move produces a single letter.
# lX, lY: coordinates of the light of power
# tX, tY: Thor's coordinates
lX, lY, tX, tY = [int(i) for i in input().split()]
# The light never moves, so the four numbers above are all we need.
# Each turn the game also sends the remaining number of moves; this code ignores it.
while (True):
# ch1 is the vertical part (N/S), ch2 the horizontal part (E/W).
# An empty string means "no move on that axis".
ch1 = ch2 = ""
# Y grows downward on CodinGame maps, so a light below Thor means going South.
if tY < lY:
ch1 = "S"
tY+=1
elif tY > lY:
ch1 = "N"
tY-=1
# Same idea on the X axis: light to the right means East.
if tX < lX:
ch2 = "E"
tX+=1
elif tX > lX:
ch2 = "W"
tX-=1
# Vertical letter first: the referee accepts "NE", not "EN".
print(ch1, ch2, sep="")
Common mistakes
- Not updating Thor's positionThe game sends Thor's coordinates only once. If you compute the direction from the initial values every turn, Thor keeps walking in the same direction and leaves the map.
- Inverting North and SouthOn screen coordinates Y grows downward. A light with a larger Y than Thor is to the South, not the North. Mixing this up sends Thor straight off the top edge.
- Writing "EN" instead of "NE"The referee only knows the eight compass strings with the vertical letter first. "EN" or "ES" is an invalid action and ends the game immediately.
- Moving one axis at a timeGoing straight first and then diagonally (or the reverse) takes |dx| + |dy| moves instead of max(|dx|, |dy|). On the tests with a tight move budget this runs out of energy.
Going further
The number of moves needed is the Chebyshev distance max(|dx|, |dy|), not the Manhattan distance |dx| + |dy|, because a diagonal step reduces both differences at once. The greedy rule "move diagonally while both axes differ, then straight" reaches that lower bound, which is why it never runs out of energy: each turn reduces the larger of the two gaps by exactly one. The Java version shows a useful reframing: instead of tracking two positions, track the vector from Thor to the light and drive it to (0, 0). The same idea comes back in Shadows of the Knight and in many path-finding puzzles where you only care about relative position.
FAQ
Why does Thor walk off the map in my Power of Thor solution?
Most likely your code never updates Thor's position. The game only sends his starting coordinates, so you must change them yourself after each move.
Which way is South in Power of Thor?
South is increasing Y. The origin (0, 0) is the top-left corner of the map, as in most screen coordinate systems.
Do I have to read the remaining turns each turn?
The value is sent each turn but you do not need it for this puzzle. Reading it is still good practice so the input stays in sync if you later add logic.