Skip to content

Commit 7e79ada

Browse files
Power of Thor 1
1 parent acc61c0 commit 7e79ada

File tree

4 files changed

+145
-34
lines changed

4 files changed

+145
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
1515
| :---: | :------: | :------: |
1616
| Onboarding 🛹 | [Python](./puzzles/python3/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding) | Variables, Input/Output, Conditions |
1717
| 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 |
18-
| 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 |
18+
| 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 |
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 |
2121
| ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art), [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) ★ | Strings |

puzzles/bash/power-of-thor1/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Power of Thor - Episode 1
2+
3+
## Description
4+
5+
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.
6+
7+
## Solution Overview
8+
9+
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.
10+
11+
## Example Input/Output
12+
13+
**Initialization input**
14+
15+
```
16+
31 4 5 4
17+
```
18+
19+
**Output for a game round**
20+
21+
```
22+
E
23+
```
24+
25+
## Code Example
26+
27+
```bash
28+
# Auto-generated code below aims at helping you parse
29+
# the standard input according to the problem statement.
30+
# ---
31+
# Hint: You can use the debug stream to print thorX and thorY if Thor seems not to follow your orders.
32+
33+
# lightX: the X position of the light of power
34+
# lightY: the Y position of the light of power
35+
# thorX: Thor's current X position
36+
# thorY: Thor's current Y position
37+
read -r lightX lightY thorX thorY
38+
39+
# game loop
40+
while true; do
41+
# remainingTurns: The remaining amount of turns Thor can move. Do not remove this line.
42+
read -r remainingTurns
43+
44+
# Calculate the direction
45+
direction=""
46+
47+
# Determine the vertical direction (N or S) and update position
48+
if [ "$thorY" -gt "$lightY" ]; then
49+
direction+="N"
50+
((thorY--))
51+
elif [ "$thorY" -lt "$lightY" ]; then
52+
direction+="S"
53+
((thorY++))
54+
fi
55+
56+
# Determine the horizontal direction (E or W) and update position
57+
if [ "$thorX" -gt "$lightX" ]; then
58+
direction+="W"
59+
((thorX--))
60+
elif [ "$thorX" -lt "$lightX" ]; then
61+
direction+="E"
62+
((thorX++))
63+
fi
64+
65+
# Output the direction
66+
echo "$direction"
67+
done
68+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Auto-generated code below aims at helping you parse
2+
# the standard input according to the problem statement.
3+
# ---
4+
# Hint: You can use the debug stream to print thorX and thorY if Thor seems not to follow your orders.
5+
6+
# lightX: the X position of the light of power
7+
# lightY: the Y position of the light of power
8+
# thorX: Thor's current X position
9+
# thorY: Thor's current Y position
10+
read -r lightX lightY thorX thorY
11+
12+
# game loop
13+
while true; do
14+
# remainingTurns: The remaining amount of turns Thor can move. Do not remove this line.
15+
read -r remainingTurns
16+
17+
# Calculate the direction
18+
direction=""
19+
20+
# Determine the vertical direction (N or S) and update position
21+
if [ "$thorY" -gt "$lightY" ]; then
22+
direction+="N"
23+
((thorY--))
24+
elif [ "$thorY" -lt "$lightY" ]; then
25+
direction+="S"
26+
((thorY++))
27+
fi
28+
29+
# Determine the horizontal direction (E or W) and update position
30+
if [ "$thorX" -gt "$lightX" ]; then
31+
direction+="W"
32+
((thorX--))
33+
elif [ "$thorX" -lt "$lightX" ]; then
34+
direction+="E"
35+
((thorX++))
36+
fi
37+
38+
# Output the direction
39+
echo "$direction"
40+
done

puzzles/python3/power-of-thor1/README.md

+36-33
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,52 @@
1-
# Power of Thor
1+
# Power of Thor - Episode 1
22

3-
This is a solution to the Power of Thor puzzle on [Codingame](https://www.codingame.com/training/easy/power-of-thor-episode-1).
4-
5-
## Problem Description
3+
## Description
64

75
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.
86

97
## Solution Overview
108

119
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.
1210

13-
## Code Example
11+
## Example Input/Output
1412

15-
```python
16-
# light_x: the X position of the light of power
17-
# light_y: the Y position of the light of power
18-
# initial_tx: Thor's starting X position
19-
# initial_ty: Thor's starting Y position
20-
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
13+
**Initialization input**
2114

22-
# game loop
23-
while True:
24-
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.
15+
```
16+
31 4 5 4
17+
```
2518

26-
move = ""
19+
**Output for a game round**
2720

28-
# Check the relative position of Thor and the light of power to determine the move direction
29-
if initial_ty > light_y:
30-
move += "N"
31-
initial_ty -= 1
32-
elif initial_ty < light_y:
33-
move += "S"
34-
initial_ty += 1
21+
```
22+
E
23+
```
3524

36-
if initial_tx > light_x:
37-
move += "W"
38-
initial_tx -= 1
39-
elif initial_tx < light_x:
40-
move += "E"
41-
initial_tx += 1
25+
## Code Example
4226

43-
print(move)
27+
```python
28+
light_x, light_y, thor_x, thor_y = map(int, input().split())
4429

30+
while True:
31+
remaining_turns = int(input())
32+
33+
direction = ""
34+
35+
# Determine the vertical direction (N or S) and update position
36+
if thor_y > light_y:
37+
direction += "N"
38+
thor_y -= 1
39+
elif thor_y < light_y:
40+
direction += "S"
41+
thor_y += 1
42+
43+
# Determine the horizontal direction (E or W) and update position
44+
if thor_x > light_x:
45+
direction += "W"
46+
thor_x -= 1
47+
elif thor_x < light_x:
48+
direction += "E"
49+
thor_x += 1
50+
51+
print(direction)
4552
```
46-
47-
## Conclusion
48-
49-
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.

0 commit comments

Comments
 (0)