The Descent – CodinGame solution in C
Each turn, read the eight mountain heights, find the index of the tallest one and print that index; the ship fires on it. Shooting the highest mountain every turn is enough, because only the tallest peak can reach the ship first. It is a linear search for the maximum (argmax) inside the game loop.
The problem
Your ship flies over eight mountains and loses altitude as it goes. It can fire on one mountain per turn, which lowers that mountain. If the ship ever touches a mountain, you lose; you win once every mountain has been flattened.
On each turn the game sends eight integers, the heights of mountains 0 to 7 (from 0 to 9). Your program must answer with a single line containing the index of the mountain to shoot. The game runs in an infinite loop, so your code reads and answers turn after turn until the game ends.
Approach, step by step
- Set up the game loopCodinGame bot puzzles never end on their own: wrap everything in while(true). Each iteration is one turn, with eight lines of input and exactly one line of output.
- Read all eight heightsRead the eight values every turn, even if you think you already know the answer. Skipping input leaves numbers in the buffer and shifts every following turn.
- Find the index of the maximumTrack the largest height and its index while reading. Start the maximum below any legal height (-1) so that a turn where every mountain is 0 still yields a valid index.
- Print the indexOutput the index, not the height. Make sure each answer ends with a newline and is flushed so the referee receives it this turn.
Complexity
O(8) per turnO(1)Each turn is a single scan of eight values, so the cost is constant. No state needs to be kept between turns because the input already reflects every previous shot.
Solution code
Bug fixed in this version: None of the three original blocks solves the current version of The Descent. The C code targets the legacy rules (ship coordinates as input, FIRE/HOLD as output) and is rejected by today's referee, which expects a mountain index. The Java and Python blocks are actually solutions to Mars Lander - Episode 1 (they read a surface and output rotation/thrust). Replacements for all three languages are provided.
The Descent – C
#include <stdio.h>
int main()
{
while (1) {
int best = 0, maxH = -1;
for (int i = 0; i < 8; i++) {
int h;
if (scanf("%d", &h) != 1)
return 0;
if (h > maxH) {
maxH = h;
best = i;
}
}
printf("%d\n", best);
fflush(stdout);
}
return 0;
}
The Descent – Java
import java.util.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
int best = 0, maxH = -1;
for (int i = 0; i < 8; i++) {
int h = in.nextInt();
if (h > maxH) {
maxH = h;
best = i;
}
}
System.out.println(best);
}
}
}
The Descent – Python 3
while True:
heights = [int(input()) for _ in range(8)]
print(heights.index(max(heights)))
Common mistakes
- Printing the height instead of the indexThe referee expects a number from 0 to 7. Printing the maximum height looks plausible in the logs but targets the wrong mountain and the ship crashes after a few turns.
- Not reading every input lineBreaking out of the read loop as soon as you spot a 9 leaves unread heights. On the next turn your program reads stale data and fires at random.
- Using an old FIRE/HOLD solutionMany tutorials still show the legacy version where you output FIRE or HOLD depending on the ship's X position. The current puzzle rejects that output immediately.
- Starting max at 0 with a strict comparisonIf max starts at 0 and you test h > max, a turn where all mountains are 0 leaves the index unset (or at its previous value). Start at -1 or initialise from the first height.
Going further
Why is the greedy choice safe? The ship keeps losing altitude while each shot only removes part of one mountain, so the first mountain the ship can collide with is always the tallest one. Shooting it every turn keeps the overall maximum height going down, and a shorter mountain can never become more dangerous than the one you are already attacking. Shooting anything else wastes a turn while the tallest peak gets closer. When two mountains share the maximum, either choice works: the other one will be the maximum on the next turn. That reasoning, "the binding constraint is always the extreme value", is the same one you meet later in scheduling problems and in Horse-racing Duals.
FAQ
What do I print each turn in The Descent?
A single integer from 0 to 7: the index of the mountain you want to shoot. Shooting the tallest one each turn solves every test.
Why does my FIRE/HOLD solution no longer work?
It was written for an older version of the puzzle. The current referee sends only the eight heights and expects an index, so FIRE or HOLD is an invalid action.
Do I need to remember previous turns?
No. The heights you receive already include the effect of your earlier shots, so each turn can be solved independently.