Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 16691ee

Browse files
committed
Merge pull request #68 from lindslev/master
two-bucket submission!
2 parents 3cbade7 + 3301d63 commit 16691ee

File tree

4 files changed

+236
-1
lines changed

4 files changed

+236
-1
lines changed

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"saddle-points",
6262
"ocr-numbers",
6363
"meetup",
64-
"bracket-push"
64+
"bracket-push",
65+
"two-bucket"
6566
],
6667
"deprecated": [
6768
"point-mutations"

two-bucket/example-2.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//alternate solution written by Fullstack student, Griffin Telljohann
2+
'use strict';
3+
4+
function TwoBucket(bucket1, bucket2, goal, startingBucket) {
5+
this.b1max = bucket1;
6+
this.b2max = bucket2;
7+
this.goalAmount = goal;
8+
this.states = {};
9+
var invalid = (startingBucket == "one" ? 0 : this.b1max) + "," + (startingBucket == "two" ? 0 : this.b2max)
10+
this.beenHere = {};
11+
this.beenHere[invalid] = true;
12+
var bestSolution = this.solve((startingBucket == "one" ? this.b1max : 0), (startingBucket == "two" ? this.b2max : 0))
13+
this.goalBucket = bestSolution.goalBucket;
14+
this.otherBucket = bestSolution.otherBucketFill;
15+
this.minMoves = 1 + bestSolution.numMoves;
16+
}
17+
18+
TwoBucket.prototype.moves = function() {
19+
return this.minMoves;
20+
};
21+
22+
TwoBucket.prototype.solve = function(bucket1fill, bucket2fill) {
23+
// if you've already been in this state, return
24+
if (this.beenHere[bucket1fill + "," + bucket2fill]) return {goalBucket: null};
25+
else this.beenHere[bucket1fill + "," + bucket2fill] = true;
26+
27+
// if either bucket is filled to the goal amount, you've found a solution
28+
if (bucket1fill == this.goalAmount) {
29+
return {numMoves: 0, goalBucket: "one", otherBucketFill: bucket2fill};
30+
}
31+
if (bucket2fill == this.goalAmount) {
32+
return {numMoves: 0, goalBucket: "two", otherBucketFill: bucket1fill};
33+
}
34+
35+
if (this.states[bucket1fill + "," + bucket2fill]) {
36+
return this.states[bucket1fill + "," + bucket2fill];
37+
}
38+
39+
40+
var testObj, bestSolution = {goalBucket: null};
41+
// fill bucket 1 to top
42+
if (bucket1fill !== this.b1max && bucket2fill !== this.b2max) {
43+
testObj = this.solve(this.b1max, bucket2fill);
44+
bestSolution = betterSolution(testObj, bestSolution);
45+
}
46+
47+
// fill bucket 2 to top
48+
if (bucket1fill !== this.b1max && bucket2fill !== this.b2max) {
49+
testObj = this.solve(bucket1fill, this.b2max);
50+
bestSolution = betterSolution(testObj, bestSolution);
51+
}
52+
53+
// empty bucket 1
54+
if (bucket1fill !== 0 && bucket2fill !== 0) {
55+
//console.log("empty 1");
56+
testObj = this.solve(0, bucket2fill);
57+
bestSolution = betterSolution(testObj, bestSolution);
58+
}
59+
60+
// empty bucket 2
61+
if (bucket1fill !== 0 && bucket2fill !== 0) {
62+
//console.log("empty 2");
63+
testObj = this.solve(bucket1fill, 0);
64+
bestSolution = betterSolution(testObj, bestSolution);
65+
}
66+
67+
var totalAmount = bucket1fill + bucket2fill;
68+
// pour bucket 1 into bucket 2
69+
if (bucket2fill !== this.b2max) {
70+
if (totalAmount <= this.b2max) {testObj = this.solve(0, totalAmount);}
71+
else {testObj = this.solve(totalAmount - this.b2max, this.b2max);}
72+
bestSolution = betterSolution(testObj, bestSolution);
73+
}
74+
75+
// pour bucket 2 into bucket 1
76+
if (bucket1fill !== this.b1max) {
77+
if (totalAmount <= this.b1max) testObj = this.solve(totalAmount, 0);
78+
else testObj = this.solve(this.b1max, totalAmount - this.b1max);
79+
bestSolution = betterSolution(testObj, bestSolution);
80+
}
81+
82+
this.states[bucket1fill + "," + bucket2fill] = bestSolution;
83+
if (typeof bestSolution.numMoves === "number") {
84+
bestSolution.numMoves++;
85+
}
86+
return bestSolution;
87+
}
88+
89+
function betterSolution(test, currentBest) {
90+
if (test.goalBucket) {
91+
if (!currentBest || !currentBest.goalBucket || test.numMoves < currentBest.numMoves) {
92+
return test;
93+
}
94+
}
95+
return currentBest;
96+
}
97+
98+
module.exports = TwoBucket;

two-bucket/example.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
function TwoBucket(x,y,z,starter) {
4+
this.starter = starter;
5+
this.x = x;
6+
this.y = y;
7+
8+
this.reachedGoal = function(measurements) {
9+
var reached = false;
10+
if(measurements[0] == z || measurements[1] == z) {
11+
if(measurements[0] == z) {
12+
this.goalBucket = 'one';
13+
this.otherBucket = measurements[1];
14+
} else {
15+
this.goalBucket = 'two';
16+
this.otherBucket = measurements[0];
17+
}
18+
reached = true;
19+
}
20+
return reached;
21+
}
22+
23+
this.bigFirst = function(measurements, moveCount, prBool) {
24+
var j = measurements[0], k = measurements[1];
25+
while(true) {
26+
if(this.reachedGoal(measurements)) break;
27+
if(k > x && j == 0 && moveCount == 0) {
28+
j = x;
29+
k = y - j;
30+
} else if(j == x) {
31+
j = 0;
32+
} else if((k > x && j !== 0) || (k > x && prBool)) {
33+
k = k - (x-j);
34+
j = x;
35+
} else if(k > x || j == 0) {
36+
j = k;
37+
k = k - j;
38+
} else if(k == 0) {
39+
k = y;
40+
}
41+
measurements = [j,k];
42+
moveCount++;
43+
prBool ? prBool = false : prBool = true;
44+
}
45+
return moveCount;
46+
}
47+
48+
this.smallFirst = function(measurements, moveCount, prBool) {
49+
var j = measurements[0], k = measurements[1];
50+
while(true) {
51+
if(this.reachedGoal(measurements)) break;
52+
if(j == x && moveCount == 0) {
53+
j = 0;
54+
k = x;
55+
} else if(j == 0) {
56+
j = x;
57+
} else if(j == x && k < y) {
58+
var tempK = k;
59+
k + j > y ? k = y : k = tempK + j;
60+
tempK + j > y ? j = j - (y- tempK) : j = 0;
61+
} else if(k == y) {
62+
k = 0;
63+
} else if(k == 0 && j < x) {
64+
k = j;
65+
j = 0;
66+
}
67+
measurements = [j,k];
68+
moveCount++;
69+
prBool ? prBool = false : prBool = true;
70+
}
71+
return moveCount;
72+
}
73+
}
74+
75+
TwoBucket.prototype.moves = function() {
76+
var j = 0, k = 0; //j will be running val of bucket one, k = running val of bucket two
77+
this.starter == 'one' ? j = this.x : k = this.y;
78+
var measurements = [j,k];
79+
var moveCount = 0;
80+
var prBool = true; // pour / receive boolean - need to pour or receive every other turn
81+
if(this.starter == 'one') {
82+
moveCount = this.smallFirst(measurements, moveCount, prBool);
83+
} else {
84+
moveCount = this.bigFirst(measurements, moveCount, prBool);
85+
}
86+
return moveCount + 1; //accounts for first move made before loop (and moveCount starts at zero before loop)
87+
}
88+
89+
module.exports = TwoBucket;

two-bucket/two-bucket_test.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var TwoBucket = require('./two-bucket');
2+
3+
describe('TwoBucket', function(){
4+
describe('works for input of 3,5,1', function(){
5+
var buckOne = 3;
6+
var buckTwo = 5;
7+
var goal = 1;
8+
9+
it('starting with bucket one', function(){
10+
var starterBuck = 'one'; //indicates which bucket to fill first
11+
var twoBucket = new TwoBucket(buckOne,buckTwo,goal,starterBuck);
12+
expect(twoBucket.moves()).toEqual(4); //includes the first fill
13+
expect(twoBucket.goalBucket).toEqual('one'); //which bucket should end up with the desired # of liters
14+
expect(twoBucket.otherBucket).toEqual(5); //leftover value in the "other" bucket once the goal has been reached
15+
});
16+
17+
it('starting with bucket two', function(){
18+
var starterBuck = 'two';
19+
var twoBucket = new TwoBucket(buckOne,buckTwo,goal,starterBuck);
20+
expect(twoBucket.moves()).toEqual(8);
21+
expect(twoBucket.goalBucket).toEqual('two');
22+
expect(twoBucket.otherBucket).toEqual(3);
23+
});
24+
});
25+
26+
describe('works for input of 7,11,2', function(){
27+
var buckOne = 7;
28+
var buckTwo = 11;
29+
var goal = 2;
30+
31+
it('starting with bucket one', function(){
32+
var starterBuck = 'one';
33+
var twoBucket = new TwoBucket(buckOne,buckTwo,goal,starterBuck);
34+
expect(twoBucket.moves()).toEqual(14);
35+
expect(twoBucket.goalBucket).toEqual('one');
36+
expect(twoBucket.otherBucket).toEqual(11);
37+
});
38+
39+
it('starting with bucket two', function(){
40+
var starterBuck = 'two';
41+
var twoBucket = new TwoBucket(buckOne,buckTwo,goal,starterBuck);
42+
expect(twoBucket.moves()).toEqual(18);
43+
expect(twoBucket.goalBucket).toEqual('two');
44+
expect(twoBucket.otherBucket).toEqual(7);
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)