calculate direction at each iteration
function directionSequence(i, dim) { // :: Int -> Int -> Int
    if (i == 0) return 0
    if (i % 2 == 0) return bitwise.trailingSetBits(i - 1) % dim
    return bitwise.trailingSetBits(i) % dim
}