ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# DunetsNM's solution

## to Two Bucket in the F# Track

Published at Feb 07 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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
• the size of bucket two
• the desired number of liters to reach
• which bucket to fill first, either bucket one or bucket two

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

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

## Source

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

### 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

open System

type Bucket = One | Two

type Solution = { Moves : int; GoalBucket : Bucket; OtherBucket : int }

type State = { One: int; Two: int }

let private statesFrom capacityOne capacityTwo state =
seq {
// pour from first to second
if state.One > 0 && state.Two < capacityTwo then
let litresToPour = Math.Min(state.One, capacityTwo - state.Two)
yield { One = state.One - litresToPour; Two = state.Two + litresToPour }
// pour from second to first
if state.Two > 0 && state.One < capacityOne then
let litresToPour = Math.Min(state.Two, capacityOne - state.One)
yield { One = state.One + litresToPour; Two = state.Two - litresToPour; }
// empty first
if state.One > 0 then
yield { state with One = 0 }
// empty second
if state.Two > 0 then
yield { state with Two = 0 }
// fill first bucket
if state.One < capacityOne then
yield { state with One = capacityOne }
// fill second bucket
if state.Two < capacityTwo then
yield { state with Two = capacityTwo }
}

let rec private loop capacityOne capacityTwo goal currentStates move alreadyProcessedStates =
let goalState1 =
currentStates |> Seq.tryFind (fun state -> state.One = goal)
let goalState2 =
currentStates |> Seq.tryFind (fun state -> state.Two = goal)
match (goalState1, goalState2) with
| (Some state, _) ->
{ Moves = move; GoalBucket = One; OtherBucket = state.Two }
| (_, Some state) ->
{ Moves = move; GoalBucket = Two; OtherBucket = state.One }
| (None, None) ->
let nextStates =
currentStates
|> Seq.collect (statesFrom capacityOne capacityTwo)
|> Seq.distinct
|> Set.ofSeq

if (Seq.isEmpty nextStates) then
failwithf "can't find, stopped at move number %d" move

loop capacityOne capacityTwo goal nextStates (move + 1)

let measure (capacityOne : int) (capacityTwo: int) (goal: int) (startBucket: Bucket) =
let litresIf actualBucket expectedBucket litres =
if expectedBucket = actualBucket then litres else 0
let initalState = { One = litresIf startBucket One capacityOne; Two = litresIf startBucket Two capacityTwo }

// disallow non-starting bucket full & starting empty.
// which in turn means disallow both buckets empty - this move makes no sense otherwise than to swap inital setup
let initialStatesToAvoid =
Set.ofList [
{ One = litresIf startBucket Two capacityOne; Two = litresIf startBucket One capacityTwo };
{ One = 0; Two = 0 }; ]

loop capacityOne capacityTwo goal (Set.ofList [initalState]) 1 initialStatesToAvoid``````