Created
October 9, 2020 07:54
-
-
Save vercetti11/31b10fb9265a6d081953f6b5921faa52 to your computer and use it in GitHub Desktop.
Calculate reachable area
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
// Helper function | |
function countUpUntil(endNum) { | |
return 1 === endNum | |
? [1] | |
: [...countUpUntil(endNum - 1), endNum ]; | |
}; | |
function calculateReachableArea(maxSide){ | |
const axisArea = maxSide * 4 + 1 // + 1 is for the [0,0] cell | |
const areaInsideQuarter = countUpUntil(maxSide - 1) // generate inner columns values | |
.reduce((sum, current) => sum + current, 0) // sum columns area | |
return axisArea + (4 * areaInsideQuarter) | |
} | |
console.log(calculateReachableArea(23)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment