Skip to content

Commit

Permalink
Mars Lander - Episode 1 & ASCII Art
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesfranciscodev committed Oct 20, 2024
1 parent 913cdcb commit 9f03d92
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 29 deletions.
6 changes: 3 additions & 3 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 @@ -17,8 +17,8 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
| 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) | 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 |
| 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 |
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.
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);
}
}
}
79 changes: 54 additions & 25 deletions puzzles/python3/ascii-art/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

## Description

"ASCII Art" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a string of characters and a font size, and is asked to generate an ASCII art representation of the string using ASCII characters.
The goal of this puzzle is to simulate an old airport terminal display by displaying a line of text in ASCII art. To solve this challenge, you'll learn how to manage strings and perform array arithmetics. You will practice splitting strings into separate parts, concatenating them into a new string, and using array indexes effectively. By leveraging data structures like arrays or hash tables, you can store and recreate strings to create the ASCII art representation of the input text.

The ASCII art output should be a sequence of lines, each line representing a row of the output. Each row should contain the ASCII characters that correspond to the input string, with each character expanded to the specified font size. If a character in the input string is not a letter or cannot be represented in ASCII art, it should be replaced with a question mark.
## Example Input/Output

The challenge consists of writing a program that takes as input the string of characters, the font size, and the ASCII art characters to be used for each letter, and outputs the corresponding ASCII art representation.

The challenge is designed to help players learn and practice programming skills such as string manipulation, input/output handling, and ASCII art rendering. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle.

### Example Input
**Input**

```
4
Expand All @@ -24,7 +20,7 @@ CodinGame
```

### Example Output
**Output**

```
## # ## ### ### ## # # # ###
Expand Down Expand Up @@ -66,28 +62,61 @@ To solve this coding puzzle, you'll need to create a program that takes the widt
7. **Testing**:
- Test your program with different input cases, including different widths, heights, and text inputs, to ensure it works correctly.

Here's a simplified Python code for the main part of the code:
## Code Example

```python
def ascii_art(L, H, T, ascii_dict):
result = [''] * H

for char in T:
if char.isalpha():
index = ord(char.upper()) - ord('A')
else:
index = 26 # '?' character

start = index * L
end = start + L
for i in range(H):
result[i] += ascii_dict[i][start:end]
L = int(input()) # width of each character
H = int(input()) # height of the ASCII art
T = input() # input text to be converted to ASCII art

# Read the ASCII art for A-Z and '?'
ascii_art = []
for i in range(H):
row = input()
ascii_art.append(row)

# Create a dictionary to store the ASCII art for each character
ascii_dict = {}

for i in range(26): # for A-Z
ascii_dict[chr(i + ord('A'))] = [ascii_art[j][i*L:(i+1)*L] for j in range(H)]

# '?' will represent any unknown character
ascii_dict['?'] = [ascii_art[j][26*L:(27)*L] for j in range(H)]

# Convert the input text T to uppercase and handle unknown characters
result = ['' for _ in range(H)]
for char in T:
if char.isalpha():
char = char.upper()
else:
char = '?'

return '\n'.join(result)
for i in range(H):
result[i] += ascii_dict.get(char, ascii_dict['?'])[i]

# Print the ASCII art for the input text T
for line in result:
print(line)

```

This pseudocode outlines the key steps to solve the coding puzzle. You need to implement the details, including reading the ASCII art representations and handling edge cases.
## Explanation of Code Steps

1. **Input Parsing**:
- Read `L` (width of each letter), `H` (height of letters), and `T` (input text).
- For `H` lines, read the ASCII art representing all characters from A to Z and `?`.

2. **Store ASCII Art**:
- Use a list to store the ASCII art of each character in a structured way.
- For each character in the string `T`, extract the corresponding ASCII art from this list.

3. **Handle Non-alphabet Characters**:
- Any character outside of the range `[A-Z]` or `[a-z]` should be replaced by the `?` character's ASCII art.

4. **Print the ASCII Art**:
- For each line in the height `H`, concatenate and print the corresponding parts of the ASCII art for each character in `T`.

## Edge Cases

Test your program by experimenting with an empty ASCII art representation for one or more characters to confirm that it appropriately manages missing character representations. Additionally, try inputting a combination of uppercase and lowercase letters to verify that the program handles case conversion and character lookup accurately. Test different combinations of widths and heights to ensure the program can handle various dimensions without errors or output distortion. Provide a single character input string to assess the program's handling of single-character input and its corresponding ASCII art output. Furthermore, examine input strings containing leading, trailing, and multiple consecutive whitespace characters to ensure proper whitespace handling. Lastly, test input strings where characters overlap in the ASCII art output to confirm the program handles such cases without merging characters or distorting the output. By conducting these tests, you can guarantee that the program gracefully handles various scenarios and produces correct ASCII art output under diverse conditions.
Test your program by experimenting with an empty ASCII art representation for one or more characters to confirm that it appropriately manages missing character representations. Additionally, try inputting a combination of uppercase and lowercase letters to verify that the program handles case conversion and character lookup accurately. Test different combinations of widths and heights to ensure the program can handle various dimensions without errors or output distortion. Provide a single character input string to assess the program's handling of single-character input and its corresponding ASCII art output. Furthermore, examine input strings containing leading, trailing, and multiple consecutive whitespace characters to ensure proper whitespace handling. In summary, thoroughly test your program with various inputs, including empty ASCII art, mixed case letters, different dimensions, single characters, and strings with whitespace, to ensure it handles all cases accurately.
5 changes: 4 additions & 1 deletion puzzles/python3/mars-lander1/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Mars Lander - Episode 1

## Description

"Mars Lander - Episode 1" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player takes on the role of a spaceship pilot trying to safely land a spacecraft on the surface of Mars.

The player is given the coordinates and the speed of the spacecraft, as well as the angle of its trajectory and the thrust power to be used to adjust its speed and direction. The objective is to write a program that computes the optimal thrust power and angle for the spacecraft to land safely on the surface of Mars without crashing or running out of fuel.

The challenge consists of writing a program that takes as input the current state of the spacecraft and outputs the thrust power and angle that should be used to adjust its trajectory and speed for a safe landing. The program needs to take into account the gravitational pull of Mars, the speed of the spacecraft, the remaining fuel, and the altitude of the spacecraft above the surface of Mars.

The challenge is designed to help players learn and practice programming skills such as physics calculations, conditional statements, and input/output handling. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle.
## 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

Expand Down

0 comments on commit 9f03d92

Please sign in to comment.