Skip to content

Instantly share code, notes, and snippets.

@jihchi
Last active November 4, 2025 14:39
Show Gist options
  • Select an option

  • Save jihchi/9cac764592a0d59d9dde89a1bf88f0a2 to your computer and use it in GitHub Desktop.

Select an option

Save jihchi/9cac764592a0d59d9dde89a1bf88f0a2 to your computer and use it in GitHub Desktop.

Prerequisite: Deno v2

deno run https://gist.github.com/jihchi/9cac764592a0d59d9dde89a1bf88f0a2/raw/solve.ts
function knightMoves(coord: [number, number]): [number, number][] {
const up = [-1, 0];
const down = [1, 0];
const left = [0, -1];
const right = [0, 1];
const moves = [
[up, up, left],
[up, up, right],
[right, right, up],
[right, right, down],
[down, down, left],
[down, down, right],
[left, left, up],
[left, left, down],
];
return moves
.map((moves) =>
moves.reduce<[number, number]>(
(acc, move) => [acc[0] + move[0], acc[1] + move[1]],
coord,
)
)
.filter((coord) => coord[0] >= 0 && coord[1] < 8);
}
console.log(knightMoves([4, 4]));
// output:
// [
// [2, 3],
// [2, 5],
// [3, 2],
// [3, 6],
// [5, 2],
// [5, 6],
// [6, 3],
// [6, 5],
// ]
console.log(knightMoves([0, 0]));
// output:
// [
// [1, 2],
// [2, 1],
// ]
console.log(knightMoves([1, 2]));
// output:
// [
// [0, 0],
// [0, 4],
// [2, 0],
// [2, 4],
// [3, 1],
// [3, 3],
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment