This project's intent is to solve a variation of the Water Jugs problem.
It provides a simple RESTful API to mix jugs to get to a target volume.
- Node.js (version 14 or higher)
- npm (Node.js package manager)
- Clone this repository:
git clone https://github.com/emystein/water-jug-challenge.git
cd water-jug-challenge
- Install dependencies:
npm install
- Start the server:
npm start
The server will start running at http://localhost:3000
.
This endpoint mixes two jugs with given volumes and a target volume.
jugX
: Volume of jug X (positive number)jugY
: Volume of jug Y (positive number)targetVolume
: Target volume to achieve (positive number)
200 OK
: If mixing is successful, returns the mixing result.400 Bad Request
: If any of the input parameters (jugX, jugY, targetVolume) are not positive numbers.
Mixing jugs with volumes jugX=2, jugY=10, targetVolume=4:
curl -X POST http://localhost:3000/mix -H "Content-Type: application/json" -d '{"jugX":2,"jugY":10,"targetVolume":4}'
should return a JSON response like this:
{
"status": "Solved",
"solution": [
{
"step": 1,
"jugX": 2,
"jugY": 0,
"action": "Fill Jug X"
},
{
"step": 2,
"jugX": 0,
"jugY": 2,
"action": "Transfer from Jug X to Jug Y"
},
{
"step": 3,
"jugX": 2,
"jugY": 2,
"action": "Fill Jug X"
},
{
"step": 4,
"jugX": 0,
"jugY": 4,
"action": "Transfer from Jug X to Jug Y"
}
]
}
Mixing jugs with volumes jugX=2, jugY=6, targetVolume=5:
curl -X POST http://localhost:3000/mix -H "Content-Type: application/json" -d '{"jugX":2,"jugY":6,"targetVolume":5}'
should return a JSON response like this:
{
"status": "No Solution",
"solution": []
}
To run the tests:
npm test