r/dailyprogrammer • u/jnazario 2 0 • Jun 10 '15
[2015-06-10] Challenge #218 [Intermediate] Generating Polyominoes
Description
A polyomino is a collection of cells (equal-sized squares) which are connected, that is, each cell shares a border with another one. Think about tetris pieces, those would be tetrominoes - they each have four squares, and there are 5 unique combinations of their squares into unique shapes. Polyominoes are considered equivalent if they can be made to look identical if they are rotated or flipped. For additional background on polyominoes see this link: http://home.adelphi.edu/~stemkoski/mathematrix/polys.html
Input Description
You will be given a single integer, this is the polyomino order to calculate and draw. Example:
4
Formal Output Description
Draw the complete set of unique polyominoes in ASCII art. Example output:
##
##
##
##
#
#
#
#
#
#
##
#
##
#
Challenge Input
6
Challenge Input Solution
######
#
#####
#
#####
#
#####
##
####
##
####
# #
####
# #
####
##
####
#
#
####
#
#
####
#
####
#
#
####
#
#
####
#
#
####
#
#
####
#
#
####
#
#
###
#
#
#
##
#
##
#
#
##
#
#
#
##
##
#
##
##
##
#
###
#
#
###
##
#
#
##
###
#
#
###
#
#
##
##
#
#
###
# #
#
# #
###
#
# #
###
#
##
#
##
#
#
##
###
#
###
##
#
###
##
#
##
##
#
13
u/Cosmologicon 2 3 Jun 10 '15 edited Jun 10 '15
I tried to go for the easy-to-understand approach in Python. It's definitely not the shortest or the fastest, but I hope it shows that this problem can be solved without any complicated algorithms, by just breaking it up into small, straightforward pieces:
5
u/ponkanpinoy Jun 10 '15 edited Jun 10 '15
Lisp, also not very clever. Culling the dupes at every level keeps the accumulator from becoming absolutely enormous. Main function that generates the polyominoes could do with a
mapcarI think, I'll edit when I get it.EDIT: accumulator now build up with maps instead of loops.
EDIT2 formatting
An n-omino is represented as a list of cells, where each cell is the dotted pair
(row . col).This procedure grows polyominoes in all four directions from the origin so we need to normalize all coordinates so that they are minimal and non-negative.
We also need to generate a list of transformations for each polyomino.
We need a way to recognize that two polyominos are equivalent, and cull the list to remove duplicates.
Generating the list of n-ominoes follows the algorithm:
((0 . 0))For each polyomino in the list:
Repeat from (2) until you achieve the desired n
(defun neighbors (polyomino) "Get the neighbors of a polyomino, which is a list of cells (n . m)" (flet ((f (cell) (let ((a (car cell)) (b (cdr cell))) (list (cons (1+ a) b) (cons (1- a) b) (cons a (1+ b)) (cons a (1- b)))))) (set-difference (remove-duplicates (mapcan #'f polyomino) :test #'equal) polyomino :test #'equal)))
(defun n-ominoes (n) (labels ((f (n accum) (if (= n 1) (unique-polyominoes accum) (f (1- n) (mapcan (lambda (polyomino) (mapcar (lambda (cell) (cons cell polyomino)) (neighbors polyomino))) (unique-polyominoes accum)))))) (f n '(((0 . 0))))))
Printing the polyominoes:
Sample output:
4
u/wizao 1 0 Jun 11 '15 edited Jun 11 '15
Haskell:
EDIT: there's a bug with my rotations. I need to use the "updated" height of the poly after each step (or toggle between w/h).
3
u/mikevdg Jun 10 '15
The "unique" part is doing my head in. They can be translated or rotated.
3
u/glenbolake 2 0 Jun 10 '15
Here's my class-based Python 2.7 solution. I used sets all over the place and normalized in the constructor to make finding unique polyominoes easier.
3
u/JeffJankowski Jun 11 '15 edited Jun 11 '15
Pretty ugly and verbose C#. Definitely a naive, brute-force method, but it's sped up by parallelizing the inductive generation of polyominoes. I am able to generate and print all 17,073 free undecominoes (n=11) in ~15 sec. Anyone else have some benchmarks to compared against?
Output (n=4):
3
u/katyne Jun 11 '15
Java - brute force recursive shape generation + transformations (transpose, reflect, translate)
Shapes are generated recursively in an NxN matrix, initially only containing one [0,0] square.
findAdjacent()enumerates all valid and available positions to connect a new square:To eliminate transformations and duplicates, overridden Shape's
equals()- to be able to checkcontains()using Collections APILink to the whole huge thing(#lolJava) + output
https://gist.github.com/dvakota/7122009e86e463dad918
1
u/marchelzo Jun 13 '15
I'm quite late on this challenge, but here is a Python 3 solution. I thought I had a really efficient way to do it, but that turned out to not work, so I reworked it into this. It's quite fast for the challenge input, but n=9 takes about 10 seconds. Still, I think it's quite easy to follow:
1
u/TieSoul 0 1 Jun 13 '15
Very, very inefficient C# solution using recursive generation of n-ominoes:
1
u/ReckoningReckoner Jun 23 '15
Ruby solution. Not the most efficient, but it works.
Sample output (size 6):
0
u/[deleted] Jun 10 '15
[deleted] — view deleted comment