@techmmunity/water-jug-solver
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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:

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.

Readme

Keywords

Package Sidebar

Install

npm i @techmmunity/water-jug-solver

Weekly Downloads

0

Version

2.0.0

License

Apache-2.0

Unpacked Size

86.9 kB

Total Files

22

Last publish

Collaborators

  • henriqueleite42