嘎嘎生气的鸭子


56

尚不确定该图释>:U打算代表什么,但许多学者认为它看起来像只愤怒的鸭子。假设是这种情况。

任务

给定整数n(介于0和3之间,包括0和3),请打印或返回

quack

如果n = 0,

>:U

如果n = 1,

     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU

如果n = 2,或者

                  >:U         >:U
>:U               >:U         >:U
   >:U       >:U  >:U         >:U
      >:U         >:U         >:U
         >:U      >:U         >:U
      >:U         >:U         >:U
   >:U       >:U  >:U         >:U
>:U               >:U         >:U
                     >:U>:U>:U

如果n = 3。

您可以假设输入将始终有效。输出中不应有前导空格,但任何数量的尾随空格都可以。鸭子(@cobaltduck可能例外)对漏洞没有任何容忍度。以字节为单位的最短代码获胜。


90
首先我在想“呵呵,鸭子是什么?”。幸运的是,您已经提供了到维基百科页面的链接。
阿德南

6
“鸭子没有漏洞。” 但是你是鸟而不是鸭,这是否意味着我们可以利用漏洞?:P
Downgoat

6
@Downgoat不。同样,该编辑完全没有必要,因此我将其回滚。
Alex A.

6
@Downgoat s /:P />:U /
ETHproductions '16

7
“鸭子没有漏洞。” 我的朋友,这是一个危险的刻板印象。如果我不知道你的意图,我可能会发怒。
cobaltduck

Answers:


19

CJam,108 90 85字节

"quack"">:U":D"scT¦{$ì"{269b22bSf*D}:F~[ZYB].*s.+s5/"ÿ3nÜïS{JÐø¦yûn"F*33/z]ri=zN*

请注意,某些字符不可打印。在线尝试!

背景

前两个输出在CJam中不可压缩。

压缩最后一个输出很简单。剥离所有换行符后,我们可以在出现时拆分结果字符串>:U,并计算每个结果字符串的长度。

这导致数组

[18 9 0 15 9 3 7 2 9 6 9 9 9 6 9 6 9 9 3 7 2 9 0 15 9 21 0 0 0]

我们可以通过将其从22转换为269来有效地存储,产生数字

[255 12 51 110 220 239 83 123 74 208 248 166 121 251 110 17]

由于每个数字都小于256,因此我们可以将其存储为单个字节。

最后,如果我们转置行和列,则压缩第三个输出将变得更加容易:

 > > 
  >  

 : : 

UUUU 
    U
    U
    U
UUUU

再次计算非空白字符之间的空格,我们得到数组

[1 1 3 8 1 6 0 0 0 5 4 4 0 0 0 0 0]

变成

[115 159 99 84 166 123 36 236 6]

从基数22转码为基数269时。

这个怎么运作

"quack"   e# Push the first output.

">:U":D   e# Push the second output, and save it in D.

e# Push [115 159 99 84 166 123 36 236 6] as bytes.

"scT¦{$ì"

{         e# Define a code block:
  269b22b e#   Transcode from base 269 to base 22.
  Sf*     e#   Replace each digit with a string of that many spaces.
  D       e#   Push D.
}         e#
:F~       e# Save the block in F. Execute it.
[ZYB]     e# Push [3 2 11].
.*        e# Vectorized repeat; push [">>>" "::" "UUUUUUUUUUU"].
s         e# Flatten the array of strings.
.+        e# Append the nth character to the nth string of spaces.
s5/       e# Flatten and split into chunks of length 5.

e# Push [255 12 51 110 220 239 83 123 74 208 248 166 121 251 110 17] as bytes.

"ÿ3nÜïS{JÐø¦yûn"

F         e# Execute F.
*         e# Join the resulting array of strings of spaces, separating by ">:U".
33/       e# Split into chunks of length 33.
z         e# Zip; transpose rows with columns.

]         e# Wrap the entire stack in an array.
ri        e# Read a token from STDIN and interpret it as an integer.
=         e# Retrieve the element at the corresponding index.
z         e# Zip; transpose rows with columns or map "string" to ["string"].
N*        e# Join, separating by linefeeds.

2
我喜欢这:D两根弦之间的多余部分。
Zgarb '16

7
@Zgarb他抚慰了这位伟大的鸭子霸主。鸭子现在微笑了。
Alex A.

7
我浏览了整个页面,思考:“ 172 ... 162 ... 182 ...我可以击败所有这些。等等,90?哦,这是无与伦比的Dennis ...”
ETHproductions 2016年

22

Java中,303个 286字节

感谢@VoteToClose,节省了17个字节!

实际上并不是最短的意思,我只是想尝试使用Java会很有趣。

创建代表普通字符串的字符串变量列表,然后创建所有输出的数组,然后输出正确的字符串。

String a(int y){String n="\n",d=">:U",A=" ",B=A+A,C=B+B,D=C+C,a="U"+B+" U"+n,G=D+A,H=C+B,c=d+G+d+n,E=B+A,F=C+E;String[]z={"quack",d,C+A+a+">"+B+": "+a+" >"+E+a+">"+B+": "+a+C+B+"UUU",D+D+B+c+d+D+F+c+B+A+d+F+d+B+c+H+d+G+c+G+d+H+c+H+d+G+c+E+d+F+d+B+c+d+D+F+c+D+D+C+A+d+d+d};return z[y];}

取消高尔夫:

String a(int y) {
    String n = "\n", d = ">:U", A = " ", B = A + A, C = B + B, D = C + C,
            a = "U" + B + " U" + n, G = D + A, H = C + B, c = d + G + d + n,
            E = B + A, F = C + E;
    String[] z = { "quack", d, C + A + a + ">" + B + ": " + a + " >" + E + a + ">" + B + ": " + a + C + B + "UUU", D + D + B + c + d + D + F + c + B + A + d + F + d + B + c + H + d + G + c + G + d + H + c + H + d + G + c + E + d + F + d + B + c + d + D + F + c + D + D + C + A + d + d + d };
    return z[y];
}

这是我在此站点上的第一个答案,所以请告诉我我做错了什么。


2
看起来不错!不错的第一篇文章,欢迎来到PPCG!
Conor O'Brien

@AlexA。感谢您让我知道,我已将其修复。
FlyingPiMonster '16

2
@ kittycat3141看起来很棒。好的解决方案,感谢您参与我的挑战!:)
Alex A.

似乎D+A并且C+B经常出现,足以进一步打入2个新变量。我也丝毫没有感觉到使用带有for循环的数组可能会有所帮助,但是我还没有弄清楚如何做……
Addison Crump

9

05AB1E162个 159 157字节

该死,太久了,但至少是这样:

">:U"VI3Qið16×7166b1ð:0Y:DUJ,Yð13×JD?X,3838b1ð:0Y:D?X,16255b1ð:0Y:D?X,16367b1ð:0Y:4F?X,}ð21×Y3×J,}¹2Qið4×" U   U"©J,">  :"®JD," >  "?®,,ð6×'U3×J,}¹iY,}"quack

在线尝试!


说明

代码的第一部分存在">:U"V,它设置Y为该字符串。之后,我们只需要检查输入是否等于3即可I3Qi。如果相等,我们将打印出喜:

N = 3

它首先以ð16×推入16个空格字符开始。在那之后,有一个数字7166b">:U "在Retina :)的帮助下,这部分内容涉及到。我使用此脚本将字符串转换为二进制数。在那之后,我们得到的1ð:0Y:一部分,它取代了每1一个空格字符,每0Y,它已经被设置为>:U。之后,我们D将这个字符串更新,X使用U并存储在J堆栈中。我们使用弹出该窗口,,该窗口显示带有换行符的完整字符串。之后的所有其他内容都基于同一原则。if语句在第二个结束}

完整的转换可以在这里找到。

N = 2

现在,我们检查输入是否等于2。这是在¹2Qi零件上完成的。此后,如果相等,则使用推动空格字符4次ð4×。之后,我们将" U U"字符串推入并使用©(从Jelly:p偷来的想法)存储它。我们再次J进入堆栈,并用换行符打印。之后,我们压入该"> :"字符串,在堆栈中检索" U U"using ®,并将该字符串uplicate并将它们都打印在同一行上。JD

小测试,你会做这:" > "?®,

在打印完上面的字符串之后,我们得到了脸部第二行的副本,并进行了打印(因为它与第二行相同)。

本案例涵盖的最后一部分是:

ð6×'U3×J,

ð6×        # Push the space character × 6
   'U3×    # Push the "U" character three times
       J,  # Join and print pop with a newline

N = 1

这个更容易解释:

¹1QiY

¹1Qi   # Check if the input is equal to 1
    Y  # Push the magpie face
       # This is then implicitly printed

N = 0

¹0Qi"quack

¹0Qi        # Check if the input is equal to 0
    "quack  # Weird sound what magpies make
            # Implicitly printed

说明PLZ?:P
Addison Crump

@VoteToClose完成:)
Adnan

“路太长”?看到我的回答:-P
路易斯·门多

2
喜气洋洋的嘎嘎叫。显然。
Alex A.

8

Vitsy172个 171 159字节

真是的 如果我想展示一些方法的力量,那我就明白了。

' 'V1+m
'kcauq'Z
'U:>'Z
58m5m6m'   > 'Z5m6m'UUU'68m
f3+bm9mamcm98m2m6bmcmam9mf6+8m3\[2m]
a'U   U'Z
' :  >'Z5m
Z2m98ma2m
\VZ
2mfbm
VVVZ2m78m2mVV7m
8m7m
68m2m9bm

在线尝试!

这是如何工作的,方法是在整个过程中调用各种方法。解释如下:

' 'V1+m
' 'V      Save character literal ' ' as a permanent variable.
    1+    Add one to the top item of the stack (input + 1)
      m   Go to that index of code.

'kcauq'Z
'kcauq'   Push 'quack' to the stack.
       Z  Output everything in the stack as a char.

'U:>'Z
'U:>'Z    Ouput ">:U" with the same method as the previous line.

Now on to some more... interesting lines.

58m5m6m'   > 'Z5m6m'UUU'68m
5              Push space, push 4
 8m            Call the 8th line index.
               As we will soon see, the 8th line index duplicates the space
               the number of times specified by the number just before the call 
               (4 in this case)
   5m          Call the 5th line index.
               The 5th line index outputs the 'U   U' and a newline.
     6m        Call the 6th line index.
               The 6th line index outputs '>  : U   U' and a newline.
'   > 'Z       Output ' >   '.
        5m6m   Same method calls as before.
'UUU'          Push 'UUU'.
     68m       Push 6, then call the 8th line index. This gives us the correct padding.

f3+bm9mamcm98m2m6bmcmam9mf6+8m3\[2m]
f3+              Push 18.
   bm            Call the 11th line index.
                 The 11th line index calls the 8th line index (which we've already seen
                 in action) and then the 7th line index, which you can find and explanation
                 for below (it does a lot)
     9m          Call the 9th line index.
                 The 9th line index outputs '>:U               >:U         >:U' (explanation lower)
       am        Call the 10th line index.
                 ...I'm gonna stop explaining these and just tell you to go to the lines now. :P
         cm      Call the 12th line index.
9                Push space, push 9.
 8m              Call the 8th line index (explained below and before).
   2m            Call the 2nd line index.
     6           Push 6.
      bm         Call the 11th line index. We've finished up to '>:U      >:U         >:U' now.
cm               You guessed it! Call the 12th line index. (explanation below)
  am             Call the 10th line index. (explanation below)
    9m           Call the 9th line index. (explanation below)
f6+              Push space, push 19 21.
   8m            Call the 8th line index. (explanation below)
     3\[2m]      Call the 2nd line index thrice.

All of the rest of these methods are supporting methods now.

a'U   U'Z       Output 'U   U' followed by a newline.

' :  >'Z5m      Output '>  : U   U' followed by a newline.

Z2m98ma2m
Z               Output everything currently in the stack.
 2m             Call the 2nd line index.
   9            Push space, push 8.
    8m          Call the 8th line index. (explained below)
      a         Push a newline to the stack.
       2m       Call the 2nd line index.
                This handles the biggest angry duck face's faces showing the eyebrows and eyes.

\VZ
\V    Push space as many times as the top item specifies.
  Z   Output everything in the stack.

2mfbm
2m      Call the 2nd line index.
  f     Push space, push 14.
   bm   Go to the 11th line index.
        This handles the mouth and some parts of the eyebrows of the biggest duck face.

VVVZ2m78m2mVV7m
VVVZ              Output 3 spaces (and whatever was pushed before it)
    2m            Call the 2nd line index.
      7           Push space, push 6.
       8m         Call the 8th line index. (explained... above)
         2m       Call the 2nd line index.
           VV     Push 2 spaces.
             7m   Call the 7th line index.

8m7m     This is pretty damn self-explanatory if you've read this far.

68m2m9bm
6            Push space, push 5.
 8m          Call the 8th line index.
   2m        Call the 2nd line index.
     9       Push space, push 9.
      bm     Call the 11th line index.

这段代码很荒谬。它的详细形式是:

toggle single quote;
 ;
toggle single quote;
save top as permanent variable;
push 1;
add top two;
goto top method;
:toggle single quote;
k;
push 12;
push 10;
flatten top two stacks;
q;
toggle single quote;
output stack as chars;
:toggle single quote;
U;
clone current stack;
go forward;
toggle single quote;
output stack as chars;
:push 5;
push 8;
goto top method;
push 5;
goto top method;
push 6;
goto top method;
toggle single quote;
 ;
 ;
 ;
go forward;
 ;
toggle single quote;
output stack as chars;
push 5;
goto top method;
push 6;
goto top method;
toggle single quote;
U;
U;
U;
toggle single quote;
push 6;
push 8;
goto top method;
:push 15;
push 3;
add top two;
push 11;
goto top method;
push 9;
goto top method;
push 10;
goto top method;
push 12;
goto top method;
push 9;
push 8;
goto top method;
push 2;
goto top method;
push 6;
push 11;
goto top method;
push 12;
goto top method;
push 10;
goto top method;
push 9;
goto top method;
push 15;
push 6;
add top two;
push 8;
goto top method;
push 3;
repeat next instruction set top times;
begin recursive area;
push 2;
goto top method;
end recursive area;
:push 10;
toggle single quote;
U;
 ;
 ;
 ;
U;
toggle single quote;
output stack as chars;
:toggle single quote;
 ;
clone current stack;
 ;
 ;
go forward;
toggle single quote;
output stack as chars;
push 5;
goto top method;
:output stack as chars;
push 2;
goto top method;
push 9;
push 8;
goto top method;
push 10;
push 2;
goto top method;
:repeat next instruction set top times;
save top as permanent variable;
output stack as chars;
:push 2;
goto top method;
push 15;
push 11;
goto top method;
:save top as permanent variable;
save top as permanent variable;
save top as permanent variable;
output stack as chars;
push 2;
goto top method;
push 7;
push 8;
goto top method;
push 2;
goto top method;
save top as permanent variable;
save top as permanent variable;
push 7;
goto top method;
:push 8;
goto top method;
push 7;
goto top method;
:push 6;
push 8;
goto top method;
push 2;
goto top method;
push 9;
push 11;
goto top method;

7

JavaScript(ES6),163字节

var solution =

n=>["quack",d=">:U",`5U3U
>2:1U3U
1>3U3U
>2:1U3U
6UUU`,`99090
096090
30702090
609090
906090
609090
30702090
096090
993000`][n].replace(/\d/g,c=>+c?" ".repeat(c):d)
<input type="number" oninput="R.textContent=solution(+this.value)"><pre id="R"></pre>

说明

使用JavaScript进行压缩:游程编码。19映射到许多空格的数字,0映射到愤怒的鸭子脸的数字,以及其他任何字符都保持不变。


1
对于非高尔夫语言而言,这非常令人印象深刻,但是似乎缺少了n = 3中心上方和下方的行。
ETHproductions 2016年

@ETHproductions糟糕,不确定发生了什么。立即修复。
user81655 '16

7

Japt,116 105 102 99 96个字节

["quack""c)`+«öÂ[@=^Gñ`1]o2"mc r'4#¿+R "4z>2:z >2z>2:z6UUU"rz" U3U
" '1]®r'1">:U" r"%d"_SpZ}ÃgU

包含不可打印的内容。在线测试!

这个怎么运作

前两个字符串完全不压缩。第三是通过用其长度代替空格每个运行时,然后简单地压缩" U3U""z"。最后一个更复杂:

  1. 用其长度替换每次2-9个空格。
  2. 替换>:U1
  3. 替换191\n4。(4未在字符串的其他任何地方使用。)
  4. 将每个有效字节码(10- 2550A- FF)映射到具有该char码的字符。

结果字符串只有21 19个字节长,但解压缩又占用31 29 个字节。

解压缩字符串后,我们只需将项目放置U在输入位置。(排列数组是[0,3,2,1]因为存在一个错误,该错误使它从数组的末尾而不是开头开始计数。)

[                      // Create an array of the following:
  "quack"              //  "quack".

  "c)`+«öÂ[@=^Gñ`1]o2" //  Take this string (contains an unprintable).
                       //  Map each char to its char code.
                       //  This produces "994196431712461949164619431712419649931112".
  r'4#¿+R              //  Replace each "4" with the char code of "¿" (191) + a newline.

  "4z>2:z >2z>2:z6UUU" //  Take this string.
  rz" U3U\n"           //  Replace each "z" with " U3U\n".

  '1                   //  "1".
]
®              Ã       // Map each item by this function:
r'1">:U"               //  Replace each "1" with ">:U".
r"%d"_SpZ}             //  Replace each remaining digit Z with Z spaces.

gU                     // Get the item at index -U, wrapping. 
                       // (This should just be U, but there's a bug which negates it.)

5

MATL283 ×182字节

@Adnan节省了101个字节!

对于情况2和情况3,它使用十进制数来编码索引0... 3到四个字符的索引。十进制数也许可以使用base-64编码进行压缩,但是很糟糕!

另外,壳体3由@Adnan建议一个非常好的特技用于:定义的二进制序列来编码每一行,其中01对应于空间和>:U分别。

~?'quack'}G1=?'>:U'}G2=?' :>U'12336 8466480h2109488h8466480h4032h4YA47-)}268697600 67174401h16795656h67174464h67174912h67174464h16795656h67174401h14680064h"@BP48+]Xh49'>:U'YX48 32YXc

在线尝试!


3
对于最后一种情况,使用this怎么样,用1空格替换和0>:U字符串替换。我不知道这是否有帮助。
阿德南

@Adnan这是个好主意!
路易斯·门多

5
@Adnan已保存101个字节:-O
Luis

这是惊人的!很好的答案!:)
阿德南(Adnan)2016年

5

肉桂胶,76字节

0000000: 6c33 502b 2c4d 4cce b636 54b3 b30a b536  l3P+,ML..6T....6
0000010: 5253 0081 5010 e6b2 5350 b082 3215 ecb0  RS..P...SP..2...
0000020: 8a42 1487 865a 1bab 2960 00a0 79c8 6c2e  .B...Z..)`..y.l.
0000030: 2026 a002 4221 0430 55c0 5938 cd40 9720   &..B!.0U.Y8.@. 
0000040: 6c06 6177 90e9 17ac 4102 4100            l.aw....A.A.

自从在挑战之后创建肉桂胶以来,就没有竞争了。

在线尝试。

说明

第一个字节l指定模式:在这种情况下,它告诉Cinnamon Gum进入查找表模式。然后,肉桂胶将字符串的其余部分(已使用压缩zopfli --deflate)解压缩为:

0&quack;1&>:U;2&     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU;3&                  >:U         >:U
>:U               >:U         >:U
   >:U       >:U  >:U         >:U
      >:U         >:U         >:U
         >:U      >:U         >:U
      >:U         >:U         >:U
   >:U       >:U  >:U         >:U
>:U               >:U         >:U
                     >:U>:U>:U

然后将其除以;,将每个键值对(带有&定界符)放入字典中,然后输出dictionary[input]


5

JavaScript的ES6,232个 223 203 188字节

ETHproductions 节省了29 44个字节!

n=>[`quack`,r=`>:U`,`     U   U
>  : U   U
 >   U   U
>  : U   U
      UUU`,`00022
10022
0100 1  12
00122
20012
00122
0100 1  12
10022
0000211`.replace(/\d/g,N=>[g=`   `,r,g+g+g+r][N])][n]

测试一下!


当然可以,如果不是,可以将10设为三进制数吗?
Downgoat '16

1
@Downgoat btoaing和trinary都更长。
·奥布莱恩

旧答案,但是您可以通过更改r+g+g+g+rg+g+g+r并相应地调整字符串来节省15个字节。
ETHproductions'Aug

0

GML,265个字节

@ kittycat3141的一个很好的移植端口,建议使用@VoteToClose的两个新变量(我分别命名为G和H)进一步打高尔夫球。我还设法使用GML相对宽松的语法来进一步缩短它。

d=">:U"A=" "B=A+A;C=B+B;D=C+C;G=D+A;H=C+B;a="U"+B+" U"+"#"c=d+G+d+"#"E=B+A;F=C+E;x[0]="quack"x[1]=d;x[2]=C+A+a+">"+B+": "+a+" >"+E+a+">"+B+": "+a+H+"UUU"x[3]=D+D+B+c+d+D+F+c+B+A+d+F+d+B+c+H+d+G+c+G+d+H+c+H+d+G+c+E+d+F+d+B+c+d+D+F+c+D+D+C+A+d+d+d;return x[argument0]
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.