These are ASCII dandelions:
\|/ \ / |
/|\ | \|/ |
| | | _\|/_
| | | /|\
ASCII dandelions have three parameters: Length of the stem (positive number between 1 and 256, number of seeds (positive number between 0 and 7), and orientation (^ or v). The above dandelions have for length, seeds and orientation, (3,5,^), (3,2,^), (2,3,^) and (3,7,v) respectively.
Seeds are filled in in the following order (flipped upside down for head-down dandelions), illustrated on a dandelion with length 2:
seeds: 0 1 2 3 4 5 6 7
| \ / \|/ \ / \|/ _\ /_ _\|/_
| | | | /|\ /|\ /|\ /|\
| | | | | | | |
The Challenge:
Write a program/function which when given an ASCII dandelion as input, returns its length, seed count, and orientation formatted similarly to the above examples and when given parameters in that format returns an ASCII dandelion with those parameters. You can ignore the parenthesis and assume the input/output will be a number, a comma, a number, a comma, and either ^
or v
. You may substitute other characters for ^
/v
so long as they can still be easily interpreted as 'up'/'down' (for example, u
/d
). You need not distinguish between dandelions that look the same, such as (2,1,^) and (3,0,^) or (2,1,^) and (2,1,v). Given the ASCII art, either set of parameters would be an acceptable output, and both sets of parameters can give the same ASCII art.
This is code-golf, so shortest code in bytes wins.
An example program in C# (not even slightly golfed):
string Dandelion(string s)
{
if (s.Contains(','))
{
//got parameters as input
string[] p = s.Split(',');
//depth and width (number of seeds)
int d = int.Parse(p[0]);
int w = int.Parse(p[1]);
//draw stem
string art = " |";
while (d > 2)
{
d--;
art += "\n |";
}
//draw head
string uhead = (w % 2 == 1 ? "|" : " ");
string dhead = uhead;
if (w > 1)
{
uhead = "\\" + uhead + "/";
dhead = "/" + dhead + "\\";
if (w > 5)
{
uhead = "_" + uhead + "_\n /|\\";
dhead = "_\\|/_\n " + dhead;
}
else if (w > 3)
{
uhead = " " + uhead + " \n /|\\";
dhead = " \\|/ \n " + dhead;
}
else
{
uhead = " " + uhead + " \n |";
dhead = " |\n " + dhead;
}
}
else
{
uhead = " " + uhead + "\n |";
dhead = " |\n " + dhead;
}
//add head to body
if (p[2] == "^")
{
return uhead + "\n" + art;
}
return art + "\n" + dhead;
}
else
{
//ASCII input
string[] p = s.Split('\n');
int l = p.Length - 1;
int offset = 0;
//find first non-' ' character in art
while (p[0][offset] == ' ')
{
offset++;
}
int w = 0;
if (p[0][offset] == '|')
{
//if '|', either head-down or no head.
if (offset == 0 || p[l][offset - 1] == ' ')
{
//if no space for a head to the left or no head at the bottom, no head.
return l.ToString() + ",1,^";
}
//head must have at least size 2, or else indistinguishable from no head case
w = 6;
if (p[l][offset] == '|')
{
//odd sized head
w = 7;
}
if (offset == 1 || p[l - 1][offset - 2] == ' ')
{
//not size 6 or 7
w -= 2;
if (p[l - 1][offset - 1] == ' ')
{
//not size 4 or 5
w -= 2;
}
}
return l.ToString() + "," + w.ToString() + ",v";
}
else if (p[0][offset] == '\\')
{
//head at least size 2 and not 6/7, or indistinguishable from no head.
w = 4;
if (p[0][offset + 1] == '|')
{
w = 5;
}
if (p[1][offset] == ' ')
{
w -= 2;
}
}
else
{
w = 6;
if (p[0][offset + 2] == '|')
{
w = 7;
}
}
return l.ToString() + "," + w.ToString() + ",^";
}
}
^
andv
? \$\endgroup\$