🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # ErikSchierboom's solution

## to Two Bucket in the F# Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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, passed as a numeric value
• the size of bucket two, passed as a numeric value
• the desired number of liters to reach, passed as a numeric value
• which bucket to fill first, passed as a String (either 'one' or 'two')

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

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.

## Source

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

## Submitting Incomplete Solutions

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

### TwoBucketTest.fs

``````// This file was auto-generated based on version 1.4.0 of the canonical data.

module TwoBucketTest

open FsUnit.Xunit
open Xunit

open TwoBucket

[<Fact>]
let ``Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one`` () =
let bucketOne = 3
let bucketTwo = 5
let goal = 1
let startBucket = Bucket.One
let expected = { Moves = 4; GoalBucket = Bucket.One; OtherBucket = 5 }
measure bucketOne bucketTwo goal startBucket |> should equal expected

[<Fact(Skip = "Remove to run test")>]
let ``Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two`` () =
let bucketOne = 3
let bucketTwo = 5
let goal = 1
let startBucket = Bucket.Two
let expected = { Moves = 8; GoalBucket = Bucket.Two; OtherBucket = 3 }
measure bucketOne bucketTwo goal startBucket |> should equal expected

[<Fact(Skip = "Remove to run test")>]
let ``Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one`` () =
let bucketOne = 7
let bucketTwo = 11
let goal = 2
let startBucket = Bucket.One
let expected = { Moves = 14; GoalBucket = Bucket.One; OtherBucket = 11 }
measure bucketOne bucketTwo goal startBucket |> should equal expected

[<Fact(Skip = "Remove to run test")>]
let ``Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two`` () =
let bucketOne = 7
let bucketTwo = 11
let goal = 2
let startBucket = Bucket.Two
let expected = { Moves = 18; GoalBucket = Bucket.Two; OtherBucket = 7 }
measure bucketOne bucketTwo goal startBucket |> should equal expected

[<Fact(Skip = "Remove to run test")>]
let ``Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two`` () =
let bucketOne = 1
let bucketTwo = 3
let goal = 3
let startBucket = Bucket.Two
let expected = { Moves = 1; GoalBucket = Bucket.Two; OtherBucket = 0 }
measure bucketOne bucketTwo goal startBucket |> should equal expected

[<Fact(Skip = "Remove to run test")>]
let ``Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two`` () =
let bucketOne = 2
let bucketTwo = 3
let goal = 3
let startBucket = Bucket.One
let expected = { Moves = 2; GoalBucket = Bucket.Two; OtherBucket = 2 }
measure bucketOne bucketTwo goal startBucket |> should equal expected``````
``````module TwoBucket

type Bucket = One | Two

let contents = fst
let size = snd

let empty (_, size) = 0, size
let fill (_, size) = size, size

let emptyFirstBucket (first, second) = empty first, second
let emptySecondBucket (first, second) = first, empty second

let fillFirstBucket (first, second) = fill first, second
let fillSecondBucket (first, second) = first, fill second

let pourFromFirstToSecond (first, second) =
let amount = min (size second - contents second) (contents first)
(contents first - amount, size first), (contents second + amount, size second)

let pourFromSecondToFirst (first, second) =
let amount = min (size first - contents first) (contents second)
(contents first + amount, size first), (contents second - amount, size second)

let isEmpty (contents, _) = contents = 0
let isFull (contents, size) = contents = size

let firstBucketEmpty (first, _) = isEmpty first
let firstBucketFull (first, _) = isFull first

let secondBucketEmpty (_, second) = isEmpty second
let secondBucketFull (_, second) = isFull second

let canPourToFirstBucket (first, second) = contents first + contents second <> size first

let canPourToSecondBucket buckets =
(firstBucketFull buckets && not (secondBucketFull buckets)) ||
(not (firstBucketFull buckets) && secondBucketEmpty buckets)

let startFromFirstBucket buckets =
if firstBucketEmpty buckets then fillFirstBucket buckets
elif secondBucketFull buckets then emptySecondBucket buckets
elif canPourToSecondBucket buckets then pourFromFirstToSecond buckets
else failwith "Cannot transition from state"

let startFromSecondBucket buckets =
if firstBucketFull buckets then emptyFirstBucket buckets
elif secondBucketEmpty buckets then fillSecondBucket buckets
elif canPourToFirstBucket buckets then pourFromSecondToFirst buckets
else failwith "Cannot transition from state"

let rec solve target strategy moves (first, second) =
if contents first = target then Some (moves, One, contents second)
elif contents second = target then Some (moves, Two, contents first)
else solve target strategy (moves + 1) (strategy (first, second))

let moves firstSize secondSize target =
function
| One -> solve target startFromFirstBucket 1 ((firstSize, firstSize), (0, secondSize))
| Two -> solve target startFromSecondBucket 1 ((0, firstSize), (secondSize, secondSize))``````