Searching for matrix in matrix
Posted: Mon May 14, 2018 3:24 pm
I'm getting to the point in learning APL that I know what most of the functions do, but not how to solve problems with them, so I went looking for some. I found some kata to solve to practice APL, after first solving in a known language. I solved this one using javascript, with no mutation, and only pure functions. Upon reflection of the solution, I recognized a much simpler solution using matrices.
https://www.codewars.com/kata/battleshi ... -validator
Since no ship placements may touch, there are only 3 "interesting" configurations.
Corner = n⍴1, n+2↑0
Wall = 0, n⍴1, n+3↑0, or in APL, since zeros propagate left, 2×(n+2) ↑ (0, n⍴1)
2x because 2 rows, n + 2 because there has to be padding on all sides.
Middle = 3×(n+2) ↑ (n+3 ↑ 0, n⍴1)
https://ngn.github.io/apl/web/
All that being said. I'm having trouble actually figuring out how to do the search. I've been through the tutor, but none of my guesses actually amount to anything. I was thinking of searching the matrix for every ⍴⍵ in board, but that leads to rank errors, because I naively assumed the search would bound itself to the board.
At first I thought this, or some variation would work out of the box:
For example ⍴corner 4 is 2 5
I forget all the permutations of that that I've tried, but I've been at it since Friday evening, and I'm stuck.
If the board is
how can I find some ⍉,⊖,⌽ of a 2xn or 3xn M in the board?
https://www.codewars.com/kata/battleshi ... -validator
Since no ship placements may touch, there are only 3 "interesting" configurations.
Corner = n⍴1, n+2↑0
1 1 1 1 0
0 0 0 0 0
Wall = 0, n⍴1, n+3↑0, or in APL, since zeros propagate left, 2×(n+2) ↑ (0, n⍴1)
2x because 2 rows, n + 2 because there has to be padding on all sides.
0 1 1 1 1 0
0 0 0 0 0 0
Middle = 3×(n+2) ↑ (n+3 ↑ 0, n⍴1)
0 0 0 0 0 0
0 1 1 1 1 0
0 0 0 0 0 0
board ← (10 10 ⍴ ⍳100) ∊ 0 10 20 30 2 3 4 6 7 8 22 23 24 26 27 29 39 59 69 80 82 60 62
corner ← {2 (⍵+1) ⍴ (2×(2+⍵)) ↑ ⍵ ⍴ 1}
wall ← {2 (⍵+2) ⍴ (2×(2+⍵)) ↑ ∊ 0 (⍵ ⍴ 1)}
middle ← {3 (⍵+2) ⍴ (3×(2+⍵)) ↑ ∊ ((⍵+3) ↑ 0) (⍵ ⍴ 1)}
board
https://ngn.github.io/apl/web/
All that being said. I'm having trouble actually figuring out how to do the search. I've been through the tutor, but none of my guesses actually amount to anything. I was thinking of searching the matrix for every ⍴⍵ in board, but that leads to rank errors, because I naively assumed the search would bound itself to the board.
At first I thought this, or some variation would work out of the box:
(corner 4) ∊ bord
For example ⍴corner 4 is 2 5
2 5⍴¨board
I forget all the permutations of that that I've tried, but I've been at it since Friday evening, and I'm stuck.
If the board is
1 0 0...
1 0 0...
1 0 0...
1 0 0...
0 0 0...
how can I find some ⍉,⊖,⌽ of a 2xn or 3xn M in the board?
1 1 1 1 0
0 0 0 0 0