Dominoes: Difference between revisions

Content added Content deleted
m (Promote to full task)
m (syntax highlighting fixup automation)
Line 21: Line 21:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Dominoes: Nigel Galloway. November 17th., 2021.
// Dominoes: Nigel Galloway. November 17th., 2021.
let cP (n:seq<uint64 list * uint64>) g=seq{for y,n in n do for g in g do let l=n^^^g in if n+g=l then yield (g::y,l)}
let cP (n:seq<uint64 list * uint64>) g=seq{for y,n in n do for g in g do let l=n^^^g in if n+g=l then yield (g::y,l)}
Line 36: Line 36:
2;1;4;3;3;4;6;6;
2;1;4;3;3;4;6;6;
6;4;5;1;5;4;1;4|]
6;4;5;1;5;4;1;4|]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 53: Line 53:
6 4+5 1+5 4 1+4
6 4+5 1+5 4 1+4
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
solve [|5;6;2;0;0;4;1;4;
solve [|5;6;2;0;0;4;1;4;
3;6;1;3;0;4;2;2;
3;6;1;3;0;4;2;2;
Line 61: Line 61:
4;4;1;3;6;6;0;2;
4;4;1;3;6;6;0;2;
1;2;6;2;6;5;0;4|]
1;2;6;2;6;5;0;4|]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 80: Line 80:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>const tableau = [
<syntaxhighlight lang="julia">const tableau = [
0 5 1 3 2 2 3 1;
0 5 1 3 2 2 3 1;
0 5 5 0 5 2 4 6;
0 5 5 0 5 2 4 6;
Line 166: Line 166:
printlayout(first(lays))
printlayout(first(lays))
println(length(lays), " layouts found.")
println(length(lays), " layouts found.")
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
0+5 1+3 2 2+3 1
0+5 1+3 2 2+3 1
Line 202: Line 202:
</pre>
</pre>
===Extra credit task ===
===Extra credit task ===
<lang julia>""" From https://en.wikipedia.org/wiki/Domino_tiling#Counting_tilings_of_regions
<syntaxhighlight lang="julia">""" From https://en.wikipedia.org/wiki/Domino_tiling#Counting_tilings_of_regions
The number of ways to cover an m X n rectangle with m * n / 2 dominoes, calculated
The number of ways to cover an m X n rectangle with m * n / 2 dominoes, calculated
independently by Temperley & Fisher (1961) and Kasteleyn (1961), is given by
independently by Temperley & Fisher (1961) and Kasteleyn (1961), is given by
Line 228: Line 228:
println("Possible flip configurations: $flips")
println("Possible flip configurations: $flips")
println("Possible permuted arrangements with flips: ", flips * arrang * perms)
println("Possible permuted arrangements with flips: ", flips * arrang * perms)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Arrangements ignoring values: 1292697
Arrangements ignoring values: 1292697
Line 238: Line 238:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[VisualizeState]
<syntaxhighlight lang="mathematica">ClearAll[VisualizeState]
VisualizeState[sol_List, tab_List] := Module[{rects},
VisualizeState[sol_List, tab_List] := Module[{rects},
rects = Apply[Rectangle[#1 - {1, 1} 0.5, #2 + {1, 1} 0.5] &, sol[[All, 2]], {1}];
rects = Apply[Rectangle[#1 - {1, 1} 0.5, #2 + {1, 1} 0.5] &, sol[[All, 2]], {1}];
Line 289: Line 289:
sols = FindSolutions[tab];
sols = FindSolutions[tab];
Length[sols]
Length[sols]
VisualizeState[sols[[1]], tab]</lang>
VisualizeState[sols[[1]], tab]</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 297: Line 297:


===Extra credit task ===
===Extra credit task ===
<lang Mathematica>ClearAll[DominoTilingCount]
<syntaxhighlight lang="mathematica">ClearAll[DominoTilingCount]
DominoTilingCount[m_, n_] := Module[{},
DominoTilingCount[m_, n_] := Module[{},
Round[Product[
Round[Product[
Line 312: Line 312:
Print["Permuted arrangements ignoring flipping dominos: ", arrangements*permutations]
Print["Permuted arrangements ignoring flipping dominos: ", arrangements*permutations]
Print["Possible flip configurations: ", flips]
Print["Possible flip configurations: ", flips]
Print["Possible permuted arrangements with flips: ", flips*arrangements*permutations]</lang>
Print["Possible permuted arrangements with flips: ", flips*arrangements*permutations]</syntaxhighlight>
{{out}}
{{out}}
<pre>Arrangements ignoring values: 1292697
<pre>Arrangements ignoring values: 1292697
Line 321: Line 321:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Dominoes
use strict; # https://rosettacode.org/wiki/Dominoes
Line 385: Line 385:
find( $x + 1, $y, $new );
find( $x + 1, $y, $new );
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 423: Line 423:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 579: Line 579:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unpack</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"05132231 05505246 43036620 06235126 11300245 21433466 64515414"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unpack</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"05132231 05505246 43036620 06235126 11300245 21433466 64515414"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rand_grid</span><span style="color: #0000FF;">())</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rand_grid</span><span style="color: #0000FF;">())</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 638: Line 638:
===Extra credit===
===Extra credit===
Pretty dumb brute force approach, dreadfully slow.
Pretty dumb brute force approach, dreadfully slow.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- too slow</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- too slow</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">IGNORE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">CONSIDER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NOSYM</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">IGNORE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">CONSIDER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NOSYM</span>
Line 688: Line 688:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arrangements ignoring symmetry: %g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">(</span><span style="color: #000000;">NOSYM</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arrangements ignoring symmetry: %g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">(</span><span style="color: #000000;">NOSYM</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 697: Line 697:
=== Much faster ===
=== Much faster ===
{{trans|Wren}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 727: Line 727:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Possible permuted arrangements with flips: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Possible permuted arrangements with flips: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">perms</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Took %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">start</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Took %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">start</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 741: Line 741:
{{trans|Julia}}
{{trans|Julia}}
===Basic task===
===Basic task===
<lang ecmascript>var tableau = [
<syntaxhighlight lang="ecmascript">var tableau = [
[0, 5, 1, 3, 2, 2, 3, 1],
[0, 5, 1, 3, 2, 2, 3, 1],
[0, 5, 5, 0, 5, 2, 4, 6],
[0, 5, 5, 0, 5, 2, 4, 6],
Line 877: Line 877:
System.print("%(lays.count) layout%(pl) found%(fo).")
System.print("%(lays.count) layout%(pl) found%(fo).")
System.print("Took %(System.clock - start) seconds.\n")
System.print("Took %(System.clock - start) seconds.\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 918: Line 918:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./big" for BigInt
<syntaxhighlight lang="ecmascript">import "./big" for BigInt
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 943: Line 943:
Fmt.print("Possible flip configurations: $,i", flips)
Fmt.print("Possible flip configurations: $,i", flips)
Fmt.print("Possible permuted arrangements with flips: $,i", perms * flips * arrang)
Fmt.print("Possible permuted arrangements with flips: $,i", perms * flips * arrang)
System.print("\nTook %(System.clock - start) seconds.")</lang>
System.print("\nTook %(System.clock - start) seconds.")</syntaxhighlight>


{{out}}
{{out}}
Line 958: Line 958:
{{libheader|Wren-gmp}}
{{libheader|Wren-gmp}}
This is just to give what will probably be a rare outing to the Mpf class though (despite their usage in the Julia example) we don't need 'big floats' here, just 'big ints'. Slightly slower than the Wren-cli example as a result.
This is just to give what will probably be a rare outing to the Mpf class though (despite their usage in the Julia example) we don't need 'big floats' here, just 'big ints'. Slightly slower than the Wren-cli example as a result.
<lang ecmascript>import "./gmp" for Mpz, Mpf
<syntaxhighlight lang="ecmascript">import "./gmp" for Mpz, Mpf
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 984: Line 984:
Fmt.print("Possible flip configurations: $,i", flips)
Fmt.print("Possible flip configurations: $,i", flips)
Fmt.print("Possible permuted arrangements with flips: $,i", perms * flips * arrang)
Fmt.print("Possible permuted arrangements with flips: $,i", perms * flips * arrang)
System.print("\nTook %(System.clock - start) seconds.")</lang>
System.print("\nTook %(System.clock - start) seconds.")</syntaxhighlight>


{{out}}
{{out}}