Peaceful chess queen armies: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, added online link)
m (syntax highlighting fixup automation)
Line 58:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">T.enum Piece
EMPTY
BLACK
Line 138:
printBoard(nm[0], blackQueens, whiteQueens)
E
print("No solution exists.\n")</langsyntaxhighlight>
 
{{out}}
Line 316:
(Commentary by the author: this program suffers similarly of slowness, in eliminating rotational equivalents, as does its Scheme ancestor. Some reasons: it uses backtracking and that is slow; it uses essentially the same inefficient storage format for solutions [one could for instance use integers], and it does not precompute rotational equivalents. However, it does satisfy the task requirements, and might be regarded as a good start. And it can solve the m=5, n=6 case in practical time on a fast machine. m=7, n=7 is a more annoying case.)
 
<langsyntaxhighlight lang="ats">(********************************************************************)
 
#define ATS_DYNLOADFLAG 0
Line 1,260:
end
 
(********************************************************************)</langsyntaxhighlight>
 
{{out}}
Line 1,306:
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Line 1,578:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 1,745:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 1,858:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,025:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 2,139:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,306:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.array;
import std.math;
import std.stdio;
Line 2,417:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,592:
 
Here is the first program, '''peaceful_queens_elements_generator.f90''', which generates code (specialized for given m and n) to deal with the representations of the armies as integers:
<langsyntaxhighlight lang="fortran">program peaceful_queens_elements_generator
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: error_unit
Line 3,407:
end subroutine fill_queen_masks
 
end program peaceful_queens_elements_generator</langsyntaxhighlight>
 
Here is the second program, '''peaceful_queens.f90''':
<langsyntaxhighlight lang="fortran">module peaceful_queens_support
use, non_intrinsic :: peaceful_queens_elements
 
Line 3,722:
& num_solutions, armies1, armies2)
 
end program peaceful_queens</langsyntaxhighlight>
 
Here is the driver script:
<langsyntaxhighlight lang="sh">#!/bin/sh
#
# Driver script for peaceful_queens in Fortran.
Line 3,769:
${FC} ${FCFLAGS} -c peaceful_queens.f90 &&
${FC} ${FCFLAGS} -o peaceful_queens peaceful_queens_elements.o peaceful_queens.o &&
if test x"${RUN_IT}" = xyes; then time ./peaceful_queens ${SHOW_EQUIVALENTS}; else :; fi</langsyntaxhighlight>
 
{{out}}
Line 4,004:
 
Textual rather than HTML output. Whilst the unicode symbols for the black and white queens are recognized by the Ubuntu 16.04 terminal, I found it hard to visually distinguish between them so I've used 'B' and 'W' instead.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 4,128:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,300:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Line 4,447:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,614:
=={{header|Julia}}==
GUI version, uses the Gtk library. The place! function is condensed from the C# example.
<langsyntaxhighlight lang="julia">using Gtk
 
struct Position
Line 4,719:
 
peacefulqueenapp()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import kotlin.math.abs
 
enum class Piece {
Line 4,825:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,991:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ValidSpots, VisibleByQueen, SolveQueen, GetSolution]
VisualizeState[state_] := Module[{q, cells},
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
Line 5,034:
]
GetSolution[8, 4, 3](* Solves placing 3 armies of each 4 queens on an 8*8 board*)
GetSolution[5, 4, 2](* Solves placing 2 armies of each 4 queens on an 5*5 board*)</langsyntaxhighlight>
{{out}}
<pre>[Graphical object]
Line 5,057:
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.
 
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat
 
type
Line 5,130:
printBoard(n, blackQueens, whiteQueens)
else:
echo "No solution exists.\n"</langsyntaxhighlight>
 
{{out}}
Line 5,299:
=={{header|Perl}}==
===Terse===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 5,317:
(my $have = tr/WB//) < $m * 2 or exit !print "Solution to $m $n\n\n$_";
place( s/-\G/ qw(W B)[$have % 2] /er ) while /-/g; # place next queen
}</langsyntaxhighlight>
{{out}}
<pre>Solution to 4 5
Line 5,328:
===Verbose===
A refactored version of the same code, with fancier output.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 5,379:
say $solution
? sprintf "Solution to $m $n\n\n%s", map { s/(.)/$1 /gm; s/B /♛/gm; s/W /♕/gmr } $solution
: "No solution to $m $n";</langsyntaxhighlight>
{{out}}
<pre>Solution to 4 5
Line 5,393:
{{trans|Python}}
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Queen_Armies.exw
Line 5,531:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
with as_html = false
Line 5,649:
=={{header|Python}}==
===Python: Textual output===
<langsyntaxhighlight lang="python">from itertools import combinations, product, count
from functools import lru_cache, reduce
 
Line 5,722:
m, n = 5, 7
ans = place(m, n)
pboard(ans, n)</langsyntaxhighlight>
 
{{out}}
Line 5,822:
===Python: HTML output===
Uses the solver function <code>place</code> from the above textual output case.
<langsyntaxhighlight lang="python">from peaceful_queen_armies_simpler import place
from itertools import product, count
 
Line 5,870:
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</langsyntaxhighlight>
 
{{out}}
Line 6,464:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line># recursively place the next queen
sub place ($board, $n, $m, $empty-square) {
my $cnt;
Line 6,511:
say $solution
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
!! "No solution to $m $n";</langsyntaxhighlight>
{{out}}
<pre>W • ◦ • W
Line 6,521:
=={{header|Ruby}}==
{{trans|Java}}
<langsyntaxhighlight lang="ruby">class Position
attr_reader :x, :y
 
Line 6,640:
print "No solution exists.\n\n"
end
end</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 6,810:
{{libheader|srfi-132}}
 
<langsyntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme (using also SRFI-132).
Line 7,039:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</langsyntaxhighlight>
 
{{out}}
Line 7,280:
===All non-equivalent solutions===
{{works with|CHICKEN|5.3.0}}
<langsyntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme. This implementation returns only one of each equivalent
Line 7,621:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</langsyntaxhighlight>
 
{{out}}
Line 7,669:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">enum Piece {
case empty, black, white
}
Line 7,777:
print("No solution")
}
}</langsyntaxhighlight>
 
{{out}}
Line 7,942:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Tuple
 
var Piece = Enum.create("Piece", ["empty", "black", "white"])
Line 8,034:
System.print("No solution exists.\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 8,042:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn isAttacked(q, x,y) // ( (r,c), x,y ) : is queen at r,c attacked by q@(x,y)?
{ r,c:=q; (r==x or c==y or r+c==x+y or r-c==x-y) }
fcn isSafe(r,c,qs) // queen safe at (r,c)?, qs=( (r,c),(r,c)..)
Line 8,074:
z.text.pump(Void,T(Void.Read,N-1),"println");
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">peacefulQueens();
foreach n in ([4..10]){ peacefulQueens(n,n) }</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits