> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/rushes/rush01.md).

# Rush01

You need to develop a program that solves a **4x4 grid puzzle** where you must place **boxes (numbers) of heights 1 to 4** in each square. However, there are **view constraints** that determine how many boxes are visible from each row and column.

* The constraints come from **four different perspectives**:
  * Left → Right
  * Right → Left
  * Top → Bottom
  * Bottom → Top
* Each row and column must have **unique** numbers (no repetition of 1-4).

***

#### **How the Puzzle Works**

1. The program takes **16 numbers** as input. Each number represents how many boxes are visible from a given direction.
2. The goal is to place numbers (1 to 4) in the **4x4 grid** so that:
   * The number of visible boxes from each direction matches the given input.
   * No number is repeated in a single row or column.

Example:

```
Input: "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2"
Output:
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
```

Here, the boxes are placed correctly based on the given constraints.

***

#### **Project Rules & Constraints**

* Your **program must compile** using:

  ```
  gcc -Wall -Wextra -Werror -o rush-01 *.c
  ```
* Allowed functions:
  * `write()`
  * `malloc()`
  * `free()`
* Any **invalid input must return an "Error" message**.
* You must **collaborate with your teammates**, and each member must understand the entire solution.
* **Submission is final**, and you must present your code during defense.

***

#### **How to Approach the Solution**

1. **Parse the Input**
   * Convert the string of numbers into an integer array.
   * Ensure it contains exactly **16 numbers (1-4).**
   * Handle errors if the input is incorrect.
2. **Grid Initialization**
   * Create a 4x4 matrix to store numbers.
3. **Backtracking Algorithm**
   * Try placing numbers (1-4) in each cell.
   * Check if the row, column, and visibility constraints are met.
   * If a placement is invalid, backtrack and try a different number.
4. **Output the Solution**
   * Print the grid in the required format.
   * If no valid solution exists, print `"Error"`.

***

#### **Next Step: Understanding the Code (without bonus)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/rushes/rush01.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
