diff --git a/README.md b/README.md index 4578556..6e0aac4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin | :---: | :------: | :------: | | 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/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 | +| Power of Thor 1 ⚡ | [Python](./puzzles/python3/power-of-thor1) ★, [Kotlin](./puzzles/kotlin/src/power-of-thor1), [TypeScript](./puzzles/ts/power-of-thor1), [Bash](./puzzles/bash/power-of-thor1), [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 | | ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art), [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) ★ | Strings | diff --git a/puzzles/bash/power-of-thor1/README.md b/puzzles/bash/power-of-thor1/README.md new file mode 100644 index 0000000..6b63e68 --- /dev/null +++ b/puzzles/bash/power-of-thor1/README.md @@ -0,0 +1,68 @@ +# Power of Thor - Episode 1 + +## Description + +In this puzzle, Thor is stranded on a rectangular grid and needs to reach a lightning bolt that is located at a specific position on the grid. The position of Thor and the lightning bolt are given as input to the program. Thor can move in four directions: North, South, East, and West. For each move, the program needs to output the direction in which Thor should move to get closer to the lightning bolt. + +## Solution Overview + +The solution uses a loop to iterate over the possible moves of Thor. At each iteration, the program calculates the direction in which Thor should move based on his current position and the position of the lightning bolt. The program then outputs the direction in which Thor should move and updates his position accordingly. + +## Example Input/Output + +**Initialization input** + +``` +31 4 5 4 +``` + +**Output for a game round** + +``` +E +``` + +## Code Example + +```bash +# Auto-generated code below aims at helping you parse +# the standard input according to the problem statement. +# --- +# Hint: You can use the debug stream to print thorX and thorY if Thor seems not to follow your orders. + +# lightX: the X position of the light of power +# lightY: the Y position of the light of power +# thorX: Thor's current X position +# thorY: Thor's current Y position +read -r lightX lightY thorX thorY + +# game loop +while true; do + # remainingTurns: The remaining amount of turns Thor can move. Do not remove this line. + read -r remainingTurns + + # Calculate the direction + direction="" + + # Determine the vertical direction (N or S) and update position + if [ "$thorY" -gt "$lightY" ]; then + direction+="N" + ((thorY--)) + elif [ "$thorY" -lt "$lightY" ]; then + direction+="S" + ((thorY++)) + fi + + # Determine the horizontal direction (E or W) and update position + if [ "$thorX" -gt "$lightX" ]; then + direction+="W" + ((thorX--)) + elif [ "$thorX" -lt "$lightX" ]; then + direction+="E" + ((thorX++)) + fi + + # Output the direction + echo "$direction" +done +``` diff --git a/puzzles/bash/power-of-thor1/power_of_thor1.sh b/puzzles/bash/power-of-thor1/power_of_thor1.sh new file mode 100644 index 0000000..6013ddf --- /dev/null +++ b/puzzles/bash/power-of-thor1/power_of_thor1.sh @@ -0,0 +1,40 @@ +# Auto-generated code below aims at helping you parse +# the standard input according to the problem statement. +# --- +# Hint: You can use the debug stream to print thorX and thorY if Thor seems not to follow your orders. + +# lightX: the X position of the light of power +# lightY: the Y position of the light of power +# thorX: Thor's current X position +# thorY: Thor's current Y position +read -r lightX lightY thorX thorY + +# game loop +while true; do + # remainingTurns: The remaining amount of turns Thor can move. Do not remove this line. + read -r remainingTurns + + # Calculate the direction + direction="" + + # Determine the vertical direction (N or S) and update position + if [ "$thorY" -gt "$lightY" ]; then + direction+="N" + ((thorY--)) + elif [ "$thorY" -lt "$lightY" ]; then + direction+="S" + ((thorY++)) + fi + + # Determine the horizontal direction (E or W) and update position + if [ "$thorX" -gt "$lightX" ]; then + direction+="W" + ((thorX--)) + elif [ "$thorX" -lt "$lightX" ]; then + direction+="E" + ((thorX++)) + fi + + # Output the direction + echo "$direction" +done diff --git a/puzzles/python3/power-of-thor1/README.md b/puzzles/python3/power-of-thor1/README.md index 714d6a1..0abfa21 100644 --- a/puzzles/python3/power-of-thor1/README.md +++ b/puzzles/python3/power-of-thor1/README.md @@ -1,8 +1,6 @@ -# Power of Thor +# Power of Thor - Episode 1 -This is a solution to the Power of Thor puzzle on [Codingame](https://www.codingame.com/training/easy/power-of-thor-episode-1). - -## Problem Description +## Description In this puzzle, Thor is stranded on a rectangular grid and needs to reach a lightning bolt that is located at a specific position on the grid. The position of Thor and the lightning bolt are given as input to the program. Thor can move in four directions: North, South, East, and West. For each move, the program needs to output the direction in which Thor should move to get closer to the lightning bolt. @@ -10,40 +8,45 @@ In this puzzle, Thor is stranded on a rectangular grid and needs to reach a ligh The solution uses a loop to iterate over the possible moves of Thor. At each iteration, the program calculates the direction in which Thor should move based on his current position and the position of the lightning bolt. The program then outputs the direction in which Thor should move and updates his position accordingly. -## Code Example +## Example Input/Output -```python -# light_x: the X position of the light of power -# light_y: the Y position of the light of power -# initial_tx: Thor's starting X position -# initial_ty: Thor's starting Y position -light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()] +**Initialization input** -# game loop -while True: - remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line. +``` +31 4 5 4 +``` - move = "" +**Output for a game round** - # Check the relative position of Thor and the light of power to determine the move direction - if initial_ty > light_y: - move += "N" - initial_ty -= 1 - elif initial_ty < light_y: - move += "S" - initial_ty += 1 +``` +E +``` - if initial_tx > light_x: - move += "W" - initial_tx -= 1 - elif initial_tx < light_x: - move += "E" - initial_tx += 1 +## Code Example - print(move) +```python +light_x, light_y, thor_x, thor_y = map(int, input().split()) +while True: + remaining_turns = int(input()) + + direction = "" + + # Determine the vertical direction (N or S) and update position + if thor_y > light_y: + direction += "N" + thor_y -= 1 + elif thor_y < light_y: + direction += "S" + thor_y += 1 + + # Determine the horizontal direction (E or W) and update position + if thor_x > light_x: + direction += "W" + thor_x -= 1 + elif thor_x < light_x: + direction += "E" + thor_x += 1 + + print(direction) ``` - -## Conclusion - -This solution demonstrates how to solve the Power of Thor puzzle on Codingame. The program reads in input values from standard input, enters a loop to calculate the direction in which Thor should move, and outputs the direction in which he should move. This solution can be used as a starting point to solve other puzzles on Codingame or similar platforms.