Robert Eisele
Engineer, Systems Architect and DBA

Codingame: The Last Crusade - Episode 1

Original Problem

The Goal

Your objective is to write a program capable of predicting the route Indy will take on his way down a tunnel. Indy is not in danger of getting trapped in this first mission.

Rules

The tunnel consists of a patchwork of square rooms of different types.The rooms can be accessed and activated by computer using an ancient RS232 serial port (because Mayans aren't very technologically advanced, as is to be expected...).

There is a total of 14 room types (6 base shapes extended to 14 through rotations).

Upon entering a room, depending on the type of the room and Indy's entrance point (TOP,LEFT, or RIGHT) he will either exit the room through a specific exit point, suffer a lethal collision or lose momentum and get stuck:

Type 0
This room type is not part of the tunnel per se as Indy cannot move across it.

Type 1
The green arrows indicate Indy's possible paths through the room.

Type 2

Type 3
The room of type 3 is similar to the room of type 2 but rotated.

Type 4

Type 5
A red arrow indicate a path that Indy cannot use to move across the room.

Type 6

Type 7

Type 8

Type 9

Type 10

Type 11

Type 12

Type 13

Indy is perpetually drawn downwards: he cannot leave a room through the top.

At the start of the game, you are given the map of the tunnel in the form of a rectangular grid of rooms. Each room is represented by its type.

For this first mission, you will familiarize yourself with the tunnel system, the rooms have all been arranged in such a way that Indy will have a safe continuous route between his starting point (top of the temple) and the exit area (bottom of the temple).

Each game turn:
  • You receive Indy's current position
  • Then you specify what Indy's position will be next turn.
  • Indy will then move from the current room to the next according to the shape of the current room.
Victory Conditions
Indy reaches the exit
Lose Conditions
You assert an incorrect position for Indy

Example

Indy starts in the room (1,0),
then, he falls down in (1,1) and moves to (0,1).
After that, he falls in (0,2) and moves to (1,2).
He finally reaches (1,3) from where he can safely escape.

Solution

The solution can be pretty short when converting the given information into a lookup table like this:

var S = [
    null,
    {
        LEFT: "DOWN",
        RIGHT: "DOWN",
        TOP: "DOWN"
    }, {
        LEFT: "RIGHT",
        RIGHT: "LEFT"
    }, {TOP: "DOWN"
    }, {
        TOP: "LEFT",
        RIGHT: "DOWN"
    }, {
        TOP: "RIGHT",
        LEFT: "DOWN"
    }, {
        LEFT: "RIGHT",
        RIGHT: "LEFT"
    }, {
        TOP: "DOWN",
        RIGHT: "DOWN"
    }, {
        RIGHT: "DOWN",
        LEFT: "DOWN"
    }, {
        TOP: "DOWN",
        LEFT: "DOWN"
    }, {
        TOP: "LEFT"
    }, {
        TOP: "RIGHT"
    }, {
        RIGHT: "DOWN"
    }, {
        LEFT: "DOWN"
    }
];

Using this table, we only look up the direction and translate it into an actual movement decision:

var inputs = readline().split(' ');
var W = parseInt(inputs[0]);
var H = parseInt(inputs[1]);
var A = [];

for (var i = 0; i < H; i++) {
    var LINE = readline();
    A.push(LINE.split(" "));
}
var EX = parseInt(readline()); // NOP

while (true) {
    var inputs = readline().split(' ');
    var XI = parseInt(inputs[0]);
    var YI = parseInt(inputs[1]);
    var POS = inputs[2];

    switch (S[A[YI][XI]][POS]) {
        case 'DOWN':
            YI++;
            break;
        case 'LEFT':
            XI--;
            break;
        case 'RIGHT':
            XI++;
            break;
    }
    print(XI + ' ' + YI);
}

Go to overview

All images are copyright to Codingame