Skip to content

Instantly share code, notes, and snippets.

@vercetti11
Created October 9, 2020 07:54
Show Gist options
  • Save vercetti11/31b10fb9265a6d081953f6b5921faa52 to your computer and use it in GitHub Desktop.
Save vercetti11/31b10fb9265a6d081953f6b5921faa52 to your computer and use it in GitHub Desktop.
Calculate reachable area
// 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