Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
86 lines (79 loc) · 3.27 KB
/
Copy pathexample.js
File metadata and controls
86 lines (79 loc) · 3.27 KB
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
'use strict';
function TwoBucket(bucketOne, bucketTwo, goal, startBucket) {
this.bucketOne = bucketOne;
this.bucketTwo = bucketTwo;
this.goal = goal;
this.startBucket = startBucket;
this.reachedGoal = function (currentBucketOne, currentBucketTwo) {
return (currentBucketOne === goal || currentBucketTwo === goal);
};
this.recordGoal = function (currentBucketOne, currentBucketTwo) {
if (currentBucketOne === goal) {
this.goalBucket = 'one';
this.otherBucket = currentBucketTwo;
} else {
this.goalBucket = 'two';
this.otherBucket = currentBucketOne;
}
};
this.bigFirst = function (initialBucketOne, initialBucketTwo) {
var currentBucketOne = initialBucketOne;
var currentBucketTwo = initialBucketTwo;
var moveCount = 0;
var pourOrReceive = true;
while (!this.reachedGoal(currentBucketOne, currentBucketTwo)) {
if (currentBucketTwo > bucketOne && currentBucketOne === 0 && moveCount === 0) {
currentBucketOne = bucketOne;
currentBucketTwo = bucketTwo - currentBucketOne;
} else if (currentBucketOne === bucketOne) {
currentBucketOne = 0;
} else if ((currentBucketTwo > bucketOne && currentBucketOne !== 0) || (currentBucketTwo > bucketOne && pourOrReceive)) {
currentBucketTwo = currentBucketTwo - (bucketOne - currentBucketOne);
currentBucketOne = bucketOne;
} else if (currentBucketTwo > bucketOne || currentBucketOne === 0) {
currentBucketOne = currentBucketTwo;
currentBucketTwo = currentBucketTwo - currentBucketOne;
} else if (currentBucketTwo === 0) {
currentBucketTwo = bucketTwo;
}
moveCount++;
pourOrReceive ? pourOrReceive = false : pourOrReceive = true;
}
this.recordGoal(currentBucketOne, currentBucketTwo);
return moveCount;
};
this.smallFirst = function (initialBucketOne, initialBucketTwo) {
var currentBucketOne = initialBucketOne;
var currentBucketTwo = initialBucketTwo;
var moveCount = 0;
var pourOrReceive = true;
while (!this.reachedGoal(currentBucketOne, currentBucketTwo)) {
if (currentBucketOne === bucketOne && moveCount === 0) {
currentBucketOne = 0;
currentBucketTwo = bucketOne;
} else if (currentBucketOne === 0) {
currentBucketOne = bucketOne;
} else if (currentBucketOne === bucketOne && currentBucketTwo < bucketTwo) {
var temp = currentBucketTwo;
currentBucketTwo + currentBucketOne > bucketTwo ? currentBucketTwo = bucketTwo : currentBucketTwo = temp + currentBucketOne;
temp + currentBucketOne > bucketTwo ? currentBucketOne = currentBucketOne - (bucketTwo - temp) : currentBucketOne = 0;
} else if (currentBucketTwo === bucketTwo) {
currentBucketTwo = 0;
} else if (currentBucketTwo === 0 && currentBucketOne < bucketOne) {
currentBucketTwo = currentBucketOne;
currentBucketOne = 0;
}
moveCount++;
pourOrReceive ? pourOrReceive = false : pourOrReceive = true;
}
this.recordGoal(currentBucketOne, currentBucketTwo);
return moveCount;
};
this.moves = function () {
if (this.startBucket === 'one') {
return this.smallFirst(this.bucketOne, 0) + 1;
}
return this.bigFirst(0, this.bucketTwo) + 1;
};
}
module.exports = TwoBucket;