@techmmunity/water-jug-solver
v2.0.0
Published
Water Jug Challenge Solver
Downloads
2
Readme
Techmmunity - Water Jug Challenge Solver
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:
yarn add @techmmunity/water-jug-solver
With NPM:
npm i @techmmunity/water-jug-solver
Usage
With TypeScript:
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.