Published at Oct 04 2019
·
0 comments

Instructions

Test suite

Solution

Given two buckets of different size, demonstrate how to measure an exact number of liters by strategically transferring liters of fluid between the buckets.

Since this mathematical problem is fairly subject to interpretation / individual approach, the tests have been written specifically to expect one overarching solution.

To help, the tests provide you with which bucket to fill first. That means, when starting with the larger bucket full, you are NOT allowed at any point to have the smaller bucket full and the larger bucket empty (aka, the opposite starting point); that would defeat the purpose of comparing both approaches!

Your program will take as input:

- the size of bucket one
- the size of bucket two
- the desired number of liters to reach
- which bucket to fill first, either bucket one or bucket two

Your program should determine:

- the total number of "moves" it should take to reach the desired number of liters, including the first fill
- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two
- how many liters are left in the other bucket (bucket B)

Note: any time a change is made to either or both buckets counts as one (1) move.

Example: Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters. Let's say bucket one, at a given step, is holding 7 liters, and bucket two is holding 8 liters (7,8). If you empty bucket one and make no change to bucket two, leaving you with 0 liters and 8 liters respectively (0,8), that counts as one "move". Instead, if you had poured from bucket one into bucket two until bucket two was full, leaving you with 4 liters in bucket one and 11 liters in bucket two (4,11), that would count as only one "move" as well.

To conclude, the only valid moves are:

- pouring from one bucket to another
- emptying one bucket and doing nothing to the other
- filling one bucket and doing nothing to the other

Written with <3 at Fullstack Academy by Lindsay Levine.

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Install assignment dependencies:

```
$ yarn install
```

Execute the tests with:

```
$ yarn test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xit`

to
`it`

.

Water Pouring Problem http://demonstrations.wolfram.com/WaterPouringProblem/

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import { TwoBucket, Bucket } from "./two-bucket"
describe("TwoBucket", () => {
describe("works for input of 3, 5, 1", () => {
const buckOne = 3
const buckTwo = 5
const goal = 1
it("starting with bucket one", () => {
const starterBuck = Bucket.One // indicates which bucket to fill first
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
expect(twoBucket.moves()).toEqual(4) // includes the first fill
expect(twoBucket.goalBucket).toEqual("one") // which bucket should end up with the desired # of liters
expect(twoBucket.otherBucket).toEqual(5) // leftover value in the "other" bucket once the goal has been reached
})
xit("starting with bucket two", () => {
const starterBuck = Bucket.Two
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
expect(twoBucket.moves()).toEqual(8)
expect(twoBucket.goalBucket).toEqual("two")
expect(twoBucket.otherBucket).toEqual(3)
})
})
describe("works for input of 7, 11, 2", () => {
const buckOne = 7
const buckTwo = 11
const goal = 2
xit("starting with bucket one", () => {
const starterBuck = Bucket.One
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
expect(twoBucket.moves()).toEqual(14)
expect(twoBucket.goalBucket).toEqual("one")
expect(twoBucket.otherBucket).toEqual(11)
})
xit("starting with bucket two", () => {
const starterBuck = Bucket.Two
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
expect(twoBucket.moves()).toEqual(18)
expect(twoBucket.goalBucket).toEqual("two")
expect(twoBucket.otherBucket).toEqual(7)
})
})
})
```

```
"use strict";
class Bucket {
public static One: string = 'one';
public static Two: string = 'two';
private contains: number = 0;
/**
* @param {number} capacity
* @param {string} name
*/
public constructor(public readonly capacity: number, public readonly name: string) {
}
/**
* @returns {number}
*/
public get amount(): number {
return this.contains;
}
/**
* @param {number} amount
* @throws {Error}
*/
public set amount(amount: number) {
if (amount > this.capacity) {
throw new Error('Bucket overflow');
}
if (amount < 0) {
throw new Error('Cant pour from empty');
}
this.contains = amount;
}
/**
* @returns {number}
*/
public amountLeft(): number {
return this.capacity - this.contains;
}
public fill(): void {
this.contains = this.capacity;
}
/**
* @returns {boolean}
*/
public isFilled(): boolean {
return this.capacity === this.contains;
}
public empty(): void {
this.contains = 0;
}
/**
* @returns {boolean}
*/
public isEmpty(): boolean {
return 0 === this.contains;
}
}
class TwoBucket {
/**
* @type {Bucket}
*/
private bucketOne: Bucket;
/**
* @type {Bucket}
*/
private bucketTwo: Bucket;
/**
* @type {number}
*/
private moveCount: number = 0;
/**
* @type {string}
*/
private goalBuck: string = '';
/**
* @param {number} buckOne
* @param {number} buckTwo
* @param {number} goal
* @param {string} current
*/
public constructor(
buckOne: number,
buckTwo: number,
private readonly goal: number,
private readonly starterBuck: string
) {
const starterBuckOne: boolean = starterBuck === Bucket.One;
this.bucketOne = new Bucket(
starterBuckOne ? buckOne : buckTwo,
starterBuckOne ? Bucket.One : Bucket.Two
);
this.bucketTwo = new Bucket(
starterBuckOne ? buckTwo : buckOne,
starterBuckOne ? Bucket.Two : Bucket.One
);
}
private transfer(): void {
do {
if (this.bucketOne.isEmpty()) {
this.bucketOne.fill();
} else if (!this.bucketTwo.isFilled()) {
this.pour(this.bucketOne, this.bucketTwo);
} else {
this.bucketTwo.empty();
}
this.moveCount++;
} while (this.bucketOne.amount !== this.goal && this.bucketTwo.amount !== this.goal);
this.goalBuck = this.bucketOne.amount === this.goal ? this.bucketOne.name : this.bucketTwo.name;
}
/**
* @param {Bucket} bucketFrom
* @param {Bucket} bucketTo
*/
private pour(bucketFrom: Bucket, bucketTo: Bucket): void {
let pourAmount: number = 0;
if (bucketFrom.amount >= bucketTo.amountLeft()) {
pourAmount = bucketTo.amountLeft();
} else {
pourAmount = bucketFrom.amount;
}
bucketFrom.amount = bucketFrom.amount - pourAmount;
bucketTo.amount = bucketTo.amount + pourAmount;
}
/**
* @returns {number}
*/
public moves(): number {
if (!this.moveCount) {
this.transfer();
}
return this.moveCount;
}
/**
* @returns {string}
*/
public get goalBucket(): string {
return this.goalBuck;
}
/**
* @returns {number}
*/
public get otherBucket(): number {
return (this.goalBuck === this.bucketOne.name ? this.bucketTwo : this.bucketOne).amount;
}
}
export {Bucket, TwoBucket};
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments