Skip to content

Instantly share code, notes, and snippets.

@JasonKleban
Last active June 30, 2025 12:54
Show Gist options
  • Save JasonKleban/f3e8a74f87d7748813c6dd74d3459473 to your computer and use it in GitHub Desktop.
Save JasonKleban/f3e8a74f87d7748813c6dd74d3459473 to your computer and use it in GitHub Desktop.
spiralWalk
function* spiralWalk(dimensions: number): Generator<number[]> {
function* spiralWalk_(
dimensions: number,
digits: number
): Generator<number[]> {
if (dimensions === 0) {
yield [];
} else {
for (let digit = 0; digit < digits; digit++) {
for (let subwalk of spiralWalk_(dimensions - 1, digits)) {
yield [digit, ...subwalk];
}
}
}
}
for (let digits = 2; true; digits++) {
for (let candidate of spiralWalk_(dimensions, digits)) {
if (digits < 2 || candidate.some((d) => d === digits - 1)) {
yield candidate.map((digit) =>
digit % 2 === 0 ? -digit / 2 : Math.ceil(digit / 2)
);
}
}
}
}
for (let x of spiralWalk(4).take(100)) {
console.log(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment