Drawing convex polyiamonds

7

Description

OEIS sequence A096004 gives the

Number of convex triangular polyominoes [polyiamonds] containing n cells.

It begins:

1, 1, 1, 2, 1, 2, 2, 3, 2, 2, 2, 3, 2, 3, 3, 5

For example, a(8)=3 with the following three convex polyiamonds:

Input: 8
Output:
  *---*---*---*---*
 / \ / \ / \ / \ /
*---*---*---*---*

    *---*---*
   / \ / \ /
  *---*---*
 / \ / \ /
*---*---*

    *---*
   / \ / \
  *---*---*
 / \ / \ / \
*---*---*---*

Requirements

The goal of this challenge is to write a program that takes in an integer n (in any reasonable format) and outputs ASCII art of all convex polyiamonds with n cells (in any orientation), with each polyiamond separated by two newlines.

Your program must be able to handle up to a(30) in under 60 seconds.


Scoring

This is a challenge, so fewest bytes wins.

Peter Kagey

Posted 2018-07-16T19:23:43.530

Reputation: 2 789

A Wumpus answer to this one would be great!

– sundar - Reinstate Monica – 2018-07-16T20:19:18.493

60 seconds on which machine.... – user202729 – 2018-07-17T04:08:56.220

1On whichever machine—I trust people to be reasonable. – Peter Kagey – 2018-07-17T05:29:08.540

And this will happen... – user202729 – 2018-07-17T06:06:18.387

You should specify that the polyiamonds are considered the same under rotation and reflection. – user202729 – 2018-07-17T06:53:27.790

2I'd seriously consider removing the time restriction. I don't see what it adds to the challenge other than ambiguity. – Post Rock Garf Hunter – 2018-07-17T18:38:06.493

Answers

2

Charcoal, 89 bytes

NθF⊕θF⊕ιF⊕κF⊕λ¿∧¬‹ι⁺⁺κλμ⁼⁻×ιιθΣE⟦κλμ⟧×νν«⸿G↙⊕⊗μ↘⊕⊗⁻ι⁺κμ→⊕×⁴κ↗⊕⊗⁻ι⁺κλ↖⊕⊗λ“⌈∨¿ZH↖↖⸿T u≡9”D⎚

Try it online! Link is to verbose version of code. Thanks to @LevelRiverSt for explaining the OEIS formula. Explanation:

Nθ

Input n. (Using the OEIS formula names rather than the Charcol names here.)

F⊕θF⊕ιF⊕κF⊕λ

Loop p from 0 to n, d from 0 to p, c from 0 to d and b from 0 to c inclusive. This satisfies the condition b<=c<=d<=p.

¿∧¬‹ι⁺⁺κλμ⁼⁻×ιιθΣE⟦κλμ⟧×νν«

Check the other two conditions: b+c+d<=p and n=p*p-b*b-c*c-d*d. Sadly the power function doesn't vectorise yet otherwise that would save 2 bytes.

⸿

Leave a blank line between polyiamonds.

G↙⊕⊗μ↘⊕⊗⁻ι⁺κμ→⊕×⁴κ↗⊕⊗⁻ι⁺κλ↖⊕⊗λ“⌈∨¿ZH↖↖⸿T u≡9”

Draw the polyiamond. Five of the sides are calculated, and the sixth is inferred and the polygon filled using the compressed string / \ *--- \ / --*-, which results in the desired pattern. (The pattern doesn't start with *--- because the blank line causes the pattern to be offset.)

D⎚

Output and clear the canvas ready for the next polyiamond. This is quicker than positioning the cursor.

Neil

Posted 2018-07-16T19:23:43.530

Reputation: 95 035

5

Ruby, 202 bytes

->n{(r=[*0..n]).product(r,r,r).map{|i|p,b,c,d=i;b>c||c>d||b+c+d>p||p*p-b*b-c*c-d*d!=n||0.upto(p-d<<1){|j|s=" "*j+"*---\\ / "[j%2*4,4]*p*2;(k=b*4-j)>0&&s[0,k]=" "*k;puts s[0,[p*4+1-j,p*4+1-c*4+j].min]}}}

Try it online!

Uses formula per the OEIS sequence: search for solutions where n = p**2 - b**2 -c**2 - d**2.

n =the number of triangular tiles

p =the side length of a large equilateral triangle (pointing downwards)

b,c,d =the side length of three smaller equilateral triangles, which are removed from the corners of the large equilateral triangle to get the required shape.

Some additional conditions (per OEIS) are needed to avoid duplicates: b<=c<=d and b+c+d<=p

Level River St

Posted 2018-07-16T19:23:43.530

Reputation: 22 049

Aha, I'd been staring at that OEIS formula wondering what it all meant... – Neil – 2018-07-19T23:47:35.680