ASCIIタンポポ


17

ASCIIタンポポは次のとおりです。

   \|/      \ /          |      
   /|\       |    \|/    |      
    |        |     |   _\|/_
    |        |     |    /|\

ASCIIタンポポには3つのパラメータがあります。茎の長さ(1〜256の正の数、種の数(0〜7の正の数)、および方向(^またはv)。上記のタンポポは、長さ、種、および方向、 3,5、^)、(3,2、^)、(2,3、^)および(3,7、v)。

種子は、長さ2のタンポポで示されている次の順序(上下逆さまのタンポポで反転)で塗りつぶされます。

seeds:  0    1    2    3    4    5     6      7

             |   \ /  \|/  \ /  \|/  _\ /_  _\|/_
        |    |    |    |   /|\  /|\   /|\    /|\
        |    |    |    |    |    |     |      |

チャレンジ:

入力としてASCIIタンポポを指定すると、上記の例と同様にフォーマットされた長さ、シードカウント、方向を返し、その形式のパラメータを指定すると、それらのパラメータでASCIIタンポポを返すプログラム/関数を作成します。括弧を無視して、入力/出力が数字、コンマ、数字、コンマ、^またはのいずれかであると想定できますv。あなたは、のために他の文字を置換してもよい^/ v限り、彼らはまだ簡単に(例えば、「アップ」/「ダウン」として解釈することができるようu/ d)。(2,1、^)と(3,0、^)や(2,1、^)と(2,1、v)など、同じように見えるタンポポを区別する必要はありません。ASCIIアートを考えると、どちらのパラメーターセットも許容可能な出力になり、両方のパラメーターセットは同じASCIIアートを提供できます。

これはなので、バイト単位の最短コードが優先されます。


C#のサンプルプログラム(少しでもゴルフではありません):

    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() + ",^";
        }
    }

^andの代わりに他のいくつかの異なるシンボルを使用できますvか?
クリティキシリソス

@KritixiLithos「アップ」と「ダウン」として簡単に解釈できる限り、そうです。
P ...

3
長さ2の種子1と長さ3の種子0のタンポポの違いをどのように見つけますか?種子の0と1の場合、彼らが反転している場合伝えることも不可能です...
ルーク

@Luke同じように見える木を区別する必要はありません。長さ2のシード1の場合、長さ3のシード0と同じASCIIアートを返す必要があり、そのアートが入力である場合は、長さ2のシード1または長さ3のシード0を返すことができます。
P ...

1
誤解がない限り、パラメーターからASCIIに変換する回答と、ASCIIからパラメーターに変換する別の回答があるようです。しかし、私たちは両方のタスクをサポートするはずですよね?
アルノー

回答:


6

Bean、321バイト

入力を、末尾の改行なしで標準入力の単一の文字列として受け入れます。パラメーターは同じ方法で取得されますが、形式は

length (1-256)
orientation (u or d)
seeds (0-7)

入力がタンポポの場合のプログラムの出力パラメーターは、上記と同じ形式になります。

Hexdump:

00000000 26 52 ca c1 20 5d d3 d0 80 d5 cd a0 5e 80 4c cc  &RÊÁ ]ÓÐ.ÕÍ ^.LÌ
00000010 a0 45 86 25 3e 88 4d a0 6b 80 4c a0 5e 80 23 60   E.%>.M k.L ^.#`
00000020 cd a0 63 80 43 cd a0 5f 80 50 84 a3 81 00 20 5e  Í c.CÍ _.P.£.. ^
00000030 d0 84 a3 81 01 4d a0 60 80 4a c1 4c a0 45 86 25  Ð.£..M `.JÁL E.%
00000040 3a d0 84 a3 81 02 4c a0 45 92 25 3a d0 84 a3 81  :Ð.£..L E.%:Ð.£.
00000050 03 20 60 a0 5f a3 81 04 cd a0 61 80 50 84 a3 81  . ` _£..Í a.P.£.
00000060 05 20 5e cf 52 cc a0 45 86 25 3c a3 81 06 23 81  . ^ÏRÌ E.%<£..#.
00000070 07 a0 61 cf 53 d0 80 a3 81 08 20 80 b5 4c a0 43  . aÏSÐ.£.. .µL C
00000080 8c 25 3a 00 52 a0 6b d3 50 80 a0 63 20 80 7e 20  .%:.R kÓP. c .~ 
00000090 63 20 80 7b 23 00 53 d0 80 c3 cc d0 80 a0 78 20  c .{#.SÐ.ÃÌÐ. x 
000000a0 80 01 8c 25 3a d2 ce cc a0 5d 80 23 81 09 80 4c  ...%:ÒÎÌ ].#...L
000000b0 d0 84 a0 5e 25 3b 81 23 81 0a ce d3 50 80 a0 78  Ð. ^%;.#..ÎÓP. x
000000c0 20 80 7e 81 23 60 23 71 cc d2 cc d0 84 d0 84 a0   .~.#`#qÌÒÌÐ.Ð. 
000000d0 78 25 3a 25 3a 81 23 81 0b cc a5 3d 8b 4c cc d0  x%:%:.#..Ì¥=.LÌÐ
000000e0 84 d0 84 a0 78 25 39 25 39 81 50 84 d0 84 a0 78  .Ð. x%9%9.P.Ð. x
000000f0 25 3a 25 39 8d 25 3b 4c cc d0 84 d0 84 a0 78 25  %:%9.%;LÌÐ.Ð. x%
00000100 39 25 3c 81 23 81 0b 8d 25 3b 8b 4c d0 84 d0 84  9%<.#...%;.LÐ.Ð.
00000110 a0 78 25 39 25 3b 81 23 81 0b 00 20 80 7b 23 81   x%9%;.#... .{#.
00000120 04 a0 df 20 a0 5c a0 7c a0 2f 0a a0 a0 5f af fc  . ß  \ | /.  _¯ü
00000130 5c a0 fc 20 8a a0 a0 fc a0 20 a0 a0 fc a0 20 7c  \ ü .  ü    ü  |
00000140 20                                                
00000141

同等のJavaScript:

+a?                               // if input is parameters
  (
    b=(C>5)<<(o=b=="d"),          // encoding if seeds > 5 and if orientation is down
    g=[                           // storing dandelion as array of characters
      c=" _ "[b],                 // "_" if seeds > 5 and orientation is up, else " "
      " \\"[d=+(C>1)],            // "\" if seeds > 1, else " "
      " |"[C&1],                  // "|" if seeds is odd, else " "
      " /"[d],
      c,                          // "_" if seeds > 5 and orientation is up, else " "
      "\n",
      e="  _"[b],                 // "_" if seeds > 5 and orientation is down, else " "
      ...(                        // spread characters for .reverse() to be correct
        C>3?                      // if seeds > 3 "/|\" else " | "
          "/|\\":
          " | "
      ),
      e,                          // "_" if seeds > 5 and orientation is down, else " "
      ..."\n  |  ".repeat(A-1)    // repeat stem length - 1 times
    ],
    o?                            // if orientation is down, reverse
      g.reverse():
      g
  ).join(""):                     // join array of characters
  [                               // else if input is dandelion
    _.length-1,                   // length of stem is number of rows - 1
    a=="  |  "||b[2]!="|"?        // test orientation of dandelion
      _.reverse()&&"d":           // reverse rows if necessary and return "d" for down
      "u"                         // else return "u" for up
    ,
    (
      _[1][1]!=" "?               // if 1,1 is not " ", seeds is 4 or more
        4+(_[0][0]!=_[1][0])*2:   // if 0,0 or 1,0 is "_", seeds is 6 or 7
        (_[0][3]!=" ")*2          // if 0,3 is not " ", seeds is 2 or 3
    )+
    (_[0][2]!=" ")                // if 0,2 is not " ", seeds is odd
  ].join("\n")                    // join parameters with newline to match input format

暗黙的にstdinをフォーマットされていない文字列の改行で区切られた配列として受け取り_、暗黙的にパラメータをトリプレットとして出力します。以下のテストスイートとデモはこちら

const js = String.raw`
+a?                               // if input is parameters
  (
    b=(C>5)<<(o=b=="d"),          // encoding if seeds > 5 and if orientation is down
    g=[                           // storing dandelion as array of characters
      c=" _ "[b],                 // "_" if seeds > 5 and orientation is up, else " "
      " \\"[d=+(C>1)],            // "\" if seeds > 1, else " "
      " |"[C&1],                  // "|" if seeds is odd, else " "
      " /"[d],
      c,                          // "_" if seeds > 5 and orientation is up, else " "
      "\n",
      e="  _"[b],                 // "_" if seeds > 5 and orientation is down, else " "
      ...(                        // spread characters for .reverse() to be correct
        C>3?                      // if seeds > 3 "/|\" else " | "
          "/|\\":
          " | "
      ),
      e,                          // "_" if seeds > 5 and orientation is down, else " "
      ..."\n  |  ".repeat(A-1)    // repeat stem length - 1 times
    ],
    o?                            // if orientation is down, reverse
      g.reverse():
      g
  ).join(""):                     // join array of characters
  [                               // else if input is dandelion
    _.length-1,                   // length of stem is number of rows - 1
    a=="  |  "||b[2]!="|"?        // test orientation of dandelion
      _.reverse()&&"d":           // reverse rows if necessary and return "d" for down
      "u"                         // else return "u" for up
    ,
    (
      _[1][1]!=" "?               // if 1,1 is not " ", seeds is 4 or more
        4+(_[0][0]!=_[1][0])*2:   // if 0,0 or 1,0 is "_", seeds is 6 or 7
        (_[0][3]!=" ")*2          // if 0,3 is not " ", seeds is 2 or 3
    )+
    (_[0][2]!=" ")                // if 0,2 is not " ", seeds is odd
  ].join("\n")                    // join parameters with newline to match input format`;

// bean binary
const bin = bean.compile(js);

// program as function
const prog = bean.program(bin);

(document.body.onchange = function () {
  const parameters = stem.value + '\n' + orientation.value + '\n' + seeds.value;
  dandelion.textContent = prog(parameters);
  params.value = prog(dandelion.textContent);
})();
textarea {
  resize: none;
}
<script src="https://cdn.rawgit.com/patrickroberts/bean/master/dst/bean.min.js"></script>
<input id=stem type=number min=1 max=256 value=5>
<select id=orientation>
  <option value="u">u</option>
  <option value="d">d</option>
</select>
<input id=seeds type=number min=0 max=7 value=5>
<p>Dandelion (output from program given parameters)</p>
<pre id=dandelion></pre>
<p>Parameters (output from program given dandelion)</p>
<textarea id=params rows=3></textarea>


2

Javascriptの513 391 379 355バイト

134バイトのゴルフを支援してくれた@Neilと、13バイトのゴルフを支援してくれた@Kritixi Lithosに感謝します。つまり、ステムは2スペースで、垂直線はさらに2スペースです。(この問題のため、作成したタンポポを分類できません)

(x,y,z)=>{a=Array(x+1).fill(1);if(x.length>1){a=x.split`
`;h=a.length-1;t=b=i=0;for(;i<(h>1)+1;i++)for(j=0;j<5;a[h-i][j++]!=' '&&b++)a[i][j]!=' '&&t++;return[h,(t>b?t:b)-(h>1),t>b?'^':'v']}z<'v'?(a[0]=y&4?y-2:y,a[1]=y&4?7:1):(a[x-1]=1+(y>4)*2+(y>4)*(y&2),a[x]=y&1+(y>2)*6);return a.map(n=>',  |, \\ /, \\|/,_\\ /_,_\\|/_, / \\, /|\\'.split`,`[n]).join`
`}

使い方

この関数は、指定された最初の引数の長さが> 1(文字列)であるかどうかをチェックします。最初の引数が文字列の場合、ASCIIタンポポの詳細を識別します。

タンポポの高さを取得するには、文字列を改行文字で分割し、要素の数をカウントします-1.シードの数を取得するには、上2行と下2行のスペース以外の文字の数をカウントします。上部にさらに文字がある場合は、直立していると宣言され、上部のcount-1が使用されます。それ以外の場合、上下が逆であると宣言され、下部のcount-1が使用されます。合計の高さが2のみの場合、各行のカウントを個別にチェックし、スペース以外の文字が多い辺を選択することにより、直立性を決定します。

それ以外の場合、関数はビットごとの数学を使用して、各レベルを適切な文字列に変換する前に描画されるタンポポの各レベルの形状に応じて0〜7の値を割り当てます。

0:  
1:  |  
2: \\ /  
3: \\|/  
4:_\\ /_  
5:_\\|/_  
6: / \\  
7: /|\\

オンラインで試す


1
@JonathanAllanは今タンポポ入力分類するようにそれを固定
fənɛtɪk

1
タンポポのクリエーターだけを連れて、125バイトのゴルフをすることができました。私のコードは、直接あなたのソリューションに戻って貼り付けることはできませんが、多分あなたは貯蓄の一部を組み込むことができます:(x,y,z,a=[...Array(x+1)].fill(1))=>a.map(n=>', |, \\ /, \\|/,_\\ //,_\\|/_, / \\, /|\\'.split`,`[n],z<'v'?(a[0]=y&4?y-2:y,a[1]=y&4?7:1):(a[x-1]=1+(y>4)*2+(y>4)*(y&2),a[x]=y&1+(y>2)*6)).join`\n`
ニール

1
とにかくパーツelseを返すため、削除できifます。また、ここにいる間、引用符で囲まれた文字列リテラルでsを呼び出すときにs splitやをjoin必要としない関数があることを指摘したかったので、以前のコメントには含めませんでした。()`
ニール

1
適切な種類の引用符を使用する必要があります。s またはs `ではなく、s でのみ機能します。'"
ニール

1
あなたは変更することができます\njoin`\n`(文字のように)改行します。また、あなたは変更することができます(h>2?1:0)だけにh>2して(h>2?2:1)まで(h>2)+1tio.run/#IRiKF
Kritixi Lithos

1

Pythonの3.6、476 453 448 413 394バイト

解決策

def h(i):
 l,s,o=i.split(",");s=int(s);z=["  |  "];q=(int(l)-1)*z;b,d,f,h,g,c,a=["  ","\\/"][s>1]+["  ","\\/"][s>3]+["| ","||"][s%2==1]+[" ","_"][s>5]
 if"d"==o:b,d,h,f,c,g=f,h,d,b,g,c
 r=[[a+b+c+d+a]+[" "+h+g+f+" "],z][s==0];return"\n".join([q+r,r+q]["u"==o])
def j(i):
 if","in i:print(h(i))
 else:[print(f"{m},{j},{k}")for m in range(257)for j in range(8)for k in"ud"if i==h(f"{m},{j},{k}")]

結果

>>> j("6,5,u")
 \|/
 /|\
  |
  |
  |
  |
  |
>>> j("5,2,d")
  |
  |
  |
  |
  |
 / \
>>> j("3,2,u")
 \ /
  |
  |
  |
>>> j("_\|/_\n /|\ \n  |  \n  |  \n  |  \n  |  \n  |  ")
6,7,u
>>> j(" \|/ \n /|\ \n  |  \n  |  \n  |  \n  |  ")
5,5,u
>>> j("  |  \n  |  \n  |  \n  |  \n  |  ")
4,1,u
4,1,d
5,0,u
5,0,d

展開する

def g(i):
    def h(i):                       # this function draw dandelion
        l, s, o = i.split(",")      # split argument 
        s = int(s)

        # Calcul the string in the flower for up case 
        #   _\|/_   --> abcdb  --> when s=7 we have a=_ b=\ c=| d=/ h=/ g=| f=\ 
        #    /|\    -->  hgf  
        a = "_" if s > 5 else " "
        b = "\\" if s > 1 else " "
        d = "/" if s > 1 else " "
        h = "/" if s > 3 else " "
        f = "\\" if s > 3 else " "
        c = "|" if s%2 == 1 else " "
        g = "|" if s%2 == 1 else "|"

        # Shuffle a bit if the position is down 
        if"d"==o:
            b,d,h,f,c,g=f,h,d,b,g,c

        # treate the case to remove the line with ony white space
        if s==0:
            res=["  |  "]
        else:
            # assemble all piece of the flower
            res += [a+b+c+d+a]
            res += [" "+h+g+f+" "]

        # add stem up or down
        if o=="u":
            res = res + (int(l)-1) * ["  |  "]
        else:
            res = (int(l)-1) * ["  |  "] + res
        return "\n".join(res)

    if "," in i:
        print(h(i))
    else:
        # search in all flower posibility if we can recreate the input
        [print(m,j,k) for m in range(1, 257) for j in range(0, 8)for k in "ud"if i == h(f"{m},{j},{k}")]
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.