Skip to content

Commit acc61c0

Browse files
Onboarding & The Descent
1 parent 255fdb9 commit acc61c0

File tree

9 files changed

+176
-114
lines changed

9 files changed

+176
-114
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 21 deletions
This file was deleted.

puzzles/cpp/the-descent/README.md

Lines changed: 60 additions & 0 deletions
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+
```
Lines changed: 25 additions & 0 deletions
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+
}
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
1-
Here's a possible solution to the "Onboarding" challenge on CodinGame using Python:
1+
# Onboarding Puzzle
2+
3+
## Description
4+
5+
In this problem, you need to choose which enemy to shoot based on their distance from your ship. You can compare the distances of the two enemies and then shoot at the closest one.
6+
7+
## Algorithm
8+
9+
The solution uses a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read in the name and distance of two enemies using the `input()` function, and then compare their distances using an `if` statement. If the distance of the first enemy is less than the distance of the second enemy, we print the name of the first enemy. Otherwise, we print the name of the second enemy.
10+
11+
## Example Input/Output
12+
13+
**Input**
14+
15+
```
16+
Nobody
17+
Rock
18+
9999
19+
70
20+
```
21+
22+
**Output**
23+
24+
```
25+
Rock
26+
```
27+
28+
## Code Example
29+
30+
The following code example provides a solution to the Onboarding puzzle. It reads input from the standard input, compares the distances of two enemies, and prints the name of the closest enemy to the standard output.
231

332
```python
33+
import sys
34+
import math
35+
36+
# game loop
437
while True:
538
enemy_1 = input() # name of enemy 1
639
dist_1 = int(input()) # distance to enemy 1
740
enemy_2 = input() # name of enemy 2
841
dist_2 = int(input()) # distance to enemy 2
942

10-
# Determine which enemy is closer and print its name
43+
# Compare the distances of the two enemies
1144
if dist_1 < dist_2:
45+
# If enemy 1 is closer, shoot enemy 1
1246
print(enemy_1)
1347
else:
48+
# If enemy 2 is closer or at the same distance, shoot enemy 2
1449
print(enemy_2)
15-
```
1650

17-
In this solution, we use a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read the name and distance of two enemies, and then determine which one is closer based on their distances. Finally, we print the name of the closer enemy using `print()`.
51+
```
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
if __name__ == "__main__":
2-
# game loop
1+
if __name__ == '__main__':
32
while True:
4-
enemy1: str = input() # name of enemy 1
5-
distance1: int = int(input()) # distance to enemy 1
6-
enemy2: str = input() # name of enemy 2
7-
distance2: int = int(input()) # distance to enemy 2
3+
enemy_1 = input() # name of enemy 1
4+
dist_1 = int(input()) # distance to enemy 1
5+
enemy_2 = input() # name of enemy 2
6+
dist_2 = int(input()) # distance to enemy 2
87

9-
# Display enemy1 name when enemy1 is the closest, enemy2 otherwise
10-
if distance1 < distance2:
11-
print(enemy1)
8+
# Compare the distances of the two enemies
9+
if dist_1 < dist_2:
10+
# If enemy 1 is closer, shoot enemy 1
11+
print(enemy_1)
1212
else:
13-
print(enemy2)
13+
# If enemy 2 is closer or at the same distance, shoot enemy 2
14+
print(enemy_2)

puzzles/python3/the-descent/README.md

Lines changed: 26 additions & 22 deletions
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

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 15 additions & 17 deletions
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)