Skip to content

Commit de138f7

Browse files
Power of Thor 1
1 parent acc61c0 commit de138f7

File tree

4 files changed

+145
-28
lines changed

4 files changed

+145
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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 |
Lines changed: 68 additions & 0 deletions
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+
```
Lines changed: 40 additions & 0 deletions
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

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1-
# Power of Thor
1+
# Power of Thor - Episode 1
22

33
This is a solution to the Power of Thor puzzle on [Codingame](https://www.codingame.com/training/easy/power-of-thor-episode-1).
44

5-
## Problem Description
5+
## Description
66

77
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.
88

99
## Solution Overview
1010

1111
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.
1212

13-
## Code Example
13+
## Example Input/Output
1414

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()]
15+
**Initialization input**
2116

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

26-
move = ""
21+
**Output for a game round**
2722

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
23+
```
24+
E
25+
```
3526

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
27+
## Code Example
4228

43-
print(move)
29+
```python
30+
light_x, light_y, thor_x, thor_y = map(int, input().split())
4431

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

4756
## Conclusion

0 commit comments

Comments
 (0)