Last active
June 30, 2025 12:54
-
-
Save JasonKleban/f3e8a74f87d7748813c6dd74d3459473 to your computer and use it in GitHub Desktop.
spiralWalk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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