bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

## exercise 2019/04

 From: Otto Diesenbacher-Reinmüller Subject: exercise 2019/04 Date: Sat, 18 Apr 2020 21:54:13 +0200 User-agent: mu4e 1.0; emacs 28.0.50

4: Knight Moves

Consider a chess board as an 8×8 matrix withsquare (1 1) in the upper
left corner andsquare (8 8) in the lower right corner. Forthose not
familiar with the game a chess, the knight, generally depicted as a
horse, canmove 2 spaces right or left and then 1 space upor down, or 2
spaces up or down and then 1space 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 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.

┌→──────────────────────────────────────────────┐
↓┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐│
││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│ │5x4│ │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││
│└───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘│
└∊──────────────────────────────────────────────┘

⍝ first try: add to asked field all possible moves and remove off-board moves.
∇z←moves field; PM; a; b
PM ← (∼a∊⊂⍬)/a←(,b ∘.{((|⍺)≠|⍵)/⍺,⍵} b←(¯2 2 ¯1 1)) ⍝ calculate possible
moves from a square
z ← PM+⊂field                  ⍝ get all moves, including off-board
z ← (∼{⍵[1]∨⍵[2]}¨{((⍵<1)∨⍵>8)}z)/z       ⍝ remove off-board moves
∇

⍝ second try: calculate from each field possible origns, afterwards
⍝ remove all, not being the asked origin, so we calculate more, but
⍝ don't have to ask for removing off-board moves.
∇z←moves2 field;PM
PM ← (∼a∊⊂⍬)/a←(,b ∘.{((|⍺)≠|⍵)/⍺,⍵} b←(¯2 2 ¯1 1)) ⍝ calculate possible
moves from a square
z←(,({ ∨/(PM + ⊂⍵) ∊ ⊂field }¨ ⍳ 8 8))/,⍳ 8 8
∇

Honestly - this solution feels clumsy, or in the words of Kacper -
C-style. ;)

But I am too dumb :( - I can't see the array-style solution, thinking
about it now for 2 days. Could you give me just a hint or direction,
no full solution?

br & many thanks - Otto

--
Dipl. Ing. (FH) Otto Diesenbacher-Reinmüller
diesenbacher.net
Salzburg, Österreich

reply via email to

 [Prev in Thread] Current Thread [Next in Thread]