https://github.com/techmmunity/water-jug-solver
https://github.com/techmmunity/water-jug-solver
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/techmmunity/water-jug-solver
- Owner: techmmunity
- License: apache-2.0
- Created: 2022-07-16T15:14:00.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-27T12:14:24.000Z (about 3 years ago)
- Last Synced: 2025-05-22T18:33:45.357Z (10 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@techmmunity/water-jug-solver
- Size: 599 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
Given two water jugs with capacities X and Y litres. Initially, both the jugs are empty. Also given that there is an infinite amount of water available. The jugs do not have markings to measure smaller quantities.
One can perform the following operations on the jug:
- Fill any of the jugs completely with water.
- Pour water from one jug to the other until one of the jugs is either empty or full, (X, Y) -> (X – d, Y + d)
- Empty any of the jugs
The task is to determine whether it is possible to measure Z litres of water using both the jugs. And if true, print any of the possible ways.
## Install
With Yarn:
```sh
yarn add @techmmunity/water-jug-solver
```
With NPM:
```sh
npm i @techmmunity/water-jug-solver
```
## Usage
With TypeScript:
```ts
import { solveWaterJugChallenge } from "@techmmunity/water-jug-solver";
console.log(
solveWaterJugChallenge({
firstJugCapacity: 2,
secondJugCapacity: 10,
desiredAmount: 4,
})
);
// Output
{
solvable: true,
minSteps: 4,
smallerJugCapacity: 2,
largerJugCapacity: 10,
steps: [
{
smallerJugContent: 2,
largerJugContent: 0,
index: "2,0",
action: {
type: "FILL",
jug: "SMALLER",
},
},
{
smallerJugContent: 0,
largerJugContent: 2,
index: "0,2",
action: {
type: "TRANSFER",
originJug: "SMALLER",
destinationJug: "BIGGER",
},
},
{
smallerJugContent: 2,
largerJugContent: 2,
index: "2,2",
action: {
type: "FILL",
jug: "SMALLER",
},
},
{
smallerJugContent: 0,
largerJugContent: 4,
index: "0,4",
action: {
type: "TRANSFER",
originJug: "SMALLER",
destinationJug: "BIGGER",
},
},
],
}
```
## How to contribute?
All the details about contributing to the project are [described here](https://github.com/techmmunity/base-project-services/blob/master/CONTRIBUTING.md).
