Coding Solution

Power of Thor - Episode 1 – CodinGame solution in C

Easygame loopcoordinatesgreedystate trackingUpdated
Short answer

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

  1. 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.
  2. 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.
  3. 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.
  4. 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

TimeO(1) per turn
SpaceO(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;
}
            

Common mistakes

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.

Similar puzzles

★ 5.0 · 16 reviews · 928 users Add to Chrome