The other day, my colleague presented to us a simple 3 jar problem.
There are 3 jars of capacity 19l, 13l and 7l with the latter two filled to its capacity (say with water).
The challenge is to transfer the liquid across these 3 jars and arrive at 10l each in the 19l and 13l jar.
This is one of those typical high school or college level puzzles that we tried and gave up out of frustration. With some perseverance, it was solvable indeed. No … this post isn’t about how to solve this by hand. Google would do a great job at that.
However, the solution has these limitations.
(1) Works on problems where there IS a solution. For example, a final configuration of 10, 5, 5 is not attainable. Currently I don’t know how to check for feasible goal states. So this program hangs in such cases.
(2) Basic validations and error handling not yet in place
As usual, V pointed out some improvements. V will be posting a python solution as well.
The results for this problem, as it appears in JsEnv is given below.
Running in bookmarklet mode…
Suggestions, improvements most welcome.
Update : Here is a 4 jar problem. The program solve it (though not in the least number of steps.)
jars = [10, 10, 5, 4]
fill = [10,10,0,0]
goal = [4,10,3,3]