From acc61c0e917dce581b54fd5c3292720b62cd4035 Mon Sep 17 00:00:00 2001 From: "Charles-A. Francisco" Date: Mon, 14 Oct 2024 15:40:36 -0700 Subject: [PATCH] Onboarding & The Descent --- README.md | 2 +- puzzles/cpp/the-descent.cpp | 21 ------- puzzles/cpp/the-descent/README.md | 60 +++++++++++++++++++ puzzles/cpp/the-descent/the_descent.cpp | 25 ++++++++ puzzles/python3/onboarding/README.md | 42 +++++++++++-- puzzles/python3/onboarding/onboarding.py | 21 +++---- puzzles/python3/the-descent/README.md | 48 ++++++++------- .../python3/the-descent/test_the_descent.py | 39 ------------ puzzles/python3/the-descent/the_descent.py | 32 +++++----- 9 files changed, 176 insertions(+), 114 deletions(-) delete mode 100644 puzzles/cpp/the-descent.cpp create mode 100644 puzzles/cpp/the-descent/README.md create mode 100644 puzzles/cpp/the-descent/the_descent.cpp delete mode 100644 puzzles/python3/the-descent/test_the_descent.py diff --git a/README.md b/README.md index 6f52ac9..4578556 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin | Title | Solution(s) | Topic(s) | | :---: | :------: | :------: | | Onboarding 🛹 | [Python](./puzzles/python3/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding) | Variables, Input/Output, Conditions | -| 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 | +| 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 | | 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 | | Temperatures ❄️ | [Python](./puzzles/python3/temperatures) ★, [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) | Conditions, Loops, Arrays | | 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 | diff --git a/puzzles/cpp/the-descent.cpp b/puzzles/cpp/the-descent.cpp deleted file mode 100644 index 9584128..0000000 --- a/puzzles/cpp/the-descent.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -using namespace std; - -int main() { - // game loop - while (true) { - int max = -1; - int indexMax = 0; - for (int i = 0; i < 8; i++) { - int height; // represents the height of one mountain. - cin >> height; cin.ignore(); - if (height > max) { - max = height; - indexMax = i; - } - } - - cout << indexMax << endl; // The index of the mountain to fire on. - } -} diff --git a/puzzles/cpp/the-descent/README.md b/puzzles/cpp/the-descent/README.md new file mode 100644 index 0000000..6588c01 --- /dev/null +++ b/puzzles/cpp/the-descent/README.md @@ -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 +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; + } +} +``` diff --git a/puzzles/cpp/the-descent/the_descent.cpp b/puzzles/cpp/the-descent/the_descent.cpp new file mode 100644 index 0000000..5e9f46c --- /dev/null +++ b/puzzles/cpp/the-descent/the_descent.cpp @@ -0,0 +1,25 @@ +#include +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; + } +} diff --git a/puzzles/python3/onboarding/README.md b/puzzles/python3/onboarding/README.md index 0ac7fea..5649b91 100644 --- a/puzzles/python3/onboarding/README.md +++ b/puzzles/python3/onboarding/README.md @@ -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()`. +``` diff --git a/puzzles/python3/onboarding/onboarding.py b/puzzles/python3/onboarding/onboarding.py index 86d082e..416c785 100644 --- a/puzzles/python3/onboarding/onboarding.py +++ b/puzzles/python3/onboarding/onboarding.py @@ -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) diff --git a/puzzles/python3/the-descent/README.md b/puzzles/python3/the-descent/README.md index d8e6b61..b7c94ab 100644 --- a/puzzles/python3/the-descent/README.md +++ b/puzzles/python3/the-descent/README.md @@ -2,21 +2,28 @@ ## Description -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. +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. -## Approach +## Algorithm -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. +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 -Let's consider the following array of mountains: +**Input for one game turn** ``` -[9, 8, 6, 7, 3, 5, 4, 1, 2] +9 +8 +7 +6 +5 +4 +3 +2 ``` -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: +**Output for one game turn** ``` 0 @@ -25,22 +32,19 @@ Here, the tallest mountain is the one with a height of `9`. The index of this mo ## Code Example ```python -import sys - -# Game loop while True: - heights = [] - - # Read the heights of the mountains - for _ in range(8): - mountain_height = int(input()) # Height of the mountain - heights.append(mountain_height) - - # Find the index of the highest mountain - max_height = max(heights) - max_height_index = heights.index(max_height) - - # Output the index of the highest mountain to shoot - print(max_height_index) + 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) ``` diff --git a/puzzles/python3/the-descent/test_the_descent.py b/puzzles/python3/the-descent/test_the_descent.py deleted file mode 100644 index 81b1fbe..0000000 --- a/puzzles/python3/the-descent/test_the_descent.py +++ /dev/null @@ -1,39 +0,0 @@ -import unittest - -from the_descent import solve - - -class TestSolve(unittest.TestCase): - def test_single_mountain(self): - mountain_heights = [5] - expected = 0 - result = solve(mountain_heights) - self.assertEqual(result, expected) - - def test_equal_heights(self): - mountain_heights = [3, 3, 3, 3] - expected = 0 - result = solve(mountain_heights) - self.assertEqual(result, expected) - - def test_increasing_heights(self): - mountain_heights = [1, 2, 3, 4, 5] - expected = 4 - result = solve(mountain_heights) - self.assertEqual(result, expected) - - def test_decreasing_heights(self): - mountain_heights = [5, 4, 3, 2, 1] - expected = 0 - result = solve(mountain_heights) - self.assertEqual(result, expected) - - def test_random_heights(self): - mountain_heights = [3, 5, 2, 6, 1, 4] - expected = 3 - result = solve(mountain_heights) - self.assertEqual(result, expected) - - -if __name__ == "__main__": - unittest.main() diff --git a/puzzles/python3/the-descent/the_descent.py b/puzzles/python3/the-descent/the_descent.py index e37ff32..3207e60 100644 --- a/puzzles/python3/the-descent/the_descent.py +++ b/puzzles/python3/the-descent/the_descent.py @@ -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)