step = (state, dir) => {
const next = state.map((p) => p.slice());
const [head] = next;
head[0] += dirs[dir][0];
head[1] += dirs[dir][1];
for (let i = 1; i < next.length; i++) {
const t = next[i];
const [hx, hy] = next[i - 1];
const [dx, dy] = [hx - t[0], hy - t[1]];
if (Math.abs(dx) === 2) {
t[0] += Math.sign(dx);
if (Math.abs(dy) >= 1) t[1] += Math.sign(dy);
} else if (Math.abs(dy) === 2) {
t[1] += Math.sign(dy);
if (Math.abs(dx) >= 1) t[0] += Math.sign(dx);
}
}
return next;
}