Given the 0-indexed position \$n\$ of the character, the direction \$0\le d\le 2\$ is given by:
$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$
Try it online! (with extra whitespace removed)
How?
Given the position \$n\$ of the character, the direction \$0\le d\le 2\$ can be computed with:
$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$
The actual JS implementation is:
~(2 * n ** 0.5 - 1 / n) % 4 % 3
which evaluates to \$0\$, \$-1\$ or \$-2\$.