Skip to content

2019-4: Knight Moves

1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8
2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8
3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8
4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8
5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8
6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8
7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8
8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8

Consider a chess board as an 8Γ—8 matrix with square (1 1) in the upper left corner and square (8 8) in the lower right corner. For those not familiar with the game of chess, the knight, generally depicted as a horse (β™ž), can move 2 spaces right or left and then 1 space up or down, or 2 spaces up or down and then 1 space right or left. This means that a knight on square (5 4) can move to any of the indicated squares. Given a 2-element vector representing the current square for a knight, return a vector of 2-element vectors representing (in any order) all the squares that the knight can move to.

πŸ’‘ Hint: The outer product operator ∘. could be useful for generating the coordinates.

Examples:

      (your_function) 5 4   ⍝ ]Box on is used to display the result
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚3 3β”‚3 5β”‚4 2β”‚4 6β”‚6 2β”‚6 6β”‚7 3β”‚7 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      (your_function) 1 1
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”
β”‚2 3β”‚3 2β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”˜
your_function ←

Solutions

Video Thumbnail YouTube

Chat transcript Code on GitHub