Count of possible hexagonal walks | Javascript implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver program to test above function ; Preprocessing all possible ways
let depth = 14 ; let ways = new Array ( 16 ) ; for ( let i = 0 ; i < 16 ; i ++ ) { ways [ i ] = new Array ( 16 ) ; for ( let j = 0 ; j < 16 ; j ++ ) { ways [ i ] [ j ] = new Array ( 16 ) ; for ( let k = 0 ; k < 16 ; k ++ ) { ways [ i ] [ j ] [ k ] = 0 ; } } } let stepNum ; function preprocess ( list ) { ways [ 0 ] [ 8 ] [ 8 ] = 1 ; for ( let N = 1 ; N <= 14 ; N ++ ) { for ( let i = 1 ; i < depth ; i ++ ) { for ( let j = 1 ; j < depth ; j ++ ) { ways [ N ] [ i ] [ j ] = ways [ N - 1 ] [ i ] [ j + 1 ] + ways [ N - 1 ] [ i ] [ j - 1 ] + ways [ N - 1 ] [ i + 1 ] [ j ] + ways [ N - 1 ] [ i - 1 ] [ j ] + ways [ N - 1 ] [ i + 1 ] [ j - 1 ] + ways [ N - 1 ] [ i - 1 ] [ j + 1 ] ; } } list [ N ] = ways [ N ] [ 8 ] [ 8 ] ; } } let list = new Array ( 15 ) ; list . fill ( 0 ) ; preprocess ( list ) ; let steps = 4 ; document . write ( " " + " " + list [ steps ] ) ;