Skip to content

Commit 63c9ba5

Browse files
The Descent
1 parent 4a9eebf commit 63c9ba5

File tree

7 files changed

+127
-100
lines changed

7 files changed

+127
-100
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
1414
| Title | Solution(s) | Topic(s) |
1515
| :---: | :------: | :------: |
1616
| Onboarding 🛹 | [Python](./puzzles/python3/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding) | Variables, Input/Output, Conditions |
17-
| The Descent 🌄 | [Python](./puzzles/python3/the-descent) ★, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C](./puzzles/c/the-descent) | Conditions, Loops |
17+
| The Descent 🌄 | [Python](./puzzles/python3/the-descent) ★, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C++](./puzzles/cpp/the-descent) | Conditions, Loops |
1818
| Power of Thor 1 ⚡ | [Python](./puzzles/python3/power-of-thor1) ★, [Kotlin](./puzzles/kotlin/src/power-of-thor1), [TypeScript](./puzzles/ts/power-of-thor1), [C++](./puzzles/cpp/power-of-thor1.cpp), [Swift](./puzzles/swift/power-of-thor1) | Input/Output, Conditions |
1919
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures) ★, [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) | Conditions, Loops, Arrays |
2020
| Mars Lander 1 🚀 | [Python](./puzzles/python3/mars-lander1), [Kotlin](./puzzles/kotlin/src/mars-lander1), [TypeScript](./puzzles/ts/mars-lander1) ★, [C++](./puzzles/cpp/mars-lander1.cpp) | Conditions, Loops |

puzzles/cpp/the-descent.cpp

-21
This file was deleted.

puzzles/cpp/the-descent/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# The Descent
2+
3+
## Description
4+
5+
The goal of the puzzle is to destroy the mountains by firing at the highest one at the start of each game turn. The heights of the mountains are given as input, and the output should be the index of the mountain to fire at. The game is won if all the mountains are destroyed, and lost if the ship crashes into a mountain.
6+
7+
## Algorithm
8+
9+
The following code snippet is a game loop that continuously reads the heights of 8 mountains and outputs the index of the highest mountain to "shoot" at. It does this by iterating through the mountain heights, keeping track of the highest height and its corresponding index, and then printing the index of the highest mountain. The loop repeats indefinitely.
10+
11+
## Example Input/Output
12+
13+
**Input**
14+
15+
```
16+
9
17+
8
18+
7
19+
6
20+
5
21+
4
22+
3
23+
2
24+
```
25+
26+
**Output**
27+
28+
```
29+
0
30+
```
31+
32+
## Code Example
33+
34+
```cpp
35+
#include <iostream>
36+
using namespace std;
37+
38+
int main() {
39+
// Game loop
40+
while (true) {
41+
int highestIndex = 0;
42+
int highestHeight = -1;
43+
44+
// Read the heights of the mountains and determine the highest
45+
for (int i = 0; i < 8; i++) {
46+
int mountainHeight;
47+
cin >> mountainHeight;
48+
49+
// Check if this mountain is the highest so far
50+
if (mountainHeight > highestHeight) {
51+
highestHeight = mountainHeight;
52+
highestIndex = i;
53+
}
54+
}
55+
56+
// Output the index of the highest mountain to shoot
57+
cout << highestIndex << endl;
58+
}
59+
}
60+
```
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
// Game loop
6+
while (true) {
7+
int highestIndex = 0;
8+
int highestHeight = -1;
9+
10+
// Read the heights of the mountains and determine the highest
11+
for (int i = 0; i < 8; i++) {
12+
int mountainHeight;
13+
cin >> mountainHeight;
14+
15+
// Check if this mountain is the highest so far
16+
if (mountainHeight > highestHeight) {
17+
highestHeight = mountainHeight;
18+
highestIndex = i;
19+
}
20+
}
21+
22+
// Output the index of the highest mountain to shoot
23+
cout << highestIndex << endl;
24+
}
25+
}

puzzles/python3/the-descent/README.md

+26-22
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
## Description
44

5-
In "The Descent" puzzle, you are given an array of integers representing the heights of a set of mountains. Your goal is to determine which mountain is the tallest and shoot it down by printing its index to the console.
5+
The goal of the puzzle is to destroy the mountains by firing at the highest one at the start of each game turn. The heights of the mountains are given as input, and the output should be the index of the mountain to fire at. The game is won if all the mountains are destroyed, and lost if the ship crashes into a mountain.
66

7-
## Approach
7+
## Algorithm
88

9-
The approach for solving "The Descent" puzzle is simple. You iterate over the array of mountains and keep track of the tallest mountain seen so far. Once you have iterated over all the mountains, you print the index of the tallest mountain to the console.
9+
The following code snippet is a game loop that continuously reads the heights of 8 mountains and outputs the index of the highest mountain to "shoot" at. It does this by iterating through the mountain heights, keeping track of the highest height and its corresponding index, and then printing the index of the highest mountain. The loop repeats indefinitely.
1010

1111
## Example Input/Output
1212

13-
Let's consider the following array of mountains:
13+
**Input for one game turn**
1414

1515
```
16-
[9, 8, 6, 7, 3, 5, 4, 1, 2]
16+
9
17+
8
18+
7
19+
6
20+
5
21+
4
22+
3
23+
2
1724
```
1825

19-
Here, the tallest mountain is the one with a height of `9`. The index of this mountain in the array is `0`. Therefore, the output of the program should be:
26+
**Output for one game turn**
2027

2128
```
2229
0
@@ -25,22 +32,19 @@ Here, the tallest mountain is the one with a height of `9`. The index of this mo
2532
## Code Example
2633

2734
```python
28-
import sys
29-
30-
# Game loop
3135
while True:
32-
heights = []
33-
34-
# Read the heights of the mountains
35-
for _ in range(8):
36-
mountain_height = int(input()) # Height of the mountain
37-
heights.append(mountain_height)
38-
39-
# Find the index of the highest mountain
40-
max_height = max(heights)
41-
max_height_index = heights.index(max_height)
42-
43-
# Output the index of the highest mountain to shoot
44-
print(max_height_index)
36+
highest_index = 0
37+
highest_height = -1
38+
39+
# Read the heights of the mountains and determine the highest
40+
for i in range(8):
41+
mountain_height = int(input())
42+
43+
# Check if this mountain is the highest so far
44+
if mountain_height > highest_height:
45+
highest_height = mountain_height
46+
highest_index = i
4547

48+
# Output the index of the highest mountain to shoot
49+
print(highest_index)
4650
```

puzzles/python3/the-descent/test_the_descent.py

-39
This file was deleted.
+15-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
from typing import List
2-
3-
4-
def solve(mountain_heights: List[int]) -> int:
5-
index_to_fire = 0
6-
max_mountain_height = -1
7-
for index, mountain_height in enumerate(mountain_heights):
8-
if mountain_height > max_mountain_height:
9-
max_mountain_height = mountain_height
10-
index_to_fire = index
11-
return index_to_fire
12-
13-
14-
if __name__ == "__main__":
15-
while True:
16-
mountain_heights = [int(input()) for _ in range(8)]
17-
print(solve(mountain_heights))
1+
while True:
2+
highest_index = 0
3+
highest_height = -1
4+
5+
# Read the heights of the mountains and determine the highest
6+
for i in range(8):
7+
mountain_height = int(input())
8+
9+
# Check if this mountain is the highest so far
10+
if mountain_height > highest_height:
11+
highest_height = mountain_height
12+
highest_index = i
13+
14+
# Output the index of the highest mountain to shoot
15+
print(highest_index)

0 commit comments

Comments
 (0)