aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorfrasiyav <notarealemail@notarealemailprovider.com>2020-10-24 10:10:20 -0600
committerfrasiyav <notarealemail@notarealemailprovider.com>2020-10-24 10:10:20 -0600
commit3a026e10f65e58ef4385d27f22c2b0a797737b5b (patch)
tree8a5e2d259997ad95ed0611b5f4f4a1c4a40a0180 /examples
parenta9094993c5dc359f021e22375936d990069012d8 (diff)
Added N Queens and Knight's Tour
Diffstat (limited to 'examples')
-rw-r--r--examples/fifty.bqn25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/fifty.bqn b/examples/fifty.bqn
index 2a9cf317..c3c76a76 100644
--- a/examples/fifty.bqn
+++ b/examples/fifty.bqn
@@ -146,6 +146,31 @@ IndexFromCombination‿CombinationFromIndex ← {
SymmetricArray ← ⍉⊸≡ ∧ 1⊸⍉⊸≡
! SymmetricArray +⌜´ 3⥊<2‿3‿7‿11
+# 25
+NQueens ← {
+ Arr ← {>(⊑⊸⊏⟜𝕩∾1⊸⊑)¨/¬(↕𝕨)∊⎉1𝕩⥊∘+⎉1‿2(-⟜↕¯1⊑≢𝕩)×⌜¯1‿0‿1}
+ 𝕩 Arr⍟(𝕩-1) ≍˘↕𝕩
+}
+QueenCheck ← {
+ ! 1=≠≢𝕩
+ ! (↕≠𝕩)≡∧𝕩
+ ! ((2⥊≠𝕩)⥊1)≡(=⌜˜↕≠𝕩)≥(|-⌜˜𝕩)=|-⌜˜↕≠𝕩
+}
+QueenCheck˘ NQueens 8
+
+#26
+KnightsTour ← {
+ Kmoves ← {
+ t ← (⥊↕𝕩‿𝕩)+⌜<˘8‿2⥊2‿1‿2‿¯1‿1‿2‿1‿¯2‿¯1‿2‿¯1‿¯2‿¯2‿1‿¯2‿¯1
+ (∧´⎉1(>t)∊↕𝕩) /¨○<˘ 𝕩{+⟜(𝕨⊸×)´⌽𝕩}¨t
+ }
+ m ← >↑˜¨⟜(⌈´≠¨) Kmoves 𝕩
+ b ← (𝕩×𝕩)⥊1
+ F ← {b↩0⌾((¯1⊑𝕩)⊸⊑)b ⋄ 𝕩∾(⊑⍋+´˘(j⊏m)⊏b)⊑j←⊏⟜b⊸/(¯1⊑𝕩)⊏m}
+ 𝕩‿𝕩⥊⍋F⍟(¯1+𝕩⋆2) 1⥊0
+}
+! (KnightsTour 6) ≡ 6‿6⥊0‿9‿20‿35‿6‿11‿21‿32‿7‿10‿19‿26‿8‿1‿34‿25‿12‿5‿33‿22‿31‿16‿27‿18‿2‿15‿24‿29‿4‿13‿23‿30‿3‿14‿17‿28
+
# 34
_pow ← {𝔽´𝔽˜⍟(/2|⌊∘÷⟜2⍟(↕·⌈2⋆⁼⊢)𝕩)𝕨}
! 847288609443 ≡ 3 ×_pow 25