apl: n-queens via permute + diagonal filter, q(8)=92 (+10 tests, 306/306)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m5s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m5s
This commit is contained in:
@@ -865,6 +865,48 @@
|
||||
(ones (apl-add (apl-mul cs (apl-scalar 0)) (apl-scalar 1))))
|
||||
(apl-mandelbrot-step cs zero zero ones max-iter))))
|
||||
|
||||
(define
|
||||
apl-insert-everywhere
|
||||
(fn
|
||||
(x lst)
|
||||
(map
|
||||
(fn (i) (append (take lst i) (cons x (drop lst i))))
|
||||
(range 0 (+ (len lst) 1)))))
|
||||
|
||||
(define
|
||||
apl-permutations
|
||||
(fn
|
||||
(n)
|
||||
(if
|
||||
(<= n 1)
|
||||
(list (list 1))
|
||||
(let
|
||||
((sub (apl-permutations (- n 1))))
|
||||
(reduce
|
||||
(fn (acc p) (append acc (apl-insert-everywhere n p)))
|
||||
(list)
|
||||
sub)))))
|
||||
|
||||
(define
|
||||
apl-queens-no-conflict?
|
||||
(fn
|
||||
(perm i j n)
|
||||
(cond
|
||||
((>= i n) true)
|
||||
((>= j n) (apl-queens-no-conflict? perm (+ i 1) (+ i 2) n))
|
||||
((= (abs (- i j)) (abs (- (nth perm i) (nth perm j)))) false)
|
||||
(else (apl-queens-no-conflict? perm i (+ j 1) n)))))
|
||||
|
||||
(define
|
||||
apl-queens-valid?
|
||||
(fn (perm) (apl-queens-no-conflict? perm 0 1 (len perm))))
|
||||
|
||||
(define
|
||||
apl-queens
|
||||
(fn
|
||||
(n)
|
||||
(apl-scalar (len (filter apl-queens-valid? (apl-permutations n))))))
|
||||
|
||||
(define
|
||||
apl-reduce
|
||||
(fn
|
||||
|
||||
Reference in New Issue
Block a user