func translate2origin(poly) {
var minx = poly.map(:head).min
var miny = poly.map(:tail).min
poly.map {|p| [p.head-minx, p.tail-miny] }.sort
}
func rotate90(x,y) { [y, -x] }
func reflect(x,y) { [-x, y] }
func rotations_and_reflections(poly) {
gather {
take(poly)
take(poly.map!{ rotate90(_...) })
take(poly.map!{ rotate90(_...) })
take(poly.map!{ rotate90(_...) })
take(poly.map!{ reflect(_...) })
take(poly.map!{ rotate90(_...) })
take(poly.map!{ rotate90(_...) })
take(poly.map!{ rotate90(_...) })
}
}
func canonical(poly) {
rotations_and_reflections(poly).map{|pl| translate2origin(pl) }
}
func contiguous(x, y) {
[[x-1, y], [x+1, y], [x, y-1], [x, y+1]]
}
func new_points(poly) {
var points = Set()
poly.each { points << contiguous(_...)... }
points - poly
}
func new_polys(polys) {
var pattern = Set()
polys.map { |poly|
gather {
new_points(poly).each { |point|
var pl = translate2origin(poly + [point])
next if pattern.has(pl)
take canonical(pl).each{ pattern << _ }.min
}
}...
}
}
func rank(n) {
given (n) {
when (0) { [[]] }
when (1) { [[[0,0]]] }
else { new_polys(rank(n-1)) }
}
}
func text_representation(poly) {
var table = Hash()
for x,y in (poly) { table{[x,y]} = '#' }
var maxx = poly.map(:head).max
var maxy = poly.map(:tail).max
(0..maxx).map{|x| (0..maxy).map{|y| table{[x,y]} \\ ' ' }.join }
}
say 8.of { rank(_).len }
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" }