Last active
October 2, 2016 08:41
-
-
Save bremac/3771d4ef4eebcb28ef5f9e00d06faf6a to your computer and use it in GitHub Desktop.
Battleship validation for https://www.codewars.com/kata/battleship-field-validator/python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shift ← {(⍺×⍳↑⍴⍵)⌽⍵} | |
adjacent ← {∨/, 2∧⌿(1 shift ⍵),(¯1 shift ⍵)} | |
runends ← {(⍵=0)∧⍵≠(¯1⌽⍵)} | |
runlength ← {(↑lengths),2{⍵-⍺}/lengths←(runends ⍵)/+\⍵} | |
verticals ← {⍵∧(¯1⊖⍵)∨1⊖⍵} | |
ships ← {(runlength ,⍵≠v) , runlength ,⍉v←verticals ⍵} | |
allpresent ← {1 1 1 1 2 2 2 3 3 4 ≡ s[⍋s←ships ⍵]} | |
validate ← {(allpresent g) ∧ ~adjacent g←(0,0⍪⍵⍪0),0} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment