Win a copy of The Java Performance Companion this week in the Performance forum!

# Need solution for below puzzle

asdw as
Greenhorn
Posts: 1
Water filling logic Puzzel:

Q : Fill 10 liter container with 3 liters.

10 liter container is empty
18 liter container is empty
29 liter container have 9 liters

we can fill the container from tap but we can't drop the water away(only drop with in containers above).

Myke Enriq
Ranch Hand
Posts: 115
Here is my solution , there are a number of steps , I will describe them by the quantities in the containers:

10 | 18 | 29
---------------
0 | 0 | 0
0 | 18 | 0 <- fill the 18 liters recipient
0 | 0 | 18 <- pour the 18 liters recipient into the 29 liters recipient
0 | 18 | 18 <- fill the 18 liters recipient
0 | 11 | 29 <- pour the 18 liters recipient into the 29 liters recipient
10 | 11 | 19 <- fill the 10 liters recipient
3 | 18 | 19 <- pour the 10 liters recipient into the 18 liters one => 3 liters in the first recipient

dennis deems
Ranch Hand
Posts: 808
According to the problem statement there were already 9 liters in the 29 liter container.

Jayesh A Lalwani
Rancher
Posts: 2756
32
If we can;t drop the water, can we have an elephant drink it? :p Just joking

Here's the solution that I got

10 | 18 | 29
---------------
0 | 0 | 9
10| 0 | 9 <-- add 10 to 10L
0 | 0 | 19 <-- move 10 from 10L to 29L
0 | 18 | 1 <-- move 18 fro 29L to 18L
10 | 8 | 1 --- move 10 from 18L to 10L
0 | 8 | 11 <- move 10 from 10L to 29L
8 | 0 | 11<-- move 8 from 18L to 10L
10 | 0 | 11 <-- add 2 to 10L
0 | 10 | 11 <-- move 10 from 10L to 18L.. now there is 8 empty in 18L
0 | 18 | 3 <-- move 8 from 29L to 18L
3 | 18 | 0 <-- move 3 from 29L to 10L

Done!

Jayesh A Lalwani
Rancher
Posts: 2756
32
Myke Enriq wrote:Here is my solution , there are a number of steps , I will describe them by the quantities in the containers:

10 | 18 | 29
---------------
0 | 0 | 0
0 | 18 | 0 <- fill the 18 liters recipient
0 | 0 | 18 <- pour the 18 liters recipient into the 29 liters recipient
0 | 18 | 18 <- fill the 18 liters recipient
0 | 11 | 29 <- pour the 18 liters recipient into the 29 liters recipient
10 | 11 | 19 <- fill the 10 liters recipient
3 | 18 | 19 <- pour the 10 liters recipient into the 18 liters one => 3 liters in the first recipient

How do you do this

0 | 18 | 18 <- fill the 18 liters recipient
0 | 11 | 29 <- pour the 18 liters recipient into the 29 liters recipient

It should be

0 | 18 | 18 <- fill the 18 liters recipient
0 | 7 | 29 <- pour the 18 liters recipient into the 29 liters recipient