Die Hard Jug Puzzle I
From jug puzzles to Bézout's Identity
June 15 / 2021
Suppose you have two jugs that hold 7 gallons and 10 gallons of liquid respectively. Let’s further suppose that there are no visible markings on the jug, thus making it impossible to eyeball the volume of liquid inside. Now, how would you fill up these jugs so that they sum to exactly 12 gallons?
This is the exact problem Detective McClane and Electrician Carver faced in the movie Die Hard 3, except for a much dire consequence—the resulting volume must be within an ounce to target or a ticking bomb goes off. Nonetheless, the jug puzzle in the movie—to make 4 gallons out of 3 and 5-gallon-jugs—seems to be a simpler variant. Therefore, a sensible approach is to toy with the simpler version and hope that it can generalize to the more difficult one.
Given a 5-gallon and a 3-gallon-jug, how can you fill them with exactly 4 gallons of liquid?
With a bit of trial and error, we can first fill up the 5-gallon-jug, then pour it into the 3-gallon-jug until filled, leaving 2 gallons of liquid behind. We can then empty the 3-gallon-jug and into it pour the remaining 2-gallon-leftover. Notice that the 3-gallon-jug is only a gallon short of full. Now, all that's left is to refill the 5-gallon-jug and continue to pour the liquid into the 3-gallon-jug until filled. In other words, we have poured away 1 gallon from 5 gallons, leaving behind exactly 4 gallons.
Phew! Another day saved! But we are not quite done yet. Can you come up with a different way of filling the jugs to get 4 gallons?
This time, we will start by filling up the 3-gallon-jug, then empty it into the 5-gallon-jug. Now, let's refill the 3-gallon-jug and, similar to the previous solution, carefully pour away another 2 gallons to fill up the 5-gallon-jug. In the 3-gallon-jug currently sits exactly one gallon of liquid. Let's empty the 5-gallon-jug to make room for the 1-gallon-leftover. With one gallon secured, we can finally refill the 3-gallon-jug to get a total sum of 4 gallons.
That was not too thorny. But how can these two solutions help solve our original puzzle? What are steps taken that lead to the target? Can an algebraic representation generalize our solution to all other jug puzzles? Let's find out.
Let denote the capacity of the two jugs, where . Let , be the current volume of liquid inside the two jugs, where .
In Die Hard 3, the jugs are 3 and 5-gallon respectively, or . Now, let these symbols represent the steps in the two solutions.
|step||physical steps||symbolic step||volume|
|1||fill 5GL jug||(5, 0)|
|2||pour 5GL jug into 3GL jug||,||(2, 3)|
|3||empty 3GL jug, into it pour 2GL leftover||(0, 2)|
|4||refill 5GL jug||(5, 2)|
|5||pour 5GL jug into 3GL jug uitil filled||,||(4, 3)|
|6||empty 3GL jug||(4, 0)|
|step||physical steps||symbolic step||volume|
|1||fill 3GL jug||(0, 3)|
|2||empty 3GL jug into 5GL jug||(3, 0)|
|3||refill 3GL jug||(3, 3)|
|4||pour 3GL jug into 5GL jug until filled||,||(5, 1)|
|5||empty 5GL jug, into it pour 1GL leftover||(1, 0)|
|6||refill 3GL jug||(1, 3)|
With the power of algebra, we can discard the numbers and focus on the symbols only. To help us generalize better, we can extract common operations used across the solutions as the following:
Note: these operations are valid if , .
|a||fill empty x|
|b||fill empty y|
|c||pour away x to fill non-empty y||,|
|d||pour away y to fill non-empty x||,|
|e||pour x into empty y|
|f||pour y into empty x|
If we think of the volume of both jugs as a state of the jug problem, then these operations are means to get from one state to another. For instance, the 4th step in Solution 1 shows an operation: via which one can get from to . Using states and operations as building blocks, an intuitive representation into which we can transform the problem is a graph.
A graph is composed of a set of vertices and edges. In Graph 1.1, the vertices represent the states while the edges the allowable operations. Notice that the edges are bidirectional, in other words, the same operation can be reversed such that any given state can reach both of its predecessor and successor. For instance, from state , one can perform operation b to get to , or operation e then a to reverse back to .
Since our target concerns only with the sum = , we can simplify the above graph by summing each vertex and prunning repeated sums. Graph 1.2 shows a prunned version of Graph 1.1, where edges with more than one operation denote a sequence of operations to be performed in order. Let's take one step further and remove vertices whose . We are able to do so for the following reasons:
where , such that .
For instance, Let . . Thus, for any sum , if we can reach all , then we shall reach all .
Assumption : , such that .
For now, should we take this assumption for granted, Graph 1.2 can be reduced to Graph 1.3. Furthermore, we can truncate the edges threading by combining their sequence of operations. For instance, operations f, b, d, f going through vertex can be reduced to , while operations a, c, h going through vertex can be simplified to .
Thus, the distilled graph representation only requires the following operations:
Note: these operations are valid only when .
Using this new symbolic representation, can you solve the original problem?
Given a 10 and a 7-gallon-jug, how can you fill them with exactly 12 gallons of liquid?
Well, we can start by reformulating the problem statement with our new found representation:
Let , . Perform one operation at a time, show all states in sequence between and ?
In this representation, there are no better ways to reach the target than trial and error, so long as the next state . Let's begin from . The valid options are or . Therefore is either or . For , the valid options are , or . In the spirit of not repeating seen numbers, we proceed with . In addition, given Assumption , we can further avoid yielding for we conject that all are reachable by , where .
Thus we turn our trial-and-error into the following algorithm:
If we "run" the algorithm in our heads, we get the following result: .
With the trial and error algorithm, it is possible that we will visit all before we can get to our target . Thus, we can estimate an upper bound for the worst case that takes n steps to reach the target, where .
All seems well, except for two minor problems:
- The assumption on which our algorithm rests upon has yet to be proven.
- Can we do better than trial and error, in other words, can we find an algorithm that takes less steps to get to the target?
To see how we might go about these problems, stay tune for part II.
Eugenia Cheng. "The Perfect Glass of Wine." The University of Sheffield Youtube Channel.
Mathologer. "How not to Die Hard with Math." Youtube Channel.