Pesare le tue parole!


10

La sfida

Date due stringhe, ciascuna della lunghezza massima di 30, costituita da caratteri ASCII stampabili (codici [32,126] ), inseriscili su una scala di bilanciamento nel minor numero di byte di codice! Questo consiste nei seguenti passaggi:

  1. Calcola e confronta i pesi delle stringhe
  2. Scegli la scala di arte ASCII inclinata in modo appropriato
  3. Posizionare le due stringhe sulla scala

Le stringhe possono essere passate come un array, due argomenti o qualsiasi altro metodo ragionevole.


Il peso di una stringa è definito come la somma dei pesi dei caratteri di quella stringa, dove:

  • Gli spazi hanno un peso di 0 ( )
  • Le lettere minuscole hanno un peso di 2 ( abcdefghijklmnopqrstuvwxyz)
  • Le lettere maiuscole hanno un peso di 4 ( ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • Tutti gli altri simboli hanno un peso di 3 ( !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~)

Le scale si presentano così:

          . _
          | _- * / \
          | - * / \
       _- * | / \
    _- * | / \
   / \ | * ------ *
  / \ |
 / \ |
/ \ |
* ------ * |
    ______ | ______
    _.
   / \ * -_ |
  / \ * - |
 / \ | * -_ 
/ \ | * -_
* ------ * | / \
          | / \
          | / \
          | / \
          | * ------ *
    ______ | ______
          .
          |
    ______ | ______
   / \ | / \
  / \ | / \
 / \ | / \
/ \ | / \
* ------ * | * ------ *
          |
          |
    ______ | ______

Se la prima stringa è più pesante, utilizzare il primo disegno come base dell'output; se la seconda stringa è più pesante, utilizzare il secondo disegno; se le stringhe hanno lo stesso peso, usa la terza. È consentito lo spazio bianco finale.


Userò un segmento del terzo disegno come base per tutti i seguenti esempi.

La prima stringa deve essere posizionata sul riquadro sinistro e la seconda stringa sul riquadro destro.

Posiziona una stringa su una padella posizionando i suoi caratteri non spaziali all'interno dell'area 6x5 immediatamente sopra i trattini, come indicato da #s qui (potresti finire per sovrascrivere parte della scala del bilanciamento - va bene):

 ###### _
 ######
 ######
 ######
/ ###### \
* ------ *

Tutti questi personaggi dovrebbero essere "risolti", ad es. sopra un -carattere o un altro carattere della stringa:

  ERRATO ERRATO CORRETTO
    ____ ____ ____
 f / \ / \ / \
  l \ / \ / \
 / \ / hov \ / s \
/ oating \ / eri ng \ / ettled \
* ------ * * ------ * * ------ *

Inoltre, l'intero stack dovrebbe essere il più piatto possibile, il che significa che delle sei colonne larghe 1, l'altezza della più alta e l'altezza della più corta non devono differire di più di 1:

    ERRATO ERRATO CORRETTO CORRETTO
[più alto: 5] [più alto: 4] [più alto: 5] [più alto: 2]
[più breve: 0] [più breve: 2] [più breve: 4] [più breve: 2]
      5__5_ ____ 5_5__ ____
     45445 & / \ 445454 / \
     45445 $% & $ @ 445454 / \
    / 45445 &% @% $ e 445454% e $ @% &
   / 45445 \ / & $ @ $ &% \ / 445454 \ / $ @ $% $$ \
   * ------ * * ------ * * ------ * * ------ *

L'esatto ordine / disposizione dei personaggi non ha importanza. Di seguito sono riportate tutte le disposizioni valide per la stringa "Pesare le tue parole!":

    ____ ____ ____ ____
   / \ / \ / \ / \
 DS! \ / owd oe \ u! Wd \
 ourwor Wihuos yoiwgr eghioo
/ Weighy \ / egyrr! \ / Wrhd! S \ / rrsuwy \
* ------ * * ------ * * ------ * * ------ *

Casi test

INPUT: "CODICE GOLF", "sfide di codifica"
PESI: 32, 32
USCITA DI ESEMPIO:
          . 
          |
    ______ | ______
   / \ | / \
  / \ | ESN \ s
 / OO \ | Challe
/ CFGLED \ | / Codifica \
* ------ * | * ------ *
          |
          |
    ______ | ______ 
INPUT: "", "$"
PESI: 0, 3
USCITA DI ESEMPIO:
    _.
   / \ * -_ |
  / \ * - |
 / \ | * -_ 
/ \ | * -_
* ------ * | / \
          | / \
          | / \
          | / $ \
          | * ------ *
    ______ | ______
INPUT: "LO SAPI COSA DICONO!", "There_always_a_relevant_xkcd"
PESI: 75, 65
USCITA DI ESEMPIO:
          . tr_a_s
          | _hekx_y
          | - * elcdta
       _- * | revanw
    _- * | / E's_al \
  T / \ | * ------ *
 AUYOHY |
 A! HWYK |
/ OTSMEW \ |
* ------ * |
    ______ | ______

1
Lo spazio bianco finale è accettabile?
Hiatsu,

@Hiatsu Sì, va bene.
negativo sette

2
Mi piace questa sfida, davvero. Tuttavia ci sono 2 punti che mi impediscono di dare il mio +1. Primo: se gli spazi non "pesano" nulla e non vengono inclusi nell'opera d'arte, perché includerli? È solo inutile sovraccarico per prima filtrarli. In secondo luogo: mi sembra una sfida "2 in 1" / camaleonte - Sfida 1: Determina quale stringa è "più pesante", Sfida 2: Genera arte ASCII.
Shaggy,

@Shaggy Penso che sia una valutazione corretta. Pubblicare questo mi ha insegnato a mantenere le cose semplici.
negativo sette

Risposte:


7

Carbone , 110 byte

UMθ⪫⪪ι ω≔⁰ηFθ≦⁻ΣEι⁻⁺³№ακ№βκηP-×⁷_↑χ.¶¶≔³ζ¿η«≔∨›⁰η⁵ζM±⁶±²_F⁴⁺¶*-§_|_ι¿›⁰η‖»P-⁺|×⁶_J±⁴±ζFθ«←⁶↑*↗⁴↓↘⁴←↖*←⪪ι⁶J⁹⁻ζ⁶

Provalo online! Il collegamento è alla versione dettagliata del codice. Accetta l'input come una matrice di due stringhe. Spiegazione:

UMθ⪫⪪ι ω

Rimuovere gli spazi da entrambe le stringhe.

≔⁰η

Assumi il bilanciamento dei pesi.

Fθ

Passa sopra entrambe le stringhe.

≦⁻ΣEι⁻⁺³№ακ№βκη

Sottrai la differenza di esecuzione dal peso della stringa.

P-×⁷_↑χ.¶¶

Stampa la base della bilancia.

≔³ζ

Supponiamo che entrambe le padelle siano 3 da terra.

¿η«

Se i pesi non si bilanciassero ...

≔∨›⁰η⁵ζ

... calcola l'altezza della padella sinistra ...

M±⁶±²_F⁴⁺¶*-§_|_ι

... disegna l'equilibrio inclinato verso destra ...

¿›⁰η‖»

... e rifletti se la padella sinistra fosse più pesante.

P-⁺|×⁶_

Altrimenti traccia un bilancio di livello.

J±⁴±ζ

Passa alla prima scala.

Fθ«

Passa sopra gli ingressi.

←⁶↑*↗⁴↓↘⁴←↖*

Disegna il piatto della scala.

←⪪ι⁶

Tagliare l'ingresso in sottostringhe di lunghezza 6 e stamparle capovolte in modo da riempire la padella verso l'alto.

J⁹⁻ζ⁶

Passa alla seconda scala.


6

Python 2 , 1101 1071 855 837 byte

-216 byte con compressione stringa

-18 byte riducendo alcune ripetizioni

from zlib import decompress as Z
from base64 import b64decode as D
r=range(6)
j="".join
w=lambda a:0if not a else(2+2*(a[0]<'[')if a[0].isalpha()else 3)+w(a[1:])
t=Z(D('eJxT0FKIV1BQ0AWT8SAIJsAcXTCppQAGumBSSx8MYsBAC0kCAiCySAIKEJW4ZHGpxA8AejMemQ=='))
p=lambda k,l,m:j(map(j,[(t[2*l+m::6][:30-len(k)]+k)[i::6]for i in r]))
def A(a,b):
 e=cmp(w(j(a.split())),w(j(b.split())))+1;return Z(D('eJxVUUGuhTAI3HOKWTdBW/U2SHoQ4O6ftvrMb0hLZJgZAYABFZB5KxD4zrZtNJOJMaHWIIoa0D6Ao+jrWRiHEI7kMcQg9VLBCo9O3dCbdanepOvZQztF9rRH2xUlwISehIZ96HltLFqu1IMF2p1QH/S+1Ge7CT5blIVOxqUWFudjqHPSwhitjPbzf7uZ1HaIaG2hShFTfU7Eca6J7MBr1K+3/YbRVLd2VlE5oilp7EG/gV7+DPQuSAsZPm7PZE9HBY2G+ctS/QzR+whSGlPAGz4mkkl5Sf18SMvkyL9iF6aLd2WLUm/KDVzvJu93k2tLZXlwetgLmFH4MzcKCaJnqX1Fz3iOf4//Pi7EwP4BHmyJpg=='))[e::3].format(*map(lambda c:[p(j(([a,b]*3)[c].split()),e,c)[i::5]for i in r],r))

Provalo online!

Correre come A(string_one, string_two .

w calcola il peso della stringa in modo ricorsivo.

t è il testo compresso e intrecciato di tutte e sei le scale possibili, che risulta comprimere molto bene.

p prende la stringa (con gli spazi rimossi), il peso della stringa e il lato del bilanciamento su cui si trova la stringa e crea un blocco di caratteri 5x6.

Aprende le stringhe e costruisce i loro blocchi con p. La stringa gigante nella parte inferiore è composta da tre stringhe di formato intrecciate e compresse.


3

JavaScript (ES6),  340  337 byte

Accetta input come una matrice di 2 array di caratteri. Disegna il carattere di output per carattere.

S=>(s=Math.sign(~(g=i=>(S[i]=S[i].filter(c=>c>' '?i+=/[a-z]/gi.test(c)?c>{}?2:4:3:0),i))(0)+g(1))+1,g=x=>y>10?'':(X=(r=x>9)?20-x:x,Y=(r?2-s:s)*2+y,S[+r][X>0&&X<7&&47-Y*6+X]||`. /\\|-_*
`[~X?x-10?y>9?X>3?6:1:[x+y*3-17,2*y-4,x+~y*3][s]/2|X<4?Y<5|Y>8?Y-9|X>7?1:X%7?5:7:~X+Y?X+Y-8?1:2^r:3^r:[7-x%3,6,5+x%3][s]:y&&4:8])+g(x<21?x+1:!++y))(y=0)

Provalo online!

Come?

S[io] e restituisce il suo peso:

g = i => (                   // i = string index
  S[i] = S[i].filter(c =>    // for each character c in S[i]:
    c > ' ' ?                //   if c is not a space:
      i +=                   //     update i:
        /[a-z]/gi.test(c) ?  //       if c is a letter:
          c > {} ?           //         if c is in lower case:
            2                //           add 2 to i
          :                  //         else:
            4                //           add 4 to i
        :                    //       else (not a letter):
          3                  //         add 3 to i
    :                        //   else (a space):
      0                      //     remove c from S[i]
  ), i                       // end of filter(); return i
)                            //

ioS[1] .

S0S[0]2S[1]1 se entrambe le stringhe hanno lo stesso peso:

s = Math.sign(~g(0) + g(1)) + 1

Richiamiamo ora la seconda funzione di supporto per disegnare l'output:

g = x =>                     // given x:
  y > 10 ?                   //   if we've reached the last row:
    ''                       //     stop recursion
  :                          //   else:
    ( X = (r = x > 9) ?      //     r = true if we're on the right side
        20 - x               //       X = 20 - x on the right side
      :                      //     or:
        x,                   //       X = x on the left side
      Y = (r ? 2 - s : s)    //     Y is the position of the scale tray
          * 2 + y,           //     according to s and the current side
      S[+r][                 //     we try to extract a character from S[0] or S[1]:
        X > 0 && X < 7 &&    //       provided that we're located above the tray
        47 - Y * 6 + X       //       and using an index based on (X, Y)
      ] ||                   //     if this character doesn't exist,
      `. /\\|-_*\n`[INDEX]   //     we need to draw the balance instead
    ) +                      //     (see the next part)
    g(x < 21 ? x + 1 : !++y) //     append the result of a recursive call

Dove INDEXviene calcolato come segue:

~X ?                         // if this is not the last character of the current row:
  x - 10 ?                   //   if this is not the central column:
    y > 9 ?                  //     if this is the last row:
      X > 3 ? 6 : 1          //       draw the base ('_' or a space)
    :                        //     else:
      [ x + y * 3 - 17,      //       attempt to draw the beam:
        2 * y - 4,           //         using an equation depending on s
        x + ~y * 3           //         whose result must be -1, 0 or 1
      ][s] / 2 | X < 4 ?     //       if it's invalid or X is less than 4:
        Y < 5 | Y > 8 ?      //         if we're not over the chains:
          Y - 9 | X > 7 ?    //           if we're not over the pan:
            1                //             draw a space
          :                  //           else:
            X % 7 ? 5 : 7    //             draw the pan ('-' or '*')
        :                    //         else:
          ~X + Y ?           //           if this is not an interior chain:
            X + Y - 8 ?      //             if this is not an exterior chain:
              1              //               draw a space
            :                //             else:
              2 ^ r          //               draw the exterior chain ('/' or '\')
          :                  //           else:
            3 ^ r            //             draw the interior chain ('/' or '\')
      :                      //       else:
        [ 7 - x % 3,         //         draw the beam, using either '_' -> '-' -> '*'
          6,                 //         or just '_'
          5 + x % 3          //         or '*' -> '-' -> '_'
        ][s]                 //         depending on s
  :                          //   else:
    y && 4                   //     draw the central pillar ('|' or '.')
:                            // else:
  8                          //   append a line feed

1

Java 10, 1043 993 988 983 byte

(a,b)->{var r=new char[11][21];for(var A:r)java.util.Arrays.fill(A,' ');a=a.replace(" ","");b=b.replace(" ","");int A=s(a),B=s(b),j,c,i=3;for(;++i<17;r[3][i]=A==B?'_':32)r[10][i]=95;for(i=11;i-->1;)r[i][10]=i>0?'|':46;if(A==B){r[8][0]=r[8][7]=r[8][13]=r[8][20]=42;for(i=0;++i<20;)if(i<8|i>13)r[8][i]=45;for(i=8;i-->4;r[i][7-i]=r[i][20-i]=47)r[i][i]=r[i][i+13]=92;A=B=8;}else{r[5][i=A<B?0:13]=r[5][i+7]=r[9][13-i]=r[9][20-i]=42;for(i=5;i-->1;r[i][A>B?18-i*3:2+i*3]=42)r[i][A>B?17-i*3:3+i*3]=45;for(i=0;++i<20;)r[i>13?A>B?5:9:A>B?9:5][i>13|i<7?i:1]=45;for(i=9;i-->1;r[i][i>4?A>B?8-i:21-i:A>B?17-i:4-i]=47)r[i][i>4?A>B?i-1:i+12:A>B?i+16:i+3]=92;A=(A>B?r[i=0][16]=r[1][13]=r[3][7]=r[4][4]=95:(r[0][i=4]=r[1][7]=r[3][13]=r[4][16]=95));A=9-i;B=5+i;}c(r,a,A,7);c(r,b,B,20);return r;};int s(String s){int r=0;for(int i:s.getBytes())r+=i>64&i<91?4:i>96&i<123?2:3;return r;}void c(char[][]r,String s,int p,int q){for(int c=0,i=p,j;i-->p-5;)for(j=q;j-->q-6&c<s.length();)r[i][j]=s.charAt(c++);}

-5 byte grazie a @ceilingcat .

Gli input sono due stringhe, che risulteranno in una matrice di caratteri come risultato.

Provalo online.

Spiegazione:

// Method with two String parameters and character-matrix return-type:
(a,b)->{
  // Result matrix, with 11 rows and 21 columns:
  var r=new char[11][21];
  // Initially fill the entire matrix with spaces:
  for(var A:r)java.util.Arrays.fill(A,' ');
  // Remove all spaces from the input-Strings:          
  a=a.replace(" ","");b=b.replace(" ","");
  // Call a separated method to calculate the scores of both input-Strings:
  int A=s(a),B=s(b),

  // Fill the cells for the base with '_',
  // and also fill the cells for the balance-bar with '_' when the scores are equal:
  j,c,i=3;for(;++i<17;r[3][i]=A==B?'_':32)r[10][i]=95;
  // Fill the cells for the stand with '|':
  for(i=11;i-->1;)r[i][10]=i>0?'|'
  // And the top of it with '.':
  :46;

  // If the scores are equal:
  if(A==B){
    // Fill the four appropriate cells for the sides of the scales with '*':
    r[8][0]=r[8][7]=r[8][13]=r[8][20]=42;
    // Fill the appropriate cells for the scales themselves with '-':
    for(i=0;++i<20;)if(i<8|i>13)r[8][i]=45;
    // Fill the appropriate cells of the robes with '/' and '\':
    for(i=8;i-->4;r[i][7-i]=r[i][20-i]=47)r[i][i]=r[i][i+13]=92;
    // Set A and B both to 8 to use later on:
    A=B=8;}
  // If the scores aren't equal:
  else{
    // Fill the four appropriate cells for the sides of the scales with '*':
    r[5][i=A<B?0:13]=r[5][i+7]=r[9][13-i]=r[9][20-i]=42;
    // Fill the appropriate four cells of the balance-bar with '-':
    for(i=5;i-->1;r[i][A>B?18-i*3:2+i*3]=42)r[i][A>B?17-i*3:3+i*3]=45;
    // Fill the appropriate cells of the scales with '-':
    for(i=0;++i<20;)r[i>13?A>B?5:9:A>B?9:5][i>13|i<7?i:1]=45;
    // Fill the appropriate cells of the robes with '/' and '\':
    for(i=9;i-->1;r[i][i>4?A>B?8-i:21-i:A>B?17-i:4-i]=47)r[i][i>4?A>B?i-1:i+12:A>B?i+16:i+3]=92;
    // Fill the four appropriate cells of the balance-bar with '_',
    // and set A and B to 9 and 5 depending on which score is higher:
    A=(A>B?r[i=0][16]=r[1][13]=r[3][7]=r[4][4]=95:(r[0][i=4]=r[1][7]=r[3][13]=r[4][16]=95));A=9-i;B=5+i;}
  // Call a separated method to fill the cells above the scales with the input-characters:
  c(r,a,A,7);c(r,b,B,20);
  // And finally return the resulting character-matrix:
  return r;};

// Separated method to calculate the score of the given String:
int s(String s){
  // Initially start the score-sum at 0:
  int r=0;
  // Loop over the characters of the given String:
  for(int i:s.getBytes())
    // Increase the sum by:
    r+=
      // 4 for uppercase letters:
      i>64&i<91?4
      // 2 for lowercase letters:
      :i>96&i<123?2
      // 3 for any other character:
      :3;
  // And return the resulting sum:
  return r;}

// Separated method to draw the strings on top of the scales:
void c(char[][]r,String s,int p,int q){
  // Keep a counter so we know when we're done drawing the given String:
  for(int c=0,
  // Loop over the appropriate rows bottom to top:
  i=p,j;i-->p-5;)
    // Inner loop over the appropriate cells of this row left to right,
    for(j=q;j-->q-6
    // as long as we're not done yet with the input-String:
        &c<s.length();)
      // And fill that appropriate cell with the next character in line of the given String:
      r[i][j]=s.charAt(c++);}
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.