Free polyominoes enumeration: Difference between revisions

Content added Content deleted
(→‎{{header|Phix}}: complete rewrite (previous was broken under pwa/p2js))
m (syntax highlighting fixup automation)
Line 48: Line 48:
{{trans|D}}
{{trans|D}}
Turns out the source for the counting only version of the D code example could be tweaked to show solutions as well. The max rank can be changed by supplying a command line parameter. The free polyominos of any rank can be displayed by changing the variable named '''target''' to a reasonable number. This program will also indicate the estimated times for larger ranks.
Turns out the source for the counting only version of the D code example could be tweaked to show solutions as well. The max rank can be changed by supplying a command line parameter. The free polyominos of any rank can be displayed by changing the variable named '''target''' to a reasonable number. This program will also indicate the estimated times for larger ranks.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 295: Line 295:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Counting polyominoes to rank 11...
<pre>Counting polyominoes to rank 11...
Line 334: Line 334:
=={{header|D}}==
=={{header|D}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang d>import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.typecons, std.conv;


alias Coord = byte;
alias Coord = byte;
Line 415: Line 415:
foreach (const poly; n.rank)
foreach (const poly; n.rank)
writefln("%-(%s\n%)\n", poly.textRepresentation);
writefln("%-(%s\n%)\n", poly.textRepresentation);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 476: Line 476:
Translated and modified from C code: http://www.geocities.jp/tok12345/countomino.txt
Translated and modified from C code: http://www.geocities.jp/tok12345/countomino.txt


<lang d>import core.stdc.stdio: printf;
<syntaxhighlight lang="d">import core.stdc.stdio: printf;
import core.stdc.stdlib: atoi;
import core.stdc.stdlib: atoi;


Line 644: Line 644:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 676: Line 676:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule Polyominoes do
<syntaxhighlight lang="elixir">defmodule Polyominoes do
defp translate2origin(poly) do
defp translate2origin(poly) do
# Finds the min x and y coordiate of a Polyomino.
# Finds the min x and y coordiate of a Polyomino.
Line 750: Line 750:
IO.puts "\nAll free polyominoes of rank #{n}:"
IO.puts "\nAll free polyominoes of rank #{n}:"
Enum.sort(Polyominoes.rank(n))
Enum.sort(Polyominoes.rank(n))
|> Enum.each(fn poly -> IO.puts "#{Polyominoes.text_representation(poly)}\n" end)</lang>
|> Enum.each(fn poly -> IO.puts "#{Polyominoes.text_representation(poly)}\n" end)</syntaxhighlight>


{{out}}
{{out}}
Line 801: Line 801:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 975: Line 975:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,002: Line 1,002:


Code updated and slightly improved from: http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue5/Generating_Polyominoes
Code updated and slightly improved from: http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue5/Generating_Polyominoes
<lang haskell>import System.Environment (getArgs)
<syntaxhighlight lang="haskell">import System.Environment (getArgs)
import Control.Arrow ((***), first)
import Control.Arrow ((***), first)
import Data.Set (toList, fromList)
import Data.Set (toList, fromList)
Line 1,098: Line 1,098:
let n = bool (read $ head args :: Int) 5 (null args)
let n = bool (read $ head args :: Int) 5 (null args)
putStrLn ("\nAll free polyominoes of rank " ++ show n ++ ":")
putStrLn ("\nAll free polyominoes of rank " ++ show n ++ ":")
mapM_ (putStrLn . textRepresentation) (rank n)</lang>
mapM_ (putStrLn . textRepresentation) (rank n)</syntaxhighlight>
{{out}}
{{out}}
<pre>[1,1,2,5,12,35,108,369,1285,4655]
<pre>[1,1,2,5,12,35,108,369,1285,4655]
Line 1,160: Line 1,160:
Generating polyominoes as ascii art:
Generating polyominoes as ascii art:


<lang J>polyominoes=:verb define
<syntaxhighlight lang="j">polyominoes=:verb define
if. 1>y do. i.0 0 0 return.end.
if. 1>y do. i.0 0 0 return.end.
if. 1=y do. 1 1 1$'#' return.end.
if. 1=y do. 1 1 1$'#' return.end.
Line 1,198: Line 1,198:
trim=:verb define&|:^:2
trim=:verb define&|:^:2
y#~+./"1 y~:' '
y#~+./"1 y~:' '
)</lang>
)</syntaxhighlight>


Example use (boxing each pentomino for display purposes):
Example use (boxing each pentomino for display purposes):


<lang j> <"2 polyominoes 5
<syntaxhighlight lang="j"> <"2 polyominoes 5
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│#####│## │# │### │## │## │### │ ## │ # │ # │ # │ ## │
│#####│## │# │### │## │## │### │ ## │ # │ # │ # │ ## │
Line 1,208: Line 1,208:
│ │# │# │# │# │## │ │## │## │### │ # │# │
│ │# │# │# │# │## │ │## │## │### │ # │# │
│ │# │# │ │ │ │ │ │ │ │ │ │
│ │# │# │ │ │ │ │ │ │ │ │ │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘</lang>
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|D]]
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|D]]
{{works with|Java|8}}
{{works with|Java|8}}
<lang java>import java.awt.Point;
<syntaxhighlight lang="java">import java.awt.Point;
import java.util.*;
import java.util.*;
import static java.util.Arrays.asList;
import static java.util.Arrays.asList;
Line 1,316: Line 1,316:
}
}
}
}
}</lang>
}</syntaxhighlight>


<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
Line 1,333: Line 1,333:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang julia>import Base.show, Base.==, Base.hash
<syntaxhighlight lang="julia">import Base.show, Base.==, Base.hash


struct Point x::Float64; y::Float64 end
struct Point x::Float64; y::Float64 end
Line 1,422: Line 1,422:


testpolys()
testpolys()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 1,471: Line 1,471:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Python}}
{{trans|Python}}
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


class Point(val x: Int, val y: Int) : Comparable<Point> {
class Point(val x: Int, val y: Int) : Comparable<Point> {
Line 1,551: Line 1,551:
for (i in 1..k) print("${rank(i).size} ")
for (i in 1..k) print("${rank(i).size} ")
println()
println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,576: Line 1,576:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import algorithm, sequtils, strutils, sugar
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils, sugar


type Point = tuple[x, y: int]
type Point = tuple[x, y: int]
Line 1,645: Line 1,645:
echo "\nNumber of free polyominoes of ranks 1 to $#:".format(k)
echo "\nNumber of free polyominoes of ranks 1 to $#:".format(k)
for i in 1..k: stdout.write rank(i).len, ' '
for i in 1..k: stdout.write rank(i).len, ' '
echo()</lang>
echo()</syntaxhighlight>


{{out}}
{{out}}
Line 1,668: Line 1,668:
=={{header|Perl}}==
=={{header|Perl}}==
Only shows the polyominoes up to rank 5.
Only shows the polyominoes up to rank 5.
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 1,747: Line 1,747:
}
}
keys %new;
keys %new;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,784: Line 1,784:
=={{header|Phix}}==
=={{header|Phix}}==
Written for clarity over raw speed.
Written for clarity over raw speed.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Polyominoes.exw</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Polyominoes.exw</span>
<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 1,881: Line 1,881:
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">5</span> <span style="color: #008080;">then</span> <span style="color: #000000;">print_polys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">5</span> <span style="color: #008080;">then</span> <span style="color: #000000;">print_polys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,923: Line 1,923:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang python>from itertools import imap, imap, groupby, chain, imap
<syntaxhighlight lang="python">from itertools import imap, imap, groupby, chain, imap
from operator import itemgetter
from operator import itemgetter
from sys import argv
from sys import argv
Line 2,001: Line 2,001:
print text_representation(poly), "\n"
print text_representation(poly), "\n"


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 2,060: Line 2,060:
not included in code below). But I think it's interesting nonetheless.
not included in code below). But I think it's interesting nonetheless.


<lang racket>#lang typed/racket
<syntaxhighlight lang="racket">#lang typed/racket
;; Inspired by C code in http://www.geocities.jp/tok12345/countomino.txt
;; Inspired by C code in http://www.geocities.jp/tok12345/countomino.txt
;; but tries to take advantage of arbitrary width integers
;; but tries to take advantage of arbitrary width integers
Line 2,273: Line 2,273:
(when (< n 6) (draw-polynominoes p))
(when (< n 6) (draw-polynominoes p))
(printf "count: ~a~%~%" (length (polynominoes-shapes p)))
(printf "count: ~a~%~%" (length (polynominoes-shapes p)))
(flush-output))))</lang>
(flush-output))))</syntaxhighlight>


{{out}}
{{out}}
Line 2,353: Line 2,353:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Python}}
{{trans|Python}}
<lang ruby>require 'set'
<syntaxhighlight lang="ruby">require 'set'


def translate2origin(poly)
def translate2origin(poly)
Line 2,424: Line 2,424:
n = ARGV[0] ? ARGV[0].to_i : 5
n = ARGV[0] ? ARGV[0].to_i : 5
puts "\nAll free polyominoes of rank %d:" % n
puts "\nAll free polyominoes of rank %d:" % n
rank(n).sort.each{|poly| puts text_representation(poly),""}</lang>
rank(n).sort.each{|poly| puts text_representation(poly),""}</syntaxhighlight>


{{out}}
{{out}}
Line 2,476: Line 2,476:
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|Java]]
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|Java]]
{{works with|Scala|2.12}}
{{works with|Scala|2.12}}
<lang Scala>object Free {
<syntaxhighlight lang="scala">object Free {
type Point = (Int, Int)
type Point = (Int, Int)
type Polyomino = List[Point]
type Polyomino = List[Point]
Line 2,544: Line 2,544:
}
}
}
}
}</lang>
}</syntaxhighlight>


<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
Line 2,561: Line 2,561:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>func translate2origin(poly) {
<syntaxhighlight lang="ruby">func translate2origin(poly) {
# Finds the min x and y coordiate of a Polyomino.
# Finds the min x and y coordiate of a Polyomino.
var minx = poly.map(:head).min
var minx = poly.map(:head).min
Line 2,636: Line 2,636:
var n = (ARGV[0] ? ARGV[0].to_i : 5)
var n = (ARGV[0] ? ARGV[0].to_i : 5)
say ("\nAll free polyominoes of rank %d:" % n)
say ("\nAll free polyominoes of rank %d:" % n)
rank(n).sort.each{|poly| say text_representation(poly).join("\n")+"\n" }</lang>
rank(n).sort.each{|poly| say text_representation(poly).join("\n")+"\n" }</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:250px">
<pre style="height:250px">
Line 2,690: Line 2,690:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<lang ecmascript>import "/trait" for Comparable
<syntaxhighlight lang="ecmascript">import "/trait" for Comparable
import "/math" for Nums
import "/math" for Nums
import "/sort" for Sort, Cmp
import "/sort" for Sort, Cmp
Line 2,816: Line 2,816:
Stdout.flush()
Stdout.flush()
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}