Water pouring puzzle

Initially each jug contains a known integer volume of liquid, not necessarily equal to its capacity.

It is a common assumption, stated as part of these puzzles, that the jugs in the puzzle are irregularly shaped and unmarked, so that it is impossible to accurately measure any quantity of water that does not completely fill a jug.

The puzzle may be solved in seven steps, passing through the following sequence of states (denoted as a bracketed triple of the three volumes of water in the three jugs): Cowley (1926) writes that this particular puzzle "dates back to mediaeval times" and notes its occurrence in Bachet's 17th-century mathematics textbook.

This version of the puzzle was featured in a scene of the 1995 movie Die Hard with a Vengeance.

[4] This variant has an optimal solution that can be obtained using a billiard-shape barycentric plot (or a mathematical billiard).

[5] The graph shows two ways to obtain 4 liters using 3-liter and 5-liter jugs, and a water source and sink on a Cartesian grid with diagonal lines of slope −1 (such that

If and only if the jugs' volumes are co-prime, every boundary point is visited, giving an algorithm to measure any integer amount up to the sum of the volumes.As shown in the previous section, we can construct the solution to the problem from the desired result by using reversible actions only (emptying a full jug into the sink and filling an empty jug from the tap are both reversible).

These solutions can be visualized by red and blue arrows in a Cartesian grid with diagonal lines (of slope -1 such that

[7] In consequence the steps can be visualized as billiard moves in the (clipped) coordinate system on a triangular lattice.

Starting at the square, solid red and dashed blue paths show pourable transitions.

Starting state of the standard puzzle; a jug filled with 8 units of water, and two empty jugs of sizes 5 and 3. The solver must pour the water so that the first and second jugs both contain 4 units, and the third is empty.
Solution to puzzle with 3 L and 5 L jugs, a tap and a drain
Two solutions on a Cartesian grid, the upper one equivalent to the diagram on the left
Starting with 9 liters in the 12-liter jug, the solution for 5 liters is plotted in red on the left, and the solution for 4 liters is plotted in blue on the right. All the slanted lines have the same slope of -1, representing pouring water from one jug to another.
Two solutions to the standard puzzle using a barycentric plot