Skip to content

Instantly share code, notes, and snippets.

@EricHech
Created December 2, 2018 08:57
Show Gist options
  • Save EricHech/ec9f068f542e1fc6342c0d4a44c44d8c to your computer and use it in GitHub Desktop.
Save EricHech/ec9f068f542e1fc6342c0d4a44c44d8c to your computer and use it in GitHub Desktop.
An answer to a version of the "find the number of paths" CC
// If a square is divided diagonally so that you have an isosceles right triangle,
// this function will find how many paths may be taken to get from one corner to the given index
function numberOfPathsInDiagonalGrid(x, y) {
if (x === 1 || y === 1) return 1;
if (x > y) return numberOfPathsInDiagonalGrid(x - 1, y);
else return (
numberOfPathsInDiagonalGrid(x - 1, y) +
numberOfPathsInDiagonalGrid(x, y - 1)
);
}
console.log(numberOfPathsInDiagonalGrid(3, 3));
/*
0,0 > 0,1 > 0,2 > 0,3 > 1,3 > 2,3 > 3,3
0,0 > 0,1 > 0,2 > 1,2 > 1,3 > 2,3 > 3,3
0,0 > 0,1 > 0,2 > 1,2 > 2,2 > 2,3 > 3,3
0,0 > 0,1 > 1,2 > 1,2 > 2,2 > 2,3 > 3,3
0,0 > 0,1 > 1,1 > 1,2 > 1,3 > 2,3 > 3,3
0,0 - 1,1 - 2,2 - 2,2 - 3,3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment