Here is a classic puzzle:
You are camping, and have an 8-liter bucket which is full of fresh water. You need to share this water fairly into exactly two portions (4 + 4 liters). But you only have two empty buckets: a 5-liter and a 3-liter. Divide the 8 liters in half in as short a time as possible.
This is not an easy task and requires at least 7 transfusions to solve it.
The procedure Pouring solves a similar problem for the general case. Given n buckets of known volume and the amount of water in each bucket is known. Buckets can be partially filled, be full or be empty (of course the case when all is empty or all is full is excluded). With each individual transfusion, the bucket from which it is poured must be completely free, or the bucket into which it is poured must be completely filled. It is forbidden to pour water anywhere other than the indicated buckets.
Formal parameters of the procedure: BucketVolumes is a list of bucket volumes, WaterVolumes is a list of water volumes in these buckets. The procedure returns all possible states that can occur during transfusions in the form of a tree (the initial state WaterVolumes is its root).
local S, W, n, N, OneStep, j, v, H, G;
uses ListTools, GraphTheory;
if nops(WaterVolumes)<>n then error "The lists should be the same length" fi;
if n<2 then error "Must have at least 2 buckets" fi;
if not `or`(op(WaterVolumes>~0)) then error "There must be at least one non-empty bucket" fi;
if BucketVolumes=WaterVolumes then error "At least one bucket should not be full" fi;
if not `and`(seq(WaterVolumes[i]<=BucketVolumes[i], i=1..n)) then error "The amount of water in each bucket cannot exceed its volume" fi;
local w, k, i, v, V, k1, v0;
for w in W do
for i from 1 to n do
for j from 1 to n do
if i<>j and w[-1][i]<>0 and w[-1][j]<BucketVolumes[j] then k:=k+1; v[k]:=subsop(i=`if`(w[-1][i]<=BucketVolumes[j]-w[-1][j],0,w[-1][i]-(BucketVolumes[j]-w[-1][j])),j=`if`(w[-1][i]<=BucketVolumes[j]-w[-1][j],w[-1][j]+w[-1][i],BucketVolumes[j]),w[-1]); fi;
if `and`(seq(v0 in L, v0=v)) then k1:=k1+1; V[k1]:=w else
for v0 in v do
if not (v0 in L) then k1:=k1+1; V[k1]:=[op(w),v0] fi;
L:=L union convert(v,set);
for N from 1 do
if S[N-1]=S[N] then break fi;
if Output=set then return L else
if Output=trails then interface(rtablesize=infinity);
return <H[N-1]> else
DrawGraph(G, style=tree, root=convert(WaterVolumes,string)[2..-2], stylesheet = [vertexcolor = "Yellow", vertexfont=[TIMES,20]], size=[800,500]) fi; fi;
Examples of use:
Here is the solution to the original puzzle above. We see that at least 7 transfusions are
required to get equal volumes (4 + 4) in two buckets
With an increase in the number of buckets, the number of solutions is extremely
increased. Here is the solution to the problem: is it possible to equalize the amount of water (7+7+7+7) in the following example?
is([7,7,7,7] in S);