Container Puzzle Algorithm: Measure 1-10 litres, given 3, 7, and 10 litre containers, with the largest being full of water.

0 / 1376
Container Puzzle

Container Puzzle Solution:  

Given three containers 3, 7, and 10 litres respectively with the largest being full of water, determine a method of measuring anything between 1 to 10 litres using these containers alone. If any quantity isn’t measurable deduce accordingly. Give solution as well as algorithm for the container puzzle.   This blog will also give an algorithm for solving this puzzle:   1)  Store initial state of containers with their capacity in an array, for e.g. [0,0,10] in this case.   2) From State S (0,0,10), determine all possible configurations achievable by transferring liquids between containers, checking each time, the liquid that the containers can hold further.   3) Store each state obtained after each transfer in a list for revisiting and discarding invalid states.   4) If any of the states obtained as a result of Step 2), are already in the global list or contain same 3 numbers (quantities), discard them.   5) Add valid state at each level to an N-Ary tree.   6) Some states could be exceptions and may not obey Rule #4. In that case, count the number of attempts in reaching next state. If that exceeds 4 (4 is the highest number of states that can be obtained by liquid transfers between 3 containers given the capacities in this case. We can have generic number of attempts before reevaluating which can be NP2  where N= number of containers), reevaluate/backtrack the states obtained at 4 and pick one of the states that gives a different next State Sn   7) Continue iterating and adding new states until the liquid quantity to be measured is reached. Terminate and add this state to the tree which will be the highest leaf of the tree.     Note:- A state S which will represent node of a tree can have a maximum of 4 children in this case as the number of containers is 3, say for e.g. S1,S2,S3,S4.  Consider and add only valid states out of these as children of that state S. An N-Ary tree is the best data structure suited for solving puzzles like this.     See the DEMO below which will print out the tree from initial to final state depending on the quantity entered for measurement.         From the output of this tree, it’s very easy to trace the route till the destination state and print it out. Read more about tree route tracing algorithm :- DEPTH OF AN N-ARY TREE AND FINDING ROUTE TO A NODE IN AN N-ARY TREE     Below is the complete code that runs the show behind the DEMO:-  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
MIT License

Copyright (c) [2017] [Golibrary.co]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@Author: Saurabh Gattani
Email:- saurabh.gattani@gmail.com
*/





/*  0 0 10
           

           
            3 0 7           0 7 3
           
    0 0 10      3 7 0   3 4 3   3 7 0
                            |
                            |
                            |
                        3 7 0     0 4 6
                                    |
                                    |
                                    |
                                3 1 6   0 7 3   0 0 10  3 4 3
                                    |
                                    |
                                    |
                            0 4 6   0 1 9
                                    |
                                    |
                                    |
                                    3 1 6  1 0 9  0 0 10
                                            |
                                            |
                                            |
                                    7 0 3   1 7 2  0 1 9
                                            |
                                            |
                                            |
                                    3 7 0  3 5 2    1 0 9    0 7 3     
                                           
                                           
0 7 3  
3 4 3
0 4 6
3 1 6
0 1 9
1 0 9
1 7 2
3 5 2
*/



   
var capacities = [3, 7, 10],
    globalList = [];

var count = 1,
    numOfTries;

// N-ary tree Node constructor
function Node(data) {
    this.data = this.head = data;
    this.children = [];
    this.id = guid();
}
// NaryTree Constructor
function NaryTree() {
    this.contents = this.head = [];
    this.children = [];
    this.id = guid();
}

function solvePuzzle() {
    numOfTries = 1,
        count = 1;
    var naryTree = new NaryTree();

    var treeJSON = [(insertNode(naryTree, [0, 0, 10]))];

    // remove all empty children
    clearProperty(treeJSON);

    if (!$('#tree').is(':empty')) {
        $('#tree').empty();
    }

    // empty tree check
    if (treeJSON[0]) {
        $("#tree").nadyTree({
            callType: 'obj',
            structureObj: treeJSON
        });
    }

    // empty all JSON and globalList
    clearProperty(treeJSON, 'ALL');
    clearProperty(naryTree, 'ALL');
    globalList = [];
}



function insertNode(naryTree, data) {
    var tree = null;
    var matchFound = false;
    if (naryTree.contents.length === 0) { // empty tree condition
        naryTree.contents = data.slice(); // store this in global array
        globalList.push(naryTree.contents);
        naryTree.head = 1;
        naryTree.id = guid();
        pushChildren(naryTree.children, solve(data, 0));
    } else {
        // traverse tree to leaf nodes and insert here
        // below for loop logic needs to be changed
        var leaves = getLeafNodes([naryTree]).slice();
        for (var p = 0; p < leaves.length; p++) {
            // search node and insert children in matched node only
            var parentToInsert = traverse(leaves, 'contents', leaves[p].contents.slice());
            var nextStates = solve(leaves[p].contents.slice(), 0);
            var states = [];
            // remove infinite loop causing nodes from nextStates by checking from global list and removing cyclic nodes
            nextStates.some(function(itemArr) {
                if (!inTree(itemArr)) {
                    states.push(itemArr);
                }
            });

            // exception case where the logic of isCyclic needs to be re-evaluated
            if (numOfTries > 4) {
                states = reevaluate(nextStates); // backtrack here
                // reset number of solve attempts at the level
                //numOfTries = 0;
            }

            pushChildren(parentToInsert.children, states);
            // check if solve returns the quantity to be measured. If YES then break.
            matchFound = nextStates.some(function(arr) {
                return arr.some(function(item) {
                    return item === parseInt($("#quantity").val());
                });
            });
        }
    }

    if (numOfTries > 10) {
        alert("Quantitity cannot be measured");
        return [];
    }

    if (!matchFound) {
        numOfTries++;
        naryTree = insertNode(naryTree, data);
    }
    return naryTree;
}

function reevaluate(statesArr) {
    var state = [];
    var flag = false;
    for (var index = 0; index < statesArr.length; index++) {
        var retStates = solve(statesArr[index].slice(), 0);
        flag = retStates.some(function(itemArr) {
            if (!inTree(itemArr)) {
                state = [itemArr];
                return itemArr;
            }
        });
        if (flag) {
            return state;
        }
    }
    return state;
}

function pushChildren(parent, children) {
    for (var index = 0; index < children.length; index++) {
        var node = new Node();
        node.contents = children[index].slice(); // store this in global array
        globalList.push(node.contents);
        node.head = ++count;
        node.id = guid();
        node.children = [];
        parent.push(node);
    }
}

function traverse(object, key, value) {
    var result = null;
    for (var i in object) {
        if (!!object[i] && typeof(object[i]) === "object") {
            if (object[i][key] && isEqual(object[i][key], value)) {
                return object[i];
            } else {
                result = traverse(object[i], key, value);
            }
        }
    }
    return result;
}




function clearProperty(JsonObj, isALL = false) {
    $.each(JsonObj, function(key, value) {
        if (isALL || value instanceof Array && value.length === 0 && JsonObj[key]) {
            delete JsonObj[key];
        } else if (typeof(value) === "object") {
            JsonObj[key] = clearProperty(value);
        }
    });
    return JsonObj;
}

function getLeafNodes(nodes, result = []) {
    for (var i = 0, length = nodes.length; i < length; i++) {
        if (!nodes[i].children.length) {
            result.push(nodes[i]);
        } else {
            result = getLeafNodes(nodes[i].children, result);
        }
    }
    return result;
}

function inTree(node) {
    var a1 = node.slice();
    var flag = globalList.some(function(arr) {
        var a2 = arr.slice();
        return isCyclic(a1, a2);
    });
    //console.log("Searching for", node, "in ", globalList, "Flag = ", flag);
    return flag;
}

function isCyclic(a1, a2) {
    var found = false;
    var a3 = a1.slice().sort();
    var a4 = a2.slice().sort();
    return a3.every(function(v, i) {
        return (v === a4[i]);
    });
    //return found;
}

function isEqual(a1, a2) {
    return a1 && a2 && a1.length === a2.length && a1.every(function(v, i) {
        return v === a2[i]
    });
}


function solve(quantities, count) {
    var source = [],
        destination = [],
        result = [];
    for (var i = 0; i < quantities.length; i++) {
        if (quantities[i] < capacities[i]) {
            destination.push(i);
        }
        if (quantities[i] > 0) {
            source.push(i);
        }
    }

    // clone the previous quantities configuration
    var savedQuantities = quantities.slice();

    for (var x = 0; x < source.length; x++) {
        for (var y = 0; y < destination.length; y++) {
            if (source[x] !== destination[y] && quantities[destination[y]] < capacities[destination[y]]) {
                var liquidToAdd = capacities[destination[y]] - quantities[destination[y]],
                    liquidToSubtract = (liquidToAdd > quantities[source[x]]) ? quantities[source[x]] : liquidToAdd;
                quantities[destination[y]] = quantities[destination[y]] + liquidToSubtract;
                quantities[source[x]] = quantities[source[x]] - liquidToSubtract;
                // restore the previous quantities configuration from cloned copy
                result.push(quantities);
                quantities = savedQuantities.slice();
            }
        }
    }

    return result;
}
    Hope you enjoyed reading this. Subscribe to Golibrary for more interesting reads like this!!

Comments

comments


An avid reader, responsible for generating creative content ideas for golibrary.co. His interests include algorithms and programming languages. Blogging is a hobby and passion.

Related Posts