权衡你的话!


10

挑战

给定两个字符串,每个字符串的长度最多为30个,由可打印的ASCII字符(代码)组成,以最小的代码字节数将它们放在天平上!这包括以下步骤:[32,126]

  1. 计算并比较琴弦的重量
  2. 选择适当倾斜的ASCII艺术比例
  3. 将两根弦放在秤上

字符串可以作为数组,两个参数或任何其他合理的方法传递。


一个字符串的重量被定义为的该字符串的字符,其中权重的总和:

  • 空格的权重为0(
  • 小写字母的权重为2(abcdefghijklmnopqrstuvwxyz
  • 大写字母的权重为4(ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 所有其他符号的权重为3(!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~

秤看起来像这样:

          。_
          | _- * / \
          |-* / \
       _- * | / \
    _- * | / \
   / \ | * ------ *
  / \ |
 / \ |
/ \ |
* ------ * |
    ______ | ______
    _。
   / \ * -_ |
  / \ *-|
 / \ | * -_ 
/ \ | * -_
* ------ * | / \
          | / \
          | / \
          | / \
          | * ------ *
    ______ | ______
          。
          |
    ______ | ______
   / \ | / \
  / \ | / \
 / \ | / \
/ \ | / \
* ------ * | * ------ *
          |
          |
    ______ | ______

如果第一个字符串较重,请使用第一个图形作为输出的基础;如果第二根弦较重,则使用第二根图纸;如果琴弦的重量相等,请使用第三个。允许尾随空格。


我将使用第三幅图的一部分作为以下所有示例的基础。

第一个字符串应该放在左声像上,第二个字符串应该在右声像上。

将字符串的非空格字符放在破折号上方的6x5区域内,将其放在平底锅上,如此处#s所示(您可能最终会覆盖部分天平的秤-很好):

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

所有这些字符都应“结算”,即。-在字符串的一个字符或另一个字符上方:

  错误错误正确
    ____ ____ ____
 F /\ /\ /\
  l \ / \ / \
 / \ / hov \ / s \
/ oating \ / ering \ / ettled \
* ------ * * ------ * * ------ *

此外,整个堆栈应尽可能平整,这意味着六个1宽的列中,最高的高度和最短的高度之差不得超过1:

    错误错误正确正确
[最高:5] [最高:4] [最高:5] [最高:2]
[最短:0] [最短:2] [最短:4] [最短:2]
      5__5_ ____ 5_5__ ____
     45445&/ \ 445454 / \
     45445 $%&$ @ 445454 / \
    / 45445&%@%$&445454%&$ @%&
   / 45445 \ / && $ @ $&%\ / 445454 \ / $ @ $%$$ \
   * ------ * * ------ * * ------ * * ------ *

字符的确切顺序/排列无关紧要。以下是字符串“称重您的话!”的所有有效安排:

    ____ ____ ____ ____
   / \ / \ / \ / \
 ds!\ /欠费\ u!
 Ourwor Wihuos yoiwgr eghioo
/ Weighy \ / egyrr!\ / Wrhd!s \ / rrsuwy \
* ------ * * ------ * * ------ * * ------ *

测试用例

输入:“ CODE GOLF”,“编码挑战”
权重:32,32
示例输出:
          。 
          |
    ______ | ______
   / \ | / \
  / \ | 恩格斯
 / OO \ | 沙勒
/ CFGLED \ | /编码\
* ------ * | * ------ *
          |
          |
    ______ | ______ 
输入:“”,“ $”
权重:0,3
示例输出:
    _。
   / \ * -_ |
  / \ *-|
 / \ | * -_ 
/ \ | * -_
* ------ * | / \
          | / \
          | / \
          | / $ \
          | * ------ *
    ______ | ______
输入:“您知道他们说什么!”,“ thes'always_a_relevant_xkcd”
体重:75,65
示例输出:
          。tr_a_s
          | _hekx_y
          |-* elcdta
       _- * | 复仇
    _- * | / e's_al \
  T / \ | * ------ *
 AUYOHY |
 A!HWYK |
/ OTSMEW \ |
* ------ * |
    ______ | ______

1
尾随空格可以接受吗?
Hiatsu

@Hiatsu Yep,很好。

2
我真的很喜欢这个挑战。但是有2点阻止我给我它+1。首先:如果空间不“称量”任何东西并且不包含在艺术品中,那么为什么还要包含它们呢?首先过滤掉它们只是不必要的开销。其次:这对我来说就像是“二合一” /变色龙的挑战-挑战1:确定哪个字符串“更重”,挑战2:生成一些ASCII艺术。
毛茸茸的

@Shaggy我认为这是一个公平的评估。发布此信息使我学会保持简单。

Answers:


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个字节

-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的字符块。

A接收字符串,并使用来构建它们的块p。底部的巨型字符串是隔行扫描和压缩的三种格式的字符串。


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

注意:因为我们重复使用来计算权重,所以的权重为。iS[1]

我们计算,如果重,则等于;如果重,则等于如果两个字符串的权重相同,则等于:s0S[0]2S[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++);}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.