Denti di leone ASCII


17

Questi sono i denti di leone ASCII:

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

I denti di leone ASCII hanno tre parametri: Lunghezza dello stelo (numero positivo compreso tra 1 e 256, numero di semi (numero positivo tra 0 e 7) e orientamento (^ o v). I denti di leone sopra indicati hanno lunghezza, semi e orientamento ( 3,5, ^), (3,2, ^), (2,3, ^) e (3,7, v) rispettivamente.

I semi vengono riempiti nel seguente ordine (capovolto per i denti di leone a testa in giù), illustrato su un dente di leone della lunghezza 2:

seeds:  0    1    2    3    4    5     6      7

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

La sfida:

Scrivere un programma / funzione che quando viene fornito un dente di leone ASCII come input, restituisce la sua lunghezza, il conteggio dei semi e l'orientamento formattati in modo simile agli esempi precedenti e quando dati parametri in quel formato restituiscono un dente di leone ASCII con tali parametri. È possibile ignorare la parentesi e supporre che l'input / output sarà un numero, una virgola, un numero, una virgola e uno ^o v. È possibile sostituire altri caratteri per ^/ vpurché possano essere facilmente interpretati come "su" / "giù" (ad esempio u/ d). Non è necessario distinguere tra denti di leone che sembrano uguali, ad esempio (2,1, ^) e (3,0, ^) o (2,1, ^) e (2,1, v). Data l'arte ASCII, entrambi i set di parametri sarebbero un output accettabile ed entrambi i set di parametri possono dare la stessa arte ASCII.

Questo è , quindi vince il codice più breve in byte.


Un programma di esempio in C # (nemmeno leggermente golfato):

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

Possiamo prendere altri simboli distinti invece di ^e v?
Kritixi Lithos,

@KritixiLithos Fintanto che possono essere facilmente interpretati come "su" e "giù", certo.
P ...

3
Come trovi la differenza tra un seme 1 di lunghezza 2 e un dente di leone 0 di lunghezza 3 seme? Per i semi 0 e 1 è anche impossibile dire se sono stati lanciati ...
Luca

@Luke Non è necessario distinguere tra alberi che sembrano uguali. Dovresti restituire la stessa arte ASCII nel caso della lunghezza 2 seme 1 della lunghezza 3 seme 0 e puoi restituire lunghezza 2 seme 1 o lunghezza 3 seme 0 quando quell'arte è l'input.
P ...

1
A meno che non mi sbagli, sembra che abbiamo una risposta che si traduce da parametri in ASCII e un'altra risposta che si traduce da ASCII in parametri. Ma dovremmo supportare entrambi i compiti, giusto?
Arnauld,

Risposte:


6

Fagiolo , 321 byte

Accetta input come stringa singola in stdin senza trascinare newline. I parametri saranno presi nello stesso modo, ma formattati come

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

I parametri di output del programma quando l'input è un dente di leone saranno nello stesso formato sopra.

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 equivalente:

+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

Prende implicitamente stdin come array separato da nuova riga di stringhe non formattate _e genera implicitamente i parametri come tripletta. Suite di test qui sotto e demo qui :

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 byte

Grazie a @Neil per aiutare a golf off 134 byte e @Kritixi Lithos per aiutare a golf off 13 byte Questo programma presume che tutti i denti di leone ASCII che sta cercando di identificare abbiano una larghezza di linea di 5 per tutte le linee della stringa. cioè: il gambo è di 2 spazi, la linea verticale quindi altri 2 spazi. (Non può classificare i denti di leone che crea a causa di questo problema)

(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`
`}

Come funziona

La funzione controlla se il primo argomento che viene fornito ha una lunghezza> 1 (è una stringa). Se il primo argomento è una stringa, identifica i dettagli del dente di leone ASCII.

Per ottenere l'altezza del dente di leone, divide la stringa attorno ai caratteri di nuova riga e conta il numero di elementi - 1. Per ottenere il numero di semi, conta il numero di caratteri non spaziali sulle due prime e due inferiori. se nella parte superiore sono presenti più caratteri, viene dichiarato verticale e utilizza il conteggio superiore 1, altrimenti viene dichiarato capovolto e utilizza il conteggio inferiore 1. Se l'altezza totale è solo 2, determina la rettilineità controllando i conteggi di ciascuna riga singolarmente e scegliendo il lato con più caratteri non spaziali.

In caso contrario, la funzione utilizza la matematica bit a bit per assegnare valori da 0 a 7 in base alla forma di ciascun livello del dente di leone da disegnare prima di convertire ciascun livello nella stringa appropriata.

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

Provalo online


1
@JonathanAllan ha risolto il problema in modo che ora classificasse un input di tarassaco
fəˈnɛtɪk

1
Prendendo solo il tuo creatore di tarassaco, sono stato quindi in grado di golf di 125 byte. Il mio codice non può essere incollato direttamente nella tua soluzione, ma forse puoi incorporare alcuni dei risparmi:(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`
Neil

1
Puoi rimuoverlo elseperché ritorni ifcomunque nella parte. Anche mentre sono qui, volevo solo sottolineare che alcune funzioni come splite joinnon hanno bisogno delle ()s quando le invochi sui `letterali di stringa con virgolette, motivo per cui non le ho incluse nel mio commento precedente.
Neil

1
Devi usare il giusto tipo di virgolette, funziona solo con `s, non 's o "s.
Neil

1
Puoi cambiare \nin join`\n`in una nuova riga (come nel personaggio). Inoltre puoi cambiare il (h>2?1:0)in giusto h>2e il (h>2?2:1)in (h>2)+1. tio.run/#IRiKF
Kritixi Lithos

1

Python 3.6, 476 453 448 413 394 byte

Soluzione :

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}")]

Risultato

>>> 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

Svelare

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}")]
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.