You have two jugs with no markings, one holding 3 litres and one holding 5. There is a tap that never runs dry and a drain. A move is one of three things: fill a jug to the brim from the tap, empty a jug completely down the drain, or pour one jug into the other until the first is empty or the second is full. How do you end up with exactly 4 litres in one of the jugs?
Write a position as a pair , where is the number of litres in the 5-litre jug and the number in the 3-litre jug. You start at . Each move takes you from one position to another, and the question is whether or is reachable, and by how short a route.
List every position and every move, and search outwards from , one move at a time, until the target appears. This is a breadth-first search: all positions one move away, then all those two moves away, and so on. A position first found after moves cannot be reached in fewer, so the first time a position with 4 in it appears, the number of moves is the fewest.
The search finds 16 reachable positions in all, and the first one with four litres in it is found after 6 moves:
| move | what you do | big jug | small jug |
|---|---|---|---|
| 1 | fill the 5-litre jug | 5 | 0 |
| 2 | pour the 5 into the 3 | 2 | 3 |
| 3 | empty the 3-litre jug | 2 | 0 |
| 4 | pour the 5 into the 3 | 0 | 2 |
| 5 | fill the 5-litre jug | 5 | 2 |
| 6 | pour the 5 into the 3 | 4 | 3 |
The 5-litre jug is filled, poured into the 3-litre jug, which leaves 2. The 3-litre jug is emptied, and the 2 is poured across. Fill the 5-litre jug again, and pour into the 3-litre jug, which already holds 2 and takes only 1 more. What is left in the big jug is 4.
The other classic route begins by filling the small jug. It takes 8 moves and ends with the big jug holding 4 and the small jug empty:
| move | what you do | big jug | small jug |
|---|---|---|---|
| 1 | fill the 3-litre jug | 0 | 3 |
| 2 | pour the 3 into the 5 | 3 | 0 |
| 3 | fill the 3-litre jug | 3 | 3 |
| 4 | pour the 3 into the 5 | 5 | 1 |
| 5 | empty the 5-litre jug | 0 | 1 |
| 6 | pour the 3 into the 5 | 1 | 0 |
| 7 | fill the 3-litre jug | 1 | 3 |
| 8 | pour the 3 into the 5 | 4 | 0 |
Draw the positions as points of a grid, with along the bottom and up the side, but slant the side by 60° so that the table is a parallelogram of width 5 and slanted height 3. Then the moves become straight lines:
Every move goes to a wall of the table: a fill or an empty goes until a jug is full or empty, and a pour goes until one jug empties or the other fills. And when the point arrives at a wall, the next move is the one that leaves along the mirror-image direction. A ball on a billiard table with this slant does exactly that: it travels in a straight line, hits a wall, and leaves at the same angle on the other side.
So the solution is a billiard shot. Shoot from the corner along the bottom and the ball bounces its way through the same 6 positions as the table above. Shoot along the slanted side instead and it follows the 8-move route. The script simulates the billiard from the bounce rule, move by move, and checks that both paths agree with the breadth-first search. The ball goes on until it runs into a corner, and it has by then visited 15 of the 16 positions: all but the one where both jugs are full, which is the opposite corner.
Every move ends with a jug empty or full, so every position after the first move lies on the edge of the table: , , or . The positions on the walls number , counting the lattice points round the edge once each, and the search finds that all 16 of them are reachable. The positions are:
(0, 0), (5, 0), (0, 3), (5, 3), (2, 3), (3, 0), (2, 0), (3, 3), (0, 2), (5, 1), (5, 2), (0, 1), (4, 3), (1, 0), (4, 0), (1, 3).
Suppose the jugs hold and litres and .
Nothing but multiples of . Filling a jug puts or litres in it, a multiple of . Emptying puts in zero. A pour moves some amount from one jug to the other, and the amount is the smaller of what is in the first jug and the room in the second, each of which is a multiple of if both jugs hold multiples of . So starting from , both jugs always hold a multiple of litres.
Everything else on the walls. The other direction, that every multiple of on the walls can be reached, is the billiard's work, and Bézout's identity says why: is a whole-number combination of and . The script checks it by search for the pairs , , , , and : in each, the reachable positions are exactly the points of the walls with both numbers a multiple of the greatest common divisor.
An example where it fails. Take a 6-litre jug and a 4-litre jug, with . Only even amounts can ever appear. The reachable positions are (0, 0), (6, 0), (0, 4), (6, 4), (2, 4), (4, 0), (2, 0), (4, 4), (0, 2), (6, 2), ten in all, and every coordinate in them is even. So 4 litres can be measured and 3 litres cannot: the script confirms that no reachable position has a 3 in it.
scripts/two-jugs.mjs asserts the claims below before it writes maths.json, and its --check fails if the file is stale. It checks that:
The "only multiples of " half of the general law is the argument above, which holds for all sizes. The "everything on the walls" half is checked for the six pairs listed and not proved here for others.
Yes. The positions include (2, 4), where the big jug has 2 litres, after filling the big jug and pouring it into the small one. Two is a multiple of the greatest common divisor, 2.
Every amount that can appear is a multiple of 2, since filling, emptying and pouring only ever move multiples of 2. Three is not.
Fill the 5-litre jug, giving (5, 0), then pour it into the 3-litre jug, which gives (2, 3).