RiddleSolver is a .NET 8 REST API designed to tackle the Water Jug Problem. This project is built with extensibility in mind, making it easy to implement solutions for other riddles in the future.
The architecture consists of three layers, which helps to organize the codebase and enhance maintainability:
- Api: Manages HTTP requests and responses through controllers.
- Core: Contains the logic that solves the riddles.
- Data: Implements caching to improve performance and efficiency.
In order to tackle the Water Jug Problem, the Breadth-First Search (BFS) algorithm was used. The process begins with both jugs empty and explores possible actions-such as filling, emptying, and pouring—level by level. The goal is to find a state where one of the jugs contains the desired amount of water.
The algorithm tracks the steps taken, providing a clear record of actions and the state of each jug throughout the solution process.
The REST API exposes the following endpoint:
- URI:
/api/v1/riddles/waterjug
- HTTP Method:
POST
- Content Type:
application/json
{
"capacityX": 3,
"capacityY": 5,
"amountWanted": 2
}
- capacityX: Capacity of the first jug.
- capacityY: Capacity of the second jug.
- amountWanted: Desired amount to measure.
The API can return the following responses:
- 200 OK: Success
{
"success": true,
"status": "Success",
"message": "Solved",
"data": {
"solution": [
{
"step": 1,
"bucketX": 0,
"bucketY": 5,
"action": "Fill bucket y"
},
{
"step": 2,
"bucketX": 3,
"bucketY": 2,
"action": "Transfer from bucket y to bucket x. SOLVED"
}
]
}
}
- 400 Bad Request: Validation Error
{
"success": false,
"status": "ValidationError",
"message": "Capacity X cannot be less than or equal to zero",
"data": null
}
- 422 Unprocessable: No solution
{
"success": false,
"status": "Unprocessable",
"message": "No Solution",
"data": null
}
- success: Indicates whether the operation was successful.
- status: Status of the request.
- message: Descriptive message about the result.
- data: Contains the solution to the riddle, represented as a list of steps.
You can run the RiddleSolver project using either the .NET 8 SDK or Docker. Follow the steps below for your preferred setup.
Ensure you have either the .NET 8 SDK installed or Docker installed on your machine.
First, clone the project repository:
git clone https://github.com/omtejeda/riddle-solver-api.git
cd riddle-solver-api
- Run the project:
dotnet run --project src/Api
- Build the Docker image:
docker build -t riddle-solver-api:1.0 .
- Run the Docker container:
docker run -dp 5045:8080 --name riddle-solver-api riddle-solver-api:1.0
Once the steps above are completed, open your browser and go to http://localhost:5045 to access the API documentation via Swagger.