Skip to content

Commit 9b689d9

Browse files
Descent, Thor 1, Temperatures, Mars Lander 1, ASCII Art, MIME Type, Defibrillators & Duals
1 parent 5c3fa24 commit 9b689d9

File tree

33 files changed

+1164
-362
lines changed

33 files changed

+1164
-362
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Solutions to CodinGame Puzzles
22

33
<p align="center">
4-
<a href="https://www.codingame.com/"><img src="images/codingame-1.svg" width="200" height="200" /></a>
4+
<a href="https://www.codingame.com/"><img src="https://cdn.brandfetch.io/idIfiwZMnL/w/820/h/177/theme/dark/logo.png?k=bfHSJFAPEG" /></a>
55
</p>
66

77
The "Solutions to CodinGame Puzzles" project is a collection of answers to coding problems from CodinGame. CodinGame is a website where people can practice coding by solving puzzles. In this project, people have written solutions to these puzzles using languages like Python, C++, and Java. Each solution is saved in a file named after the puzzle it solves. The puzzles range from easy to hard and cover different coding topics like variables, conditions, and arrays. The goal of the project is to help developers get better at problem-solving and learn different ways of coding. It also includes explanations to help people understand the solutions better.
@@ -14,15 +14,15 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
1414
| Title | Solution(s) | Topic(s) |
1515
| :---: | :------: | :------: |
1616
| Onboarding 🛹 | [Python](./puzzles/python3/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding) | Variables, Input/Output, Conditions |
17-
| The Descent 🌄 | [Python](./puzzles/python3/the-descent) &starf;, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C](./puzzles/c/the-descent) | Conditions, Loops |
18-
| Power of Thor 1 ⚡ | [Python](./puzzles/python3/power-of-thor1) &starf;, [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 |
19-
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures) &starf;, [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) | Conditions, Loops, Arrays |
20-
| Mars Lander 1 🚀 | [Python](./puzzles/python3/mars-lander1), [Kotlin](./puzzles/kotlin/src/mars-lander1), [TypeScript](./puzzles/ts/mars-lander1) &starf;, [C++](./puzzles/cpp/mars-lander1.cpp) | Conditions, Loops |
21-
| ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art), [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) &starf; | Strings |
17+
| The Descent 🌄 | [Python](./puzzles/python3/the-descent) &starf;, [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) &starf;, [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 |
19+
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures), [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) &starf; | Conditions, Loops, Arrays |
20+
| Mars Lander 1 🚀 | [Python](./puzzles/python3/mars-lander1), [Kotlin](./puzzles/kotlin/src/mars-lander1), [TypeScript](./puzzles/ts/mars-lander1) &starf;, [C++](./puzzles/cpp/mars-lander1.cpp), [C#](./puzzles/cs/mars-lander1) | Conditions, Loops |
21+
| ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art) &starf;, [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) | Strings |
2222
| Unary 1️⃣ | [Python](./puzzles/python3/unary), [TypeScript](./puzzles/ts/unary) &starf;, [Haskell](./puzzles/haskell/unary), [C#](./puzzles/cs/unary) | Strings, Encoding |
2323
| MIME Type 🎵 | [Python](./puzzles/python3/mime-type) &starf;, [Kotlin](./puzzles/kotlin/src/mime-type), [TypeScript](./puzzles/ts/mime-type), [C#](./puzzles/cs/mime-type) | Strings, Hash Tables |
24-
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) &starf;, [Kotlin](./puzzles/kotlin/src/defibrillators), [TypeScript](./puzzles/ts/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
25-
| Racing Duals 🏁 | [Python](./puzzles/python3/horse-racing-duals) &starf;, [Kotlin](./puzzles/kotlin/src/horse-racing-duals), [JavaScript](./puzzles/js/horse-racing-duals), [Ruby](./puzzles/ruby/horse-racing-duals) | Arrays, Sorting |
24+
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) &starf;, [Kotlin](./puzzles/kotlin/src/defibrillators), [JavaScript](./puzzles/js/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
25+
| Horse-racing Duals 🏁 | [Python](./puzzles/python3/horse-racing-duals) &starf;, [Kotlin](./puzzles/kotlin/src/horse-racing-duals), [JavaScript](./puzzles/js/horse-racing-duals) | Arrays, Sorting |
2626

2727
### Medium Puzzles
2828
| Title | Solution(s) | Topic(s) |

images/codingame_logo.png

13.1 KB
Loading
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/cpp/the-descent.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

puzzles/cpp/the-descent/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# The Descent
2+
3+
## Description
4+
5+
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.
6+
7+
## Algorithm
8+
9+
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.
10+
11+
## Example Input/Output
12+
13+
**Input**
14+
15+
```
16+
9
17+
8
18+
7
19+
6
20+
5
21+
4
22+
3
23+
2
24+
```
25+
26+
**Output**
27+
28+
```
29+
0
30+
```
31+
32+
## Code Example
33+
34+
```cpp
35+
#include <iostream>
36+
using namespace std;
37+
38+
int main() {
39+
// Game loop
40+
while (true) {
41+
int highestIndex = 0;
42+
int highestHeight = -1;
43+
44+
// Read the heights of the mountains and determine the highest
45+
for (int i = 0; i < 8; i++) {
46+
int mountainHeight;
47+
cin >> mountainHeight;
48+
49+
// Check if this mountain is the highest so far
50+
if (mountainHeight > highestHeight) {
51+
highestHeight = mountainHeight;
52+
highestIndex = i;
53+
}
54+
}
55+
56+
// Output the index of the highest mountain to shoot
57+
cout << highestIndex << endl;
58+
}
59+
}
60+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
// Game loop
6+
while (true) {
7+
int highestIndex = 0;
8+
int highestHeight = -1;
9+
10+
// Read the heights of the mountains and determine the highest
11+
for (int i = 0; i < 8; i++) {
12+
int mountainHeight;
13+
cin >> mountainHeight;
14+
15+
// Check if this mountain is the highest so far
16+
if (mountainHeight > highestHeight) {
17+
highestHeight = mountainHeight;
18+
highestIndex = i;
19+
}
20+
}
21+
22+
// Output the index of the highest mountain to shoot
23+
cout << highestIndex << endl;
24+
}
25+
}

puzzles/cs/mars-lander1/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Mars Lander - Episode 1
2+
3+
## Description
4+
5+
This puzzle teaches you how to compare values using a simple condition. It's an introduction to the "Mars Lander" series of puzzles and requires only a simple condition to solve.
6+
7+
## Solution Overview
8+
9+
The algorithm starts by reading the surface data. Then, it enters an infinite loop, continually reading the current state of the spacecraft and calculating the new thrust power based on the vertical speed. The thrust power is adjusted by one unit if the vertical speed exceeds the maximum allowed, and the new power value is adjusted to be within the valid range. The thrust power is adjusted by one unit if the vertical speed is below the minimum allowed, and the new power value is adjusted to be within the valid range. Finally, the new thrust power and rotation angle are printed to the console.
10+
11+
## Code Example
12+
13+
```csharp
14+
using System;
15+
16+
class Player
17+
{
18+
// Define constants for better readability and maintainability
19+
const int MAX_VERTICAL_SPEED = -40; // Maximum allowed vertical speed before adjusting thrust
20+
const int MAX_THRUST_POWER = 4; // Maximum thrust power
21+
const int MIN_THRUST_POWER = 0; // Minimum thrust power
22+
23+
static void Main(string[] args)
24+
{
25+
string[] inputs;
26+
int surfaceN = int.Parse(Console.ReadLine()); // the number of points used to draw the surface of Mars.
27+
for (int i = 0; i < surfaceN; i++)
28+
{
29+
inputs = Console.ReadLine().Split(' ');
30+
int landX = int.Parse(inputs[0]); // X coordinate of a surface point. (0 to 6999)
31+
int landY = int.Parse(inputs[1]); // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
32+
}
33+
34+
// game loop
35+
while (true)
36+
{
37+
inputs = Console.ReadLine().Split(' ');
38+
int hSpeed = int.Parse(inputs[2]); // the horizontal speed (in m/s), can be negative.
39+
int vSpeed = int.Parse(inputs[3]); // the vertical speed (in m/s), can be negative.
40+
int power = int.Parse(inputs[6]); // the thrust power (0 to 4).
41+
42+
// Adjust thrust power based on the vertical speed using constants
43+
if (vSpeed <= MAX_VERTICAL_SPEED && power < MAX_THRUST_POWER) // Increase thrust if falling too fast
44+
{
45+
power++;
46+
}
47+
else if (vSpeed > MAX_VERTICAL_SPEED && power > MIN_THRUST_POWER) // Decrease thrust if falling slowly
48+
{
49+
power--;
50+
}
51+
52+
// Output the desired rotation and power (rotation is always 0 for this level)
53+
Console.WriteLine("0 " + power);
54+
}
55+
}
56+
}
57+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
class Player
4+
{
5+
// Define constants for better readability and maintainability
6+
const int MAX_VERTICAL_SPEED = -40; // Maximum allowed vertical speed before adjusting thrust
7+
const int MAX_THRUST_POWER = 4; // Maximum thrust power
8+
const int MIN_THRUST_POWER = 0; // Minimum thrust power
9+
10+
static void Main(string[] args)
11+
{
12+
string[] inputs;
13+
int surfaceN = int.Parse(Console.ReadLine()); // the number of points used to draw the surface of Mars.
14+
for (int i = 0; i < surfaceN; i++)
15+
{
16+
inputs = Console.ReadLine().Split(' ');
17+
int landX = int.Parse(inputs[0]); // X coordinate of a surface point. (0 to 6999)
18+
int landY = int.Parse(inputs[1]); // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
19+
}
20+
21+
// game loop
22+
while (true)
23+
{
24+
inputs = Console.ReadLine().Split(' ');
25+
int hSpeed = int.Parse(inputs[2]); // the horizontal speed (in m/s), can be negative.
26+
int vSpeed = int.Parse(inputs[3]); // the vertical speed (in m/s), can be negative.
27+
int power = int.Parse(inputs[6]); // the thrust power (0 to 4).
28+
29+
// Adjust thrust power based on the vertical speed using constants
30+
if (vSpeed <= MAX_VERTICAL_SPEED && power < MAX_THRUST_POWER) // Increase thrust if falling too fast
31+
{
32+
power++;
33+
}
34+
else if (vSpeed > MAX_VERTICAL_SPEED && power > MIN_THRUST_POWER) // Decrease thrust if falling slowly
35+
{
36+
power--;
37+
}
38+
39+
// Output the desired rotation and power (rotation is always 0 for this level)
40+
Console.WriteLine("0 " + power);
41+
}
42+
}
43+
}

puzzles/js/defibrillators.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)