To solve the CodinGame puzzle "The Descent" with an optimal and concise solution, the goal is to identify and shoot the highest mountain in each game turn. Below is an explanation of the optimal approach and a compact implementation in Python.
Optimal Approach
-
Understand the Input and Output:
- Each turn, the heights of 8 mountains are provided as input.
- The task is to output the index of the mountain with the maximum height.
-
Algorithm:
- Use a loop to read the heights of the mountains.
- Track the maximum height and its corresponding index during the loop.
- Output the index of the mountain with the maximum height.
-
Efficiency:
- The solution should process the input in a single loop for optimal performance.
- Avoid unnecessary data structures or operations to minimize code size and execution time.
Python Implementation
Here is a compact and efficient implementation of the solution:
while True:
max_h, index = -1, 0
for i in range(8):
h = int(input())
if h > max_h:
max_h, index = h, i
print(index)
Explanation:
max_h is initialized to -1 to ensure any mountain height (0–9) can replace it.
- The loop iterates through the 8 mountains, comparing each height (
h) with max_h.
- If a higher mountain is found,
max_h is updated, and its index is stored in index.
- After the loop, the index of the highest mountain is printed.
Key Features of the Solution
- Single Loop: The solution uses a single loop to process the heights, ensuring efficiency.
- Minimal Variables: Only two variables (
max_h and index) are used to track the highest mountain.
- Compact Code: The implementation is concise, adhering to the constraints of code size optimization.
This solution is optimal for the problem and adheres to the constraints provided by the CodinGame platform. It ensures correctness and efficiency while maintaining a minimal code size.