All games

Water Jug Problem: Solutions Step by Step

See each classic answer one pour at a time, or enter your jugs to get the fewest steps.

Play the Water Jug Puzzle (free)

Keep pouring in one direction to reach the goal

  1. If the jug you pour into is full, empty it
  2. If the jug you pour from is empty, fill it
  3. Otherwise pour from it into the other jug

3 and 5 gal jugs + tap: make 4 gal

0
3 gal
0
5 gal
01234501235 gal jug →↑ 3 gal jugGoal (4 gal)

Starting point

This way takes 6 steps (the fewest)

Try both orders. The shorter one is the fewest steps.

Die Hard riddle: 3 and 5 make 4 in 6 steps

In Die Hard with a Vengeance (1995), the heroes need exactly 4 gallons from a fountain.

Step3 gal5 galAction
000Start
105Fill 5 gal
2325 gal → 3 gal
302Empty 3 gal
4205 gal → 3 gal
525Fill 5 gal
6345 gal → 3 gal

Filling the 3-gallon jug first takes 8 steps.

4 and 3 gallons: 2 gallons in the 4-gallon jug takes 6 steps

This one is a classic exercise in AI courses. Fill the 3-gallon jug first:

Step4 gal3 galAction
000Start
103Fill 3 gal
2303 gal → 4 gal
333Fill 3 gal
4423 gal → 4 gal
502Empty 4 gal
6203 gal → 4 gal

If either jug can hold the 2 gallons, stop at step 4.

8, 5 and 3: split 8 gallons in half in 7 steps

There is no tap: the 8-gallon jug starts full, and you fill from it and empty into it.

Step8 gal5 gal3 galAction
0800Start
13508 gal → 5 gal
23235 gal → 3 gal
36203 gal → 8 gal
46025 gal → 3 gal
51528 gal → 5 gal
61435 gal → 3 gal
74403 gal → 8 gal

Going 8 → 3 → 5 → 8 instead takes 8 steps.

It’s solvable when the target is a multiple of the greatest common divisor

Enter your jugs to get the fewest steps

Up to 4 jugs of up to 30 gallons. Handy for checking homework answers.

JugSizeStartGoal
A
B
C
D

gallons in any jug

The solver uses breadth-first search

FAQ

Can I pour only part of a jug?
No. With no markings, you pour until one jug is empty or the other is full
Do filling and emptying count as steps?
Yes, one step each (in the game and the solver too)
Is the shorter order always the fewest steps?
Yes with two jugs and a tap (705 checked) and for splitting in half (864). Other goals can be quicker
Does it work in liters?
Yes. Only the numbers matter, so the steps are the same in any unit
How do I solve LeetCode 365, Water and Jug Problem?
Water in both jugs counts, so it’s possible when the target is at most x + y and a multiple of gcd(x, y)
Can I practice?
The game has 60 puzzles and a daily puzzle. Hint shows the next step of a shortest solution
Play the Water Jug Puzzle (free)