Peaceful chess queen armies: Difference between revisions

(→‎{{header|jq}}: simplify)
Line 4,913:
 
'''Works with gojq, the Go implementation of jq'''
 
In the following, positions on the chessboard are represented by {x,y} objects.
<syntaxhighlight lang="jq">
Line 4,926 ⟶ 4,925:
# and similarly for .whiteQueens.
# input: {blackQueens, whiteQueens}
# output: same asupdated input but with {ok} where .ok indicateson success, orotherwise failurenull.
def place($qqueens; $n):
def place($q):
if $q == 0 then .ok = true
else if $q == 0 then .placingBlackok = true
else .placingBlack = true
| label $out
| first(
| foreach range(0; $n) as $i (.;
foreach range (0; $n) as $ji (.;
foreach {x:$i,range(0; y:$j}n) as $posj (.;
| .placingBlack {x:$i, y:$j} as $placingBlackpos
| if any(| .blackQueens[],placingBlack .whiteQueens[];as $placingBlack
| if any((.x == $pos.x) and (.y ==blackQueens[], $pos.y)))whiteQueens[];
then ((.x #== failure$pos.x) and (.y == $pos.y)))
elif then .placingBlack # failure
then if any(elif .whiteQueens[]; isAttacking($pos) )placingBlack
then if thenany( .whiteQueens[]; isAttacking($pos) )
else .blackQueens +=then [$pos].
| else .placingBlackblackQueens += false[$pos]
end | .placingBlack = false
elif any( .blackQueens[]; isAttacking($pos) ) end
then elif any( .blackQueens[]; isAttacking($pos) )
else .whiteQueens +=then [$pos].
| place($q-1; $n)else .whiteQueens as+= [$placepos]
| if place($place.okq-1) thenas $place, break $out
else| .blackQueensif |=$place then $place # .[:-1]success
| else .whiteQueensblackQueens |= .[:-1]
| .placingBlackwhiteQueens |= true.[:-1]
| .placingBlack = true
end
end
| if $i == $n-1 and $j == $n-1 then .ok = false end );
| if $i == $n-1 and $j == $n-1 then select(.ok = false end ) )
| select(.ok !=) // null)
end;
(({blackQueens: [], whiteQueens: [] } | place($queens);
 
# Input {blackQueens, whiteQueens}
Line 4,989 ⟶ 4,991:
tasks[] as $t
| "\($t.queens) black and \($t.queens) white queens on a \($t.squares) x \($t.squares) board:",
((place($t.queens; $t.squares)
(({blackQueens: [], whiteQueens: [] }
| placeselect($t.queens; $t.squares)
| select(.ok)
| printBoard($t.squares))
// "No solution exists."),
2,442

edits