-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
255fdb9
commit acc61c0
Showing
9 changed files
with
176 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# The Descent | ||
|
||
## Description | ||
|
||
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. | ||
|
||
## Algorithm | ||
|
||
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. | ||
|
||
## Example Input/Output | ||
|
||
**Input** | ||
|
||
``` | ||
9 | ||
8 | ||
7 | ||
6 | ||
5 | ||
4 | ||
3 | ||
2 | ||
``` | ||
|
||
**Output** | ||
|
||
``` | ||
0 | ||
``` | ||
|
||
## Code Example | ||
|
||
```cpp | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() { | ||
// Game loop | ||
while (true) { | ||
int highestIndex = 0; | ||
int highestHeight = -1; | ||
|
||
// Read the heights of the mountains and determine the highest | ||
for (int i = 0; i < 8; i++) { | ||
int mountainHeight; | ||
cin >> mountainHeight; | ||
|
||
// Check if this mountain is the highest so far | ||
if (mountainHeight > highestHeight) { | ||
highestHeight = mountainHeight; | ||
highestIndex = i; | ||
} | ||
} | ||
|
||
// Output the index of the highest mountain to shoot | ||
cout << highestIndex << endl; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() { | ||
// Game loop | ||
while (true) { | ||
int highestIndex = 0; | ||
int highestHeight = -1; | ||
|
||
// Read the heights of the mountains and determine the highest | ||
for (int i = 0; i < 8; i++) { | ||
int mountainHeight; | ||
cin >> mountainHeight; | ||
|
||
// Check if this mountain is the highest so far | ||
if (mountainHeight > highestHeight) { | ||
highestHeight = mountainHeight; | ||
highestIndex = i; | ||
} | ||
} | ||
|
||
// Output the index of the highest mountain to shoot | ||
cout << highestIndex << endl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
Here's a possible solution to the "Onboarding" challenge on CodinGame using Python: | ||
# Onboarding Puzzle | ||
|
||
## Description | ||
|
||
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. | ||
|
||
## Algorithm | ||
|
||
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. | ||
|
||
## Example Input/Output | ||
|
||
**Input** | ||
|
||
``` | ||
Nobody | ||
Rock | ||
9999 | ||
70 | ||
``` | ||
|
||
**Output** | ||
|
||
``` | ||
Rock | ||
``` | ||
|
||
## Code Example | ||
|
||
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. | ||
|
||
```python | ||
import sys | ||
import math | ||
|
||
# game loop | ||
while True: | ||
enemy_1 = input() # name of enemy 1 | ||
dist_1 = int(input()) # distance to enemy 1 | ||
enemy_2 = input() # name of enemy 2 | ||
dist_2 = int(input()) # distance to enemy 2 | ||
|
||
# Determine which enemy is closer and print its name | ||
# Compare the distances of the two enemies | ||
if dist_1 < dist_2: | ||
# If enemy 1 is closer, shoot enemy 1 | ||
print(enemy_1) | ||
else: | ||
# If enemy 2 is closer or at the same distance, shoot enemy 2 | ||
print(enemy_2) | ||
``` | ||
|
||
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()`. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
if __name__ == "__main__": | ||
# game loop | ||
if __name__ == '__main__': | ||
while True: | ||
enemy1: str = input() # name of enemy 1 | ||
distance1: int = int(input()) # distance to enemy 1 | ||
enemy2: str = input() # name of enemy 2 | ||
distance2: int = int(input()) # distance to enemy 2 | ||
enemy_1 = input() # name of enemy 1 | ||
dist_1 = int(input()) # distance to enemy 1 | ||
enemy_2 = input() # name of enemy 2 | ||
dist_2 = int(input()) # distance to enemy 2 | ||
|
||
# Display enemy1 name when enemy1 is the closest, enemy2 otherwise | ||
if distance1 < distance2: | ||
print(enemy1) | ||
# Compare the distances of the two enemies | ||
if dist_1 < dist_2: | ||
# If enemy 1 is closer, shoot enemy 1 | ||
print(enemy_1) | ||
else: | ||
print(enemy2) | ||
# If enemy 2 is closer or at the same distance, shoot enemy 2 | ||
print(enemy_2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
from typing import List | ||
|
||
|
||
def solve(mountain_heights: List[int]) -> int: | ||
index_to_fire = 0 | ||
max_mountain_height = -1 | ||
for index, mountain_height in enumerate(mountain_heights): | ||
if mountain_height > max_mountain_height: | ||
max_mountain_height = mountain_height | ||
index_to_fire = index | ||
return index_to_fire | ||
|
||
|
||
if __name__ == "__main__": | ||
while True: | ||
mountain_heights = [int(input()) for _ in range(8)] | ||
print(solve(mountain_heights)) | ||
while True: | ||
highest_index = 0 | ||
highest_height = -1 | ||
|
||
# Read the heights of the mountains and determine the highest | ||
for i in range(8): | ||
mountain_height = int(input()) | ||
|
||
# Check if this mountain is the highest so far | ||
if mountain_height > highest_height: | ||
highest_height = mountain_height | ||
highest_index = i | ||
|
||
# Output the index of the highest mountain to shoot | ||
print(highest_index) |