An open API service indexing awesome lists of open source software.

https://github.com/techmmunity/water-jug-solver


https://github.com/techmmunity/water-jug-solver

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

# Techmmunity - Water Jug Challenge Solver


Style Guide: Techmmunity


Coveralls


Tests


Npm


Downloads




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).