On Sat, Apr 18, 2020 at 09:54:13PM +0200, Otto Diesenbacher-Reinmüller wrote:
4: Knight Moves
(...)
*. Given a 2-element vector representing thecurrent square for a
knight, return a vector of 2-element vectors representing (in any
order) all the squares that the knight can moveto. Hint: The outer
product operator ∘. could be useful for generating the coordinates.
PM ← (∼a∊⊂⍬)/a←(,b ∘.{((|⍺)≠|⍵)/⍺,⍵} b←(¯2 2 ¯1 1)) ⍝ calculate possible moves
from a square
I would shorten it a bit into something more like:
PM← (≠/¨|PM)/ PM←, ∘.,⍨ ¯2 2 ¯1 1
z ← (∼{⍵[1]∨⍵[2]}¨{((⍵<1)∨⍵>8)}z)/z ⍝ remove off-board moves
Selection of those locations that lie within the board could be more
naturally written as (z∊⍳8 8)/z, but I understand you tried to avoid
generating the whole board here, unlike in the second attempt.
But more importantly, there is a simple relation between the current
location and valid moves--a relation elsewhere known as the equation
of a circle.
-k
My solution would be:
{(5=+/¨(H-⊂⍵)⋆2)/H←,⍳8 8 ;H}