Last active
August 29, 2015 14:14
-
-
Save martineno/73dc35021448fa2c14a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simulation written to help experiment with the well known water puzzle: | |
* | |
* If you have two unmarked jugs: a 5 gallon one and a 3 gallon. Provide a series of steps | |
* to fill one of the jugs with 4 gallons of water. | |
* This doesn't acutally solve the puzzle, but provides constraints to let the user experiment | |
* with the puzzle. | |
* | |
* The constraints are: | |
* # You can drain a jug completely see: drain | |
* # You can fill a jug completely from tap. See: fill | |
* # You can fill a jug from a another jug, where you will fill the current jug | |
* to capacity, and the rest of the water, if any, remain in the jug that is | |
* the source. | |
**/ | |
function WaterContainer(capacity) { | |
if (typeof capacity !== 'number' && number <= 0) { | |
throw new TypeError('capacity has to be > 0'); | |
} | |
this.capacity = capacity; | |
this.currentVolume = 0; | |
} | |
WaterContainer.prototype.fill = function() { | |
this.currentVolume = this.capacity; | |
return this; | |
}; | |
WaterContainer.prototype.drain = function() { | |
this.currentVolume = 0; | |
return this; | |
}; | |
WaterContainer.prototype.drainBy = function(amount) { | |
if (typeof amount !== 'number' && number <= 0) { | |
throw new TypeError('amount has to be > 0'); | |
} | |
this.currentVolume = Math.max(this.currentVolume - amount, 0); | |
return this; | |
}; | |
WaterContainer.prototype.fillFrom = function(otherWaterContainer) { | |
var amountPouredIntoThisContainer = Math.min(this.remainingCapacity(), otherWaterContainer.currentVolume); | |
otherWaterContainer.drainBy(amountPouredIntoThisContainer); | |
this.currentVolume = this.currentVolume + amountPouredIntoThisContainer; | |
return this; | |
}; | |
WaterContainer.prototype.toString = function() { | |
return 'Capacity: ' + this.capacity + ', CurrentVolume: ' + this.currentVolume; | |
}; | |
WaterContainer.prototype.remainingCapacity = function() { | |
return this.capacity - this.currentVolume; | |
}; | |
module.exports = WaterContainer; | |
// > var WaterContainer = require('./watercontainer.js') | |
// undefined | |
// > var jug5 = new WaterContainer(5); | |
// undefined | |
// > var jug3 = new WaterContainer(3); | |
// undefined | |
// | |
// Fill the 5 gallon jug | |
// | |
// > jug5.fill() | |
// { capacity: 5, currentVolume: 5 } | |
// > jug3.fillFrom(jug5) | |
// { capacity: 3, currentVolume: 3 } | |
// > jug5 | |
// { capacity: 5, currentVolume: 2 } | |
// > jug3.drain() | |
// { capacity: 3, currentVolume: 0 } | |
// > jug3.fillFrom(jug5) | |
// { capacity: 3, currentVolume: 2 } | |
// > jug3 | |
// { capacity: 3, currentVolume: 2 } | |
// > jug5 | |
// { capacity: 5, currentVolume: 0 } | |
// > jug5.fill() | |
// { capacity: 5, currentVolume: 5 } | |
// > jug3.fillFrom(jug5) | |
// { capacity: 3, currentVolume: 3 } | |
// > jug5 | |
// { capacity: 5, currentVolume: 4 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment