Peaceful chess queen armies: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, added online link)
m (syntax highlighting fixup automation)
Line 58: Line 58:
{{trans|D}}
{{trans|D}}


<lang 11l>T.enum Piece
<syntaxhighlight lang="11l">T.enum Piece
EMPTY
EMPTY
BLACK
BLACK
Line 138: Line 138:
printBoard(nm[0], blackQueens, whiteQueens)
printBoard(nm[0], blackQueens, whiteQueens)
E
E
print("No solution exists.\n")</lang>
print("No solution exists.\n")</syntaxhighlight>


{{out}}
{{out}}
Line 316: 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.)
(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.)


<lang ats>(********************************************************************)
<syntaxhighlight lang="ats">(********************************************************************)


#define ATS_DYNLOADFLAG 0
#define ATS_DYNLOADFLAG 0
Line 1,260: Line 1,260:
end
end


(********************************************************************)</lang>
(********************************************************************)</syntaxhighlight>


{{out}}
{{out}}
Line 1,306: Line 1,306:
=={{header|C}}==
=={{header|C}}==
{{trans|C#}}
{{trans|C#}}
<lang c>#include <math.h>
<syntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
Line 1,578: Line 1,578:


return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 1,745: Line 1,745:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 1,858: Line 1,858:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,025: Line 2,025:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>


Line 2,139: Line 2,139:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,306: Line 2,306:
=={{header|D}}==
=={{header|D}}==
{{trans|Go}}
{{trans|Go}}
<lang d>import std.array;
<syntaxhighlight lang="d">import std.array;
import std.math;
import std.math;
import std.stdio;
import std.stdio;
Line 2,417: Line 2,417:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,592: 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:
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:
<lang fortran>program peaceful_queens_elements_generator
<syntaxhighlight lang="fortran">program peaceful_queens_elements_generator
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: error_unit
use, intrinsic :: iso_fortran_env, only: error_unit
Line 3,407: Line 3,407:
end subroutine fill_queen_masks
end subroutine fill_queen_masks


end program peaceful_queens_elements_generator</lang>
end program peaceful_queens_elements_generator</syntaxhighlight>


Here is the second program, '''peaceful_queens.f90''':
Here is the second program, '''peaceful_queens.f90''':
<lang fortran>module peaceful_queens_support
<syntaxhighlight lang="fortran">module peaceful_queens_support
use, non_intrinsic :: peaceful_queens_elements
use, non_intrinsic :: peaceful_queens_elements


Line 3,722: Line 3,722:
& num_solutions, armies1, armies2)
& num_solutions, armies1, armies2)


end program peaceful_queens</lang>
end program peaceful_queens</syntaxhighlight>


Here is the driver script:
Here is the driver script:
<lang sh>#!/bin/sh
<syntaxhighlight lang="sh">#!/bin/sh
#
#
# Driver script for peaceful_queens in Fortran.
# Driver script for peaceful_queens in Fortran.
Line 3,769: Line 3,769:
${FC} ${FCFLAGS} -c peaceful_queens.f90 &&
${FC} ${FCFLAGS} -c peaceful_queens.f90 &&
${FC} ${FCFLAGS} -o peaceful_queens peaceful_queens_elements.o peaceful_queens.o &&
${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</lang>
if test x"${RUN_IT}" = xyes; then time ./peaceful_queens ${SHOW_EQUIVALENTS}; else :; fi</syntaxhighlight>


{{out}}
{{out}}
Line 4,004: 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.
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.
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 4,128: Line 4,128:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,300: Line 4,300:
=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>import java.util.ArrayList;
<syntaxhighlight lang="java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.List;
import java.util.List;
Line 4,447: Line 4,447:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,614: Line 4,614:
=={{header|Julia}}==
=={{header|Julia}}==
GUI version, uses the Gtk library. The place! function is condensed from the C# example.
GUI version, uses the Gtk library. The place! function is condensed from the C# example.
<lang julia>using Gtk
<syntaxhighlight lang="julia">using Gtk


struct Position
struct Position
Line 4,719: Line 4,719:


peacefulqueenapp()
peacefulqueenapp()
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|D}}
{{trans|D}}
<lang scala>import kotlin.math.abs
<syntaxhighlight lang="scala">import kotlin.math.abs


enum class Piece {
enum class Piece {
Line 4,825: Line 4,825:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,991: Line 4,991:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[ValidSpots, VisibleByQueen, SolveQueen, GetSolution]
<syntaxhighlight lang="mathematica">ClearAll[ValidSpots, VisibleByQueen, SolveQueen, GetSolution]
VisualizeState[state_] := Module[{q, cells},
VisualizeState[state_] := Module[{q, cells},
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
Line 5,034: Line 5,034:
]
]
GetSolution[8, 4, 3](* Solves placing 3 armies of each 4 queens on an 8*8 board*)
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*)</lang>
GetSolution[5, 4, 2](* Solves placing 2 armies of each 4 queens on an 5*5 board*)</syntaxhighlight>
{{out}}
{{out}}
<pre>[Graphical object]
<pre>[Graphical object]
Line 5,057: Line 5,057:
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.


<lang Nim>import sequtils, strformat
<syntaxhighlight lang="nim">import sequtils, strformat


type
type
Line 5,130: Line 5,130:
printBoard(n, blackQueens, whiteQueens)
printBoard(n, blackQueens, whiteQueens)
else:
else:
echo "No solution exists.\n"</lang>
echo "No solution exists.\n"</syntaxhighlight>


{{out}}
{{out}}
Line 5,299: Line 5,299:
=={{header|Perl}}==
=={{header|Perl}}==
===Terse===
===Terse===
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 5,317: Line 5,317:
(my $have = tr/WB//) < $m * 2 or exit !print "Solution to $m $n\n\n$_";
(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
place( s/-\G/ qw(W B)[$have % 2] /er ) while /-/g; # place next queen
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Solution to 4 5
<pre>Solution to 4 5
Line 5,328: Line 5,328:
===Verbose===
===Verbose===
A refactored version of the same code, with fancier output.
A refactored version of the same code, with fancier output.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 5,379: Line 5,379:
say $solution
say $solution
? sprintf "Solution to $m $n\n\n%s", map { s/(.)/$1 /gm; s/B /♛/gm; s/W /♕/gmr } $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";</lang>
: "No solution to $m $n";</syntaxhighlight>
{{out}}
{{out}}
<pre>Solution to 4 5
<pre>Solution to 4 5
Line 5,393: Line 5,393:
{{trans|Python}}
{{trans|Python}}
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Queen_Armies.exw
-- demo\rosetta\Queen_Armies.exw
Line 5,531: Line 5,531:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<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>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
with as_html = false
with as_html = false
Line 5,649: Line 5,649:
=={{header|Python}}==
=={{header|Python}}==
===Python: Textual output===
===Python: Textual output===
<lang python>from itertools import combinations, product, count
<syntaxhighlight lang="python">from itertools import combinations, product, count
from functools import lru_cache, reduce
from functools import lru_cache, reduce


Line 5,722: Line 5,722:
m, n = 5, 7
m, n = 5, 7
ans = place(m, n)
ans = place(m, n)
pboard(ans, n)</lang>
pboard(ans, n)</syntaxhighlight>


{{out}}
{{out}}
Line 5,822: Line 5,822:
===Python: HTML output===
===Python: HTML output===
Uses the solver function <code>place</code> from the above textual output case.
Uses the solver function <code>place</code> from the above textual output case.
<lang python>from peaceful_queen_armies_simpler import place
<syntaxhighlight lang="python">from peaceful_queen_armies_simpler import place
from itertools import product, count
from itertools import product, count


Line 5,870: Line 5,870:
html += hboard(ans, n)
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</lang>
f.write(html)</syntaxhighlight>


{{out}}
{{out}}
Line 6,464: Line 6,464:
(formerly Perl 6)
(formerly Perl 6)
{{trans|Perl}}
{{trans|Perl}}
<lang perl6># recursively place the next queen
<syntaxhighlight lang="raku" line># recursively place the next queen
sub place ($board, $n, $m, $empty-square) {
sub place ($board, $n, $m, $empty-square) {
my $cnt;
my $cnt;
Line 6,511: Line 6,511:
say $solution
say $solution
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
!! "No solution to $m $n";</lang>
!! "No solution to $m $n";</syntaxhighlight>
{{out}}
{{out}}
<pre>W • ◦ • W
<pre>W • ◦ • W
Line 6,521: Line 6,521:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Java}}
{{trans|Java}}
<lang ruby>class Position
<syntaxhighlight lang="ruby">class Position
attr_reader :x, :y
attr_reader :x, :y


Line 6,640: Line 6,640:
print "No solution exists.\n\n"
print "No solution exists.\n\n"
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 6,810: Line 6,810:
{{libheader|srfi-132}}
{{libheader|srfi-132}}


<lang scheme>;;;
<syntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme (using also SRFI-132).
;;; Scheme (using also SRFI-132).
Line 7,039: Line 7,039:
(newline)
(newline)
(newline)
(newline)
(loop (+ next-solution-number 1))))))</lang>
(loop (+ next-solution-number 1))))))</syntaxhighlight>


{{out}}
{{out}}
Line 7,280: Line 7,280:
===All non-equivalent solutions===
===All non-equivalent solutions===
{{works with|CHICKEN|5.3.0}}
{{works with|CHICKEN|5.3.0}}
<lang scheme>;;;
<syntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme. This implementation returns only one of each equivalent
;;; Scheme. This implementation returns only one of each equivalent
Line 7,621: Line 7,621:
(newline)
(newline)
(newline)
(newline)
(loop (+ next-solution-number 1))))))</lang>
(loop (+ next-solution-number 1))))))</syntaxhighlight>


{{out}}
{{out}}
Line 7,669: Line 7,669:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang swift>enum Piece {
<syntaxhighlight lang="swift">enum Piece {
case empty, black, white
case empty, black, white
}
}
Line 7,777: Line 7,777:
print("No solution")
print("No solution")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 7,942: Line 7,942:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
<lang ecmascript>import "/dynamic" for Enum, Tuple
<syntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Tuple


var Piece = Enum.create("Piece", ["empty", "black", "white"])
var Piece = Enum.create("Piece", ["empty", "black", "white"])
Line 8,034: Line 8,034:
System.print("No solution exists.\n")
System.print("No solution exists.\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 8,042: Line 8,042:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn isAttacked(q, x,y) // ( (r,c), x,y ) : is queen at r,c attacked by q@(x,y)?
<syntaxhighlight 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) }
{ 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)..)
fcn isSafe(r,c,qs) // queen safe at (r,c)?, qs=( (r,c),(r,c)..)
Line 8,074: Line 8,074:
z.text.pump(Void,T(Void.Read,N-1),"println");
z.text.pump(Void,T(Void.Read,N-1),"println");
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>peacefulQueens();
<syntaxhighlight lang="zkl">peacefulQueens();
foreach n in ([4..10]){ peacefulQueens(n,n) }</lang>
foreach n in ([4..10]){ peacefulQueens(n,n) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>