Skip to content

Commit

Permalink
Descent, Thor 1, Temperatures, Mars Lander 1, ASCII Art, MIME Type, D…
Browse files Browse the repository at this point in the history
…efibrillators & Duals
  • Loading branch information
charlesfranciscodev committed Oct 22, 2024
1 parent 5c3fa24 commit 9b689d9
Show file tree
Hide file tree
Showing 33 changed files with 1,164 additions and 362 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Solutions to CodinGame Puzzles

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

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.
Expand All @@ -14,15 +14,15 @@ 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) &starf;, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C](./puzzles/c/the-descent) | Conditions, Loops |
| 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 |
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures) &starf;, [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) &starf;, [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) &starf; | Strings |
| 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 |
| 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 |
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures), [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) &starf; | Conditions, Loops, Arrays |
| 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 |
| 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 |
| Unary 1️⃣ | [Python](./puzzles/python3/unary), [TypeScript](./puzzles/ts/unary) &starf;, [Haskell](./puzzles/haskell/unary), [C#](./puzzles/cs/unary) | Strings, Encoding |
| 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 |
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) &starf;, [Kotlin](./puzzles/kotlin/src/defibrillators), [TypeScript](./puzzles/ts/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
| 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 |
| Defibrillators 💖 | [Python](./puzzles/python3/defibrillators) &starf;, [Kotlin](./puzzles/kotlin/src/defibrillators), [JavaScript](./puzzles/js/defibrillators), [C#](./puzzles/cs/defibrillators) | Strings, Trigonometry |
| 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 |

### Medium Puzzles
| Title | Solution(s) | Topic(s) |
Expand Down
Binary file added images/codingame_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions puzzles/bash/power-of-thor1/README.md
Original file line number Diff line number Diff line change
@@ -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
```
40 changes: 40 additions & 0 deletions puzzles/bash/power-of-thor1/power_of_thor1.sh
Original file line number Diff line number Diff line change
@@ -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
21 changes: 0 additions & 21 deletions puzzles/cpp/the-descent.cpp

This file was deleted.

60 changes: 60 additions & 0 deletions puzzles/cpp/the-descent/README.md
Original file line number Diff line number Diff line change
@@ -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 <iostream>
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;
}
}
```
25 changes: 25 additions & 0 deletions puzzles/cpp/the-descent/the_descent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
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;
}
}
57 changes: 57 additions & 0 deletions puzzles/cs/mars-lander1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Mars Lander - Episode 1

## Description

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.

## Solution Overview

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.

## Code Example

```csharp
using System;

class Player
{
// Define constants for better readability and maintainability
const int MAX_VERTICAL_SPEED = -40; // Maximum allowed vertical speed before adjusting thrust
const int MAX_THRUST_POWER = 4; // Maximum thrust power
const int MIN_THRUST_POWER = 0; // Minimum thrust power
static void Main(string[] args)
{
string[] inputs;
int surfaceN = int.Parse(Console.ReadLine()); // the number of points used to draw the surface of Mars.
for (int i = 0; i < surfaceN; i++)
{
inputs = Console.ReadLine().Split(' ');
int landX = int.Parse(inputs[0]); // X coordinate of a surface point. (0 to 6999)
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.
}

// game loop
while (true)
{
inputs = Console.ReadLine().Split(' ');
int hSpeed = int.Parse(inputs[2]); // the horizontal speed (in m/s), can be negative.
int vSpeed = int.Parse(inputs[3]); // the vertical speed (in m/s), can be negative.
int power = int.Parse(inputs[6]); // the thrust power (0 to 4).
// Adjust thrust power based on the vertical speed using constants
if (vSpeed <= MAX_VERTICAL_SPEED && power < MAX_THRUST_POWER) // Increase thrust if falling too fast
{
power++;
}
else if (vSpeed > MAX_VERTICAL_SPEED && power > MIN_THRUST_POWER) // Decrease thrust if falling slowly
{
power--;
}

// Output the desired rotation and power (rotation is always 0 for this level)
Console.WriteLine("0 " + power);
}
}
}
```
43 changes: 43 additions & 0 deletions puzzles/cs/mars-lander1/mars_lander1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

class Player
{
// Define constants for better readability and maintainability
const int MAX_VERTICAL_SPEED = -40; // Maximum allowed vertical speed before adjusting thrust
const int MAX_THRUST_POWER = 4; // Maximum thrust power
const int MIN_THRUST_POWER = 0; // Minimum thrust power

static void Main(string[] args)
{
string[] inputs;
int surfaceN = int.Parse(Console.ReadLine()); // the number of points used to draw the surface of Mars.
for (int i = 0; i < surfaceN; i++)
{
inputs = Console.ReadLine().Split(' ');
int landX = int.Parse(inputs[0]); // X coordinate of a surface point. (0 to 6999)
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.
}

// game loop
while (true)
{
inputs = Console.ReadLine().Split(' ');
int hSpeed = int.Parse(inputs[2]); // the horizontal speed (in m/s), can be negative.
int vSpeed = int.Parse(inputs[3]); // the vertical speed (in m/s), can be negative.
int power = int.Parse(inputs[6]); // the thrust power (0 to 4).

// Adjust thrust power based on the vertical speed using constants
if (vSpeed <= MAX_VERTICAL_SPEED && power < MAX_THRUST_POWER) // Increase thrust if falling too fast
{
power++;
}
else if (vSpeed > MAX_VERTICAL_SPEED && power > MIN_THRUST_POWER) // Decrease thrust if falling slowly
{
power--;
}

// Output the desired rotation and power (rotation is always 0 for this level)
Console.WriteLine("0 " + power);
}
}
}
36 changes: 0 additions & 36 deletions puzzles/js/defibrillators.js

This file was deleted.

Loading

0 comments on commit 9b689d9

Please sign in to comment.