Last active
January 4, 2021 22:28
-
-
Save MartinPavlik/f3edca5bad1a8a95c3d36e9456d5abe2 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
// https://ramdajs.com/repl/#?%2F%2F%20https%3A%2F%2Fphilipnilsson.github.io%2FBadness10k%2Ffunctional-waterflow%2F%0A%0Aconst%20input%20%3D%20%5B2%2C%205%2C%201%2C%202%2C%203%2C%204%2C%207%2C%207%2C%206%5D%0A%2F%2F%20const%20input%20%3D%20%5B2%2C%201%2C%203%2C%204%2C%201%2C%202%2C%201%2C%203%2C%202%2C%201%2C%200%2C%202%2C%204%2C%203%2C%201%2C%205%2C%203%2C%201%2C%204%2C%201%2C%202%2C%201%2C%200%2C%205%2C%202%2C%201%2C%203%5D%0A%0A%0A%2F%2F%20When%20is%20a%20one%20block%20%60b%60%20of%20%60height%28b%29%60%20filled%20with%20water%3F%0A%0A%2F%2F%20there%20is%20some%20bL%20%28on%20the%20left%29%20that%20has%20height%28bL%29%20%3E%20height%28b%29%0A%2F%2F%20and%20there%20is%20some%20bH%20on%20the%20right%20that%20has%20height%28bH%29%20%3E%20height%28b%29%0A%0Aconst%20mapIndexed%20%3D%20addIndex%28map%29%0A%0A%2F%2F%20For%20each%20index%20in%20the%20input%2C%20compute%20highest%20wall%20from%20the%20left%20side%0Aconst%20leftWalls%20%3D%20source%20%3D%3E%20map%28%0A%20%20i%20%3D%3E%20pipe%28%0A%20%20%20%20take%28i%29%2C%0A%20%20%20%20reduce%28max%2C%200%29%2C%0A%20%20%20%29%28source%29%0A%29%28range%280%2C%20source.length%29%29%0A%0Aconsole.log%28%27left%20walls%3A%27%2C%20leftWalls%28input%29%29%0A%0A%2F%2F%20The%20same%20just%20from%20the%20other%20side%0Aconst%20rightWalls%20%3D%20pipe%28reverse%2C%20leftWalls%2C%20reverse%29%0Aconsole.log%28%27right%20walls%3A%27%2C%20rightWalls%28input%29%29%0A%0Aconst%20data%20%3D%20zip%28input%2C%20zip%28%0A%20%20leftWalls%28input%29%2C%0A%20%20rightWalls%28input%29%0A%29%29%0A%0A%2F%2F%20For%20each%20i%20in%20input%2C%20compute%20how%20much%20of%20water%20it%20will%20keep%0Aconst%20contributions%20%3D%20map%28%28%5Bheight%2C%20%5BleftHeight%2C%20rightHeight%5D%5D%29%20%3D%3E%20%7B%0A%20%20const%20waterKept%20%3D%20min%28leftHeight%2C%20rightHeight%29%20-%20height%0A%20%20return%20max%280%2C%20waterKept%29%0A%7D%29%28data%29%0A%0A%0Asum%28contributions%29%0A%0A | |
// https://philipnilsson.github.io/Badness10k/functional-waterflow/ | |
const input = [2, 5, 1, 2, 3, 4, 7, 7, 6] | |
// const input = [2, 1, 3, 4, 1, 2, 1, 3, 2, 1, 0, 2, 4, 3, 1, 5, 3, 1, 4, 1, 2, 1, 0, 5, 2, 1, 3] | |
// When is a one block `b` of `height(b)` filled with water? | |
// there is some bL (on the left) that has height(bL) > height(b) | |
// and there is some bR on the right that has height(bR) > height(b) | |
const mapIndexed = addIndex(map) | |
// For each index in the input, compute highest wall from the left side | |
const leftWalls = source => map( | |
i => pipe( | |
take(i), | |
reduce(max, 0), | |
)(source) | |
)(range(0, source.length)) | |
console.log('left walls:', leftWalls(input)) | |
// The same just from the other side | |
const rightWalls = pipe(reverse, leftWalls, reverse) | |
console.log('right walls:', rightWalls(input)) | |
const data = zip(input, zip( | |
leftWalls(input), | |
rightWalls(input) | |
)) | |
// For each i in input, compute how much of water it will keep | |
const contributions = map(([height, [leftHeight, rightHeight]]) => { | |
const waterKept = min(leftHeight, rightHeight) - height | |
return max(0, waterKept) | |
})(data) | |
sum(contributions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment