ชั่งน้ำหนักคำพูดของคุณ!


10

ความท้าทาย

รับสองสตริงแต่ละความยาวสูงสุด 30 ประกอบด้วยอักขระ ASCII ที่พิมพ์ได้ (รหัส [32,126]) วางลงบนเครื่องชั่งในจำนวนไบต์ที่น้อยที่สุดของโค้ด! ประกอบด้วยขั้นตอนต่อไปนี้:

  1. คำนวณและเปรียบเทียบน้ำหนักของสตริง
  2. เลือกสเกลศิลปะ ASCII ที่เอียงได้อย่างเหมาะสม
  3. วางสองสตริงลงบนสเกล

สตริงอาจถูกส่งเป็นอาร์เรย์อาร์กิวเมนต์สองรายการหรือวิธีการอื่นที่สมเหตุสมผล


น้ำหนักของสตริงที่ถูกกำหนดให้เป็นผลรวมของน้ำหนักของตัวละครของสตริงที่ที่นี้:

  • ช่องว่างมีน้ำหนัก 0 ( )
  • อักษรตัวพิมพ์เล็กมีน้ำหนัก 2 ( abcdefghijklmnopqrstuvwxyz)
  • ตัวอักษรตัวพิมพ์ใหญ่มีน้ำหนัก 4 ( ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • สัญลักษณ์อื่น ๆ ทั้งหมดมีน้ำหนัก 3 ( !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~)

เครื่องชั่งมีลักษณะดังนี้:

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

หากสตริงแรกหนักกว่าให้ใช้การวาดแรกเป็นฐานของเอาต์พุตของคุณ ถ้าสตริงที่สองหนักให้ใช้การวาดที่สอง หากสตริงมีน้ำหนักเท่ากันให้ใช้ชุดที่สาม อนุญาตช่องว่างต่อท้าย


ฉันจะใช้ส่วนของการวาดภาพที่สามเป็นฐานสำหรับตัวอย่างต่อไปนี้ทั้งหมด

สายแรกควรวางไว้บนถาดด้านซ้ายและสายที่สองลงบนถาดด้านขวา

วางสตริงบนกระทะโดยวางอักขระที่ไม่ใช่ช่องว่างไว้ในพื้นที่ 6x5 เหนือเครื่องหมายขีดกลางดังที่ทำเครื่องหมายไว้#ที่นี่ (คุณอาจจะเขียนทับส่วนหนึ่งของเครื่องชั่ง - ซึ่งก็ดี):

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

อักขระเหล่านี้ทั้งหมดควร "ตัดสิน" เช่น เหนือ-อักขระหรืออักขระอื่นจากสตริง:

  ผิดที่ไม่ถูกต้อง
    ____ ____ ____
 f / \ / \ / \
  l \ / \ / \
 / \ / hov \ / s \
/ oating \ / eri ng \ / ettled \
* ------ * * ------ * * ------ *

นอกจากนี้สแต็กทั้งหมดควรเป็นแบบเรียบที่สุดเท่าที่จะเป็นไปได้ซึ่งหมายความว่าคอลัมน์หกกว้าง 1 คอลัมน์ความสูงของที่สูงที่สุดและความสูงของที่สั้นที่สุดจะต้องไม่แตกต่างกันมากกว่า 1:

    ผิดถูกต้องผิดถูกต้อง
[สูงที่สุด: 5] [สูงที่สุด: 4] [สูงที่สุด: 5] [สูงที่สุด: 2]
[สั้นที่สุด: 0] [สั้นที่สุด: 2] [สั้นที่สุด: 4] [สั้นที่สุด: 2]
      5__5_ ____ 5_5__ ____
     45445 & / \ 445454 / \
     45445 $% & $ @ 445454 / \
    / 45445 &% @% $ & 445454% & $ @% &
   / 45445 \ / & $ @ $ &% \ / 445454 \ / $ @ $% $$ \
   * ------ * * ------ * * ------ * * ------ *

ลำดับ / การจัดเรียงที่แน่นอนของตัวละครไม่สำคัญ ต่อไปนี้เป็นข้อตกลงที่ถูกต้องทั้งหมดสำหรับสตริง "ชั่งน้ำหนักคำพูดของคุณ!":

    ____ ____ ____ ____
   / \ / \ / \ / \
 ds! \ / เป็นหนี้ oe \ u! Wd \
 ของเราหรือ Wihuos yoiwgr eghioo
/ Weighy \ / egyrr! \ / Wrhd! s \ / rrsuwy \
* ------ * * ------ * * ------ * * ------ *

กรณีทดสอบ

อินพุต: "โค้ดกอล์ฟ", "ความท้าทายในการเขียนโค้ด"
น้ำหนัก: 32, 32
ตัวอย่างผลลัพธ์:
          . 
          |
    ______ | ______
   / \ | / \
  / \ | Nge \ s
 / OO \ | challe
/ CFGLED \ | / การเข้ารหัส \
* ------ * | * * * * * * * * ------
          |
          |
    ______ | ______ 
INPUT: "", "$"
น้ำหนัก: 0, 3
ตัวอย่างผลลัพธ์:
    _.
   / \ * -_ |
  / \ * - |
 / \ | * -_ 
/ \ | * -_
* ------ * | / \
          | / \
          | / \
          | / $ \
          | * * * * * * * * ------
    ______ | ______
อินพุต: "คุณรู้ว่าพวกเขาพูดอะไร!", "there_always_a_relevant_xkcd"
น้ำหนัก: 75, 65
ตัวอย่างผลลัพธ์:
          . tr_a_s
          | _hekx_y
          | - * elcdta
       _- * | revanw
    _- * | / e's_al \
  T / \ | * * * * * * * * ------
 AUYOHY |
 A! HWYK |
/ OTSMEW \ |
* ------ * |
    ______ | ______

1
ช่องว่างต่อท้ายยอมรับได้หรือไม่?
Hiatsu

@Hiatsu Yep นั่นเป็นเรื่องปกติ
ลบเจ็ด

2
ฉันชอบความท้าทายนี้ฉันทำจริงๆ อย่างไรก็ตามมี 2 +1จุดป้องกันผมจากการให้มันของฉัน ประการแรก: หากช่องว่างไม่ "ชั่งน้ำหนัก" อะไรเลยและไม่รวมอยู่ในงานศิลปะแล้วทำไมต้องรวมมันเข้าไปด้วย เป็นค่าใช้จ่ายที่ไม่จำเป็นเพียงเพื่อกรองพวกเขาออกก่อน ประการที่สอง: นี่เป็นความรู้สึกท้าทายสำหรับฉัน "2-in-1" / chameleon สำหรับฉัน - ความท้าทายที่ 1: ตรวจสอบว่าสตริงใดที่ "หนักกว่า" ความท้าทายที่ 2: สร้าง ASCII-art บางส่วน
Shaggy

@Shaggy ฉันคิดว่านั่นเป็นการประเมินที่ยุติธรรม การโพสต์สิ่งนี้ได้สอนให้ฉันทำสิ่งต่าง ๆ ให้เรียบง่าย
ลบเจ็ด

คำตอบ:


7

ถ่าน 110 ไบต์

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

ลองออนไลน์! การเชื่อมโยงคือการใช้รหัสเวอร์ชันอย่างละเอียด รับอินพุตเป็นอาร์เรย์ของสองสตริง คำอธิบาย:

UMθ⪫⪪ι ω

ลบช่องว่างออกจากทั้งสองสตริง

≔⁰η

สมมติว่าน้ำหนักสมดุล

Fθ

วนรอบสตริงทั้งสอง

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

ลบความแตกต่างในการทำงานจากน้ำหนักของสตริง

P-×⁷_↑χ.¶¶

พิมพ์ฐานของเครื่องชั่ง

≔³ζ

สมมติว่ากระทะทั้งสองอยู่ห่างจากพื้น 3

¿η«

หากน้ำหนักไม่สมดุล ...

≔∨›⁰η⁵ζ

... คำนวณความสูงของกระทะด้านซ้าย ...

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

... วาดสมดุลลงไปทางขวา ...

¿›⁰η‖»

... และไตร่ตรองว่ากระทะซ้ายหนักกว่าหรือไม่

P-⁺|×⁶_

มิฉะนั้นวาดสมดุลระดับ

J±⁴±ζ

ข้ามไปที่กระทะขนาดแรก

Fθ«

วนซ้ำอินพุต

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

วาดมาตราส่วน

←⪪ι⁶

แบ่งอินพุตออกเป็นสตริงย่อยของความยาว 6 และพิมพ์คว่ำลงเพื่อให้ถาดขึ้นด้านบน

J⁹⁻ζ⁶

ข้ามไปที่กระทะระดับที่สอง


6

Python 2 , 1101 1071 855 837 bytes

-216 ไบต์พร้อมการบีบอัดสตริง

-18 ไบต์โดยลดการทำซ้ำบางอย่าง

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

ลองออนไลน์!

A(string_one, string_twoทำงานเป็น

w คำนวณน้ำหนักของสตริงซ้ำ

t เป็นข้อความที่ถูกบีบอัดและอินเทอร์เลซของเครื่องชั่งทั้งหกที่เป็นไปได้ซึ่งเกิดขึ้นในการบีบอัดอย่างมาก

p รับสตริง (โดยลบช่องว่าง) น้ำหนักของสตริงและด้านข้างของยอดคงเหลือที่สตริงเปิดอยู่และสร้างบล็อกขนาด 5x6 อักขระ

Apใช้เวลาสตริงและสร้างบล็อกของพวกเขาด้วย สตริงยักษ์ที่ด้านล่างคือสตริงรูปแบบสามรายการอินเทอร์เลซและบีบอัด


3

JavaScript (ES6),  340  337 ไบต์

รับอินพุตเป็นอาร์เรย์ของอักขระ 2 อาร์เรย์ วาดอักขระเอาต์พุตโดยอักขระ

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)

ลองออนไลน์!

อย่างไร?

เรากำหนดฟังก์ชันตัวช่วยแรกที่จะลบช่องว่างออกจากสตริงอินพุต S[i] และคืนน้ำหนัก:

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

NB: เพราะเราใช้ซ้ำ i เพื่อคำนวณน้ำหนักมันปิดโดยหนึ่งสำหรับ S[1].

เราคำนวณ sซึ่งเท่ากับ 0 ถ้า S[0] หนักกว่า 2 ถ้า S[1] หนักกว่าหรือ 1 หากสตริงทั้งสองมีน้ำหนักเท่ากัน:

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

ตอนนี้เราเรียกใช้ฟังก์ชันตัวช่วยที่สองเพื่อวาดผลลัพธ์:

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

INDEXคำนวณได้ที่ไหนดังนี้:

~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 ไบต์

(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 ไบต์ขอบคุณที่@ceilingcat

อินพุตเป็นสองสตริงซึ่งจะส่งผลให้เมทริกซ์ตัวละครเป็นผลลัพธ์

ลองออนไลน์

คำอธิบาย:

// 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++);}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.