PuzzleReels
Sections

The Two Jugs

The maths · Episode 32 · 2:43 · gives the answer

The puzzle

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?

The maths, properly

Write a position as a pair (x,y), where x is the number of litres in the 5-litre jug and y the number in the 3-litre jug. You start at (0,0). Each move takes you from one position to another, and the question is whether (4,y) or (x,4) is reachable, and by how short a route.

Route one: search the positions

List every position and every move, and search outwards from (0,0), 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 k 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:

movewhat you dobig jugsmall jug
1fill the 5-litre jug50
2pour the 5 into the 323
3empty the 3-litre jug20
4pour the 5 into the 302
5fill the 5-litre jug52
6pour the 5 into the 343

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:

movewhat you dobig jugsmall jug
1fill the 3-litre jug03
2pour the 3 into the 530
3fill the 3-litre jug33
4pour the 3 into the 551
5empty the 5-litre jug01
6pour the 3 into the 510
7fill the 3-litre jug13
8pour the 3 into the 540

Route two: a billiard table

Draw the positions as points of a grid, with x along the bottom and y 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:

  • filling or emptying the big jug changes only x, so it moves a point along the bottom direction, at 0°;
  • filling or emptying the small jug changes only y, so it moves a point along the slanted side, at 60°;
  • pouring changes x and y by opposite amounts, so x+y stays the same, and the point moves along the third direction, at 120°.

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 (0,0) 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.

Why only the walls

Every move ends with a jug empty or full, so every position after the first move lies on the edge of the table: x=0, x=5, y=0 or y=3. The positions on the walls number 2×(5+3)=16, 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).

The general law

Suppose the jugs hold a and b litres and g=gcd(a,b).

Nothing but multiples of g. Filling a jug puts a or b litres in it, a multiple of g. 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 g if both jugs hold multiples of g. So starting from (0,0), both jugs always hold a multiple of g litres.

Everything else on the walls. The other direction, that every multiple of g on the walls can be reached, is the billiard's work, and Bézout's identity says why: g is a whole-number combination of a and b. The script checks it by search for the pairs (5,3), (6,4), (6,2), (10,6), (11,7) and (9,3): 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 gcd=2. 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.

Checking 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 search from (0,0) with jugs of 5 and 3 reaches exactly 16 positions, the number of lattice points on the walls;
  • the shortest route to a position holding four litres is 6 moves, and is the route in the table above;
  • the billiard simulation, with the rule of reflection written out for the 60° table, gives the 6-move route when it starts along the bottom, and the 8-move route when it starts along the side;
  • for the six pairs of jug sizes above, the reachable positions are exactly the points on the walls whose coordinates are both multiples of gcd(a,b);
  • with jugs of 6 and 4, every reachable position is even, no position has 3 litres in it and some position has 4.

The "only multiples of g" 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.

Where this shows up

  • State-space search. Treat each situation as a point and each legal move as a step, and search outwards. The same method solves sliding puzzles, and finds routes in maps.
  • The greatest common divisor. The question of which amounts you can make from two sizes is the question of which whole-number combinations ma+nb exist, which is the content of Bézout's identity. Euclid's algorithm is the same pouring done on numbers.
  • Billiards. Unfolding a bouncing path into a straight line is a standard trick, used on rectangular tables and on the slanted table here. In the film it is what turns a sequence of pours into one line.

Try it yourself

1. With jugs of 6 and 4 litres, can you measure 2 litres?

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.

2. With jugs of 6 and 4 litres, why can you never measure 3 litres?

Every amount that can appear is a multiple of 2, since filling, emptying and pouring only ever move multiples of 2. Three is not.

3. What are the first two moves of the 6-move solution, and where are the jugs after them?

Fill the 5-litre jug, giving (5, 0), then pour it into the 3-litre jug, which gives (2, 3).