Free polyominoes enumeration: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Phix}}: complete rewrite (previous was broken under pwa/p2js))
m (syntax highlighting fixup automation)
Line 48:
{{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.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 295:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Counting polyominoes to rank 11...
Line 334:
=={{header|D}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
 
alias Coord = byte;
Line 415:
foreach (const poly; n.rank)
writefln("%-(%s\n%)\n", poly.textRepresentation);
}</langsyntaxhighlight>
{{out}}
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 476:
Translated and modified from C code: http://www.geocities.jp/tok12345/countomino.txt
 
<langsyntaxhighlight lang="d">import core.stdc.stdio: printf;
import core.stdc.stdlib: atoi;
 
Line 644:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 676:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Polyominoes do
defp translate2origin(poly) do
# Finds the min x and y coordiate of a Polyomino.
Line 750:
IO.puts "\nAll free polyominoes of rank #{n}:"
Enum.sort(Polyominoes.rank(n))
|> Enum.each(fn poly -> IO.puts "#{Polyominoes.text_representation(poly)}\n" end)</langsyntaxhighlight>
 
{{out}}
Line 801:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 975:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,002:
 
Code updated and slightly improved from: http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue5/Generating_Polyominoes
<langsyntaxhighlight lang="haskell">import System.Environment (getArgs)
import Control.Arrow ((***), first)
import Data.Set (toList, fromList)
Line 1,098:
let n = bool (read $ head args :: Int) 5 (null args)
putStrLn ("\nAll free polyominoes of rank " ++ show n ++ ":")
mapM_ (putStrLn . textRepresentation) (rank n)</langsyntaxhighlight>
{{out}}
<pre>[1,1,2,5,12,35,108,369,1285,4655]
Line 1,160:
Generating polyominoes as ascii art:
 
<langsyntaxhighlight Jlang="j">polyominoes=:verb define
if. 1>y do. i.0 0 0 return.end.
if. 1=y do. 1 1 1$'#' return.end.
Line 1,198:
trim=:verb define&|:^:2
y#~+./"1 y~:' '
)</langsyntaxhighlight>
 
Example use (boxing each pentomino for display purposes):
 
<langsyntaxhighlight lang="j"> <"2 polyominoes 5
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│#####│## │# │### │## │## │### │ ## │ # │ # │ # │ ## │
Line 1,208:
│ │# │# │# │# │## │ │## │## │### │ # │# │
│ │# │# │ │ │ │ │ │ │ │ │ │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘</langsyntaxhighlight>
 
=={{header|Java}}==
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|D]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.Point;
import java.util.*;
import static java.util.Arrays.asList;
Line 1,316:
}
}
}</langsyntaxhighlight>
 
<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
Line 1,333:
=={{header|Julia}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="julia">import Base.show, Base.==, Base.hash
 
struct Point x::Float64; y::Float64 end
Line 1,422:
 
testpolys()
</langsyntaxhighlight>{{out}}
<pre>
[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 1,471:
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
class Point(val x: Int, val y: Int) : Comparable<Point> {
Line 1,551:
for (i in 1..k) print("${rank(i).size} ")
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,576:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, sugar
 
type Point = tuple[x, y: int]
Line 1,645:
echo "\nNumber of free polyominoes of ranks 1 to $#:".format(k)
for i in 1..k: stdout.write rank(i).len, ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,668:
=={{header|Perl}}==
Only shows the polyominoes up to rank 5.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,747:
}
keys %new;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,784:
=={{header|Phix}}==
Written for clarity over raw speed.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,923:
=={{header|Python}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="python">from itertools import imap, imap, groupby, chain, imap
from operator import itemgetter
from sys import argv
Line 2,001:
print text_representation(poly), "\n"
 
main()</langsyntaxhighlight>
{{out}}
<pre>[1, 1, 2, 5, 12, 35, 108, 369, 1285, 4655]
Line 2,060:
not included in code below). But I think it's interesting nonetheless.
 
<langsyntaxhighlight lang="racket">#lang typed/racket
;; Inspired by C code in http://www.geocities.jp/tok12345/countomino.txt
;; but tries to take advantage of arbitrary width integers
Line 2,273:
(when (< n 6) (draw-polynominoes p))
(printf "count: ~a~%~%" (length (polynominoes-shapes p)))
(flush-output))))</langsyntaxhighlight>
 
{{out}}
Line 2,353:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">require 'set'
 
def translate2origin(poly)
Line 2,424:
n = ARGV[0] ? ARGV[0].to_i : 5
puts "\nAll free polyominoes of rank %d:" % n
rank(n).sort.each{|poly| puts text_representation(poly),""}</langsyntaxhighlight>
 
{{out}}
Line 2,476:
Translation of [[Free_polyominoes_enumeration#Haskell|Haskell]] via [[Free_polyominoes_enumeration#D|Java]]
{{works with|Scala|2.12}}
<langsyntaxhighlight Scalalang="scala">object Free {
type Point = (Int, Int)
type Polyomino = List[Point]
Line 2,544:
}
}
}</langsyntaxhighlight>
 
<pre>(0,0) (0,1) (1,1) (1,2) (2,1)
Line 2,561:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func translate2origin(poly) {
# Finds the min x and y coordiate of a Polyomino.
var minx = poly.map(:head).min
Line 2,636:
var n = (ARGV[0] ? ARGV[0].to_i : 5)
say ("\nAll free polyominoes of rank %d:" % n)
rank(n).sort.each{|poly| say text_representation(poly).join("\n")+"\n" }</langsyntaxhighlight>
{{out}}
<pre style="height:250px">
Line 2,690:
{{libheader|Wren-sort}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/trait" for Comparable
import "/math" for Nums
import "/sort" for Sort, Cmp
Line 2,816:
Stdout.flush()
}
System.print()</langsyntaxhighlight>
 
{{out}}
10,333

edits