Logic games in APL: Electric charges

Rules

Source code

mat ← 4 4⍴3 9 ¯1 9 9 9 9 9 0 9 9 0 ¯2 9 0 9

pmat ← {{,[⍳2]↑(⊂⊂⎕IO,1+⍵)⌷¨⍒¨↓∘.=⍨⍳1+1↓⍴⍵}⍣⍵⍉⍪⍬}

f ← {
  y←⍺ ⋄ x←⍵
  n ← ≢near ← ({1↓↓⍵⌿⍨∧/⍵∊⍳≢mat}y+⍤1⊢↑,∘.,⍨0 1 ¯1)~⍸0≠x+9≠mat
  s ← y⌷mat-{+/,⍵}⌺3 3⊢x
  ((0=n)∧0≠s)∨(≠/2|n s)∨n<|s: ⍬
  sum ← {1 ¯1/⍨⍵⊃⍨⍸s=-/¨⍵}(⌽,¨⊢)0,⍳n
  {⍵@near⊢x}¨↓∪sum[pmat n]
}

solver ← {⊃⊃{⊃,/⍺∘f¨⍵}/(⍸9≠⍵),(⊂∘⊂0⍴⍨⍴)⍵⊣mat←⍵}