# 画一个随机的ASCII Art被子

31

========================
------------------------
//\\//\\\//\/\\///\\//\\
<<><<>>>>><<>><<<<<>><>>
/\\/\\\\/\/\/\/\////\//\
------------------------
/\/////\\///\\\//\\\\\/\
\///\/\/\\\\////\/\/\\\/
\///\/\/\\\\////\/\/\\\/
/\\\/\/\////\\\\/\/\///\
/\\\/\/\////\\\\/\/\///\
\/\\\\\//\\\///\\/////\/
------------------------
\//\////\/\/\/\/\\\\/\\/
<<><<>>>>><<>><<<<<>><>>
\\//\\///\\/\//\\\//\\//
------------------------
========================

• 他们总是24乘18。
• 顶线（第1行）和底线（第18行）=一直贯穿。
• 第2、6、13和17 -行一直贯穿。
• 线4和15是相同的随机水平对称的模式<>
• 所有其他线条（3、5、7、8、9、10、11、12、14、16）充满/\完全随机，从而使整个被子保持水平和垂直对称

# 挑战

• 在第4行的前12个字符应能是任何字符的长度12的字符串<>
• 第3、5、7、8、9行的前12个字符应该可以是任意长度的12个字符串，/并且\（彼此独立）。
• 然后将这些随机的字符串进行相应镜像以制成整个被子。

15

# CJam，61 60 58 55 54 52 51字节

"=-/</-///"{C*1{"<\/>"%1\$W%\_W%er}:F~+mrC<1FN}%s3F(

## 说明

{"<\/>"%1\$W%\_W%er}:F

"<\/>"            "Push a string with all four relevant characters.";
%           "% applied to a string and an integer N (in any order) selects every
Nth character, starting from the first. So with N = 1 this just
leaves the string unchanged, but with N = 3 it returns a string
containing only < and >.";
1\$         "Copy the string we want to mirror.";
W%       "% also takes negative arguments. Giving it -1 reverses the string.";
\_     "Swap the two strings and duplicate the <\/> or <> string.";
W%   "Reverse that one. Due to the symmetry of this string, we'll now
have the characters to be swapped at corresponding indices.";
er "Perform element-wise transliteration on the reversed input string
to complete the mirroring operation.";

"=-/</-///"                            "This string encodes the 9 different line types.
Note that for the /\ and <> lines we only use
one of the characters. This idea is due to
Sp3000. Thanks! :)";
{                   }%      "Map this block onto the characters.";
C*                         "Repeat the character 12 times, turning it into
a string.";
1{...}:F~                "Define and call F on the resulting string. The
reversal doesn't do anything, but the character
swapping creates strings containing both \/ and
<>.";
+mr             "Add the two halves together and shuffle them.";
C<           "Truncate to 12 characters. We've now got our
random half-lines.";
1F         "Call F again to mirror the half-line.";
N        "Push a newline.";
s     "Join all those separate strings together by
converting the array to a string.";
3F   "Perform one more mirroring operation on the
half-quilt, but this time only swap < and >.
This yields the correct full quilt, except
there are two newlines in the centre.";
(  "This slices the leading newline off the second
half and pushes it onto the stack.";

12

# Python 3中，257个229 192 185 176 149 143字节

from random import*
k,*L=80703,
while k:s=eval("''"+".join(sample('--==<>\/'[k%4*2:][:2],2))"*12);L=[s]+L+[s[::(-1)**k]];k//=4
*_,=map(print,L)

========================
------------------------
///////////\/\\\\\\\\\\\
>><<<>><<<><><>>><<>>><<
/\/\\/\/\\/\/\//\/\//\/\
------------------------
//\\////\\/\/\//\\\\//\\
/////\\\/\/\/\/\///\\\\\
/\\//\\/////\\\\\//\\//\
\//\\//\\\\\/////\\//\\/
\\\\\///\/\/\/\/\\\/////
\\//\\\\//\/\/\\////\\//
------------------------
\/\//\/\//\/\/\\/\/\\/\/
>><<<>><<<><><>>><<>>><<
\\\\\\\\\\\/\///////////
------------------------
========================

## 说明

（稍有过时，将在以后更新）

"444046402"对行进行编码，每个数字均引用的相关2个字符的子字符串的起始索引'--==\/<>'。通过对两个字符进行重复改组（不幸的是使用sample(...,2)，因为使用了random.shuffle）和字符串连接，从而由内而外构建了每一行。

''.join(['<','>']).join(['>','<']).join(['>','<']).join(['<','>']).join(['>','<'])

''
<>         .join(['<','>'])
>  <        .join(['>','<'])
>    <       .join(['>','<'])
<      >      .join(['<','>'])
>        <     .join(['>','<'])

L=[s]+L+[[s,s[::-1]][n<"5"]]

n<"5"条件是检查我们是否有一排包括><，在这种情况下，我们附加相同的行到后面，否则它的反面。

g=lambda s:s.translate({60:62,62:60,92:47,47:92})

6

# Python 2，300字节

from random import*
f=lambda a,b,t:t.replace(a,'*').replace(b,a).replace('*',b)
k=lambda a:''.join(sample(a*12,12))
c='-'*24
e=k('<>')
h=e+f('<','>',e[::-1])
j=[d+f('/','\\',d[::-1])for d in[k('\\/')for i in'quilt']]
g=['='*24,c,j[0],h,j[1],c]+j[2:]
print'\n'.join(g+[f('/','\\',d)for d in g[::-1]])

from random import *

change = lambda a,b,t: t.replace(a,'*').replace(b,a).replace('*',b)
pick = lambda a: ''.join(sample(a*12, 12))

innerline = '-' * 24
line4h = pick('<>')
line4 = line4h + change('<', '>', line4h[::-1])
diag = [d + change('/', '\\', d[::-1]) for d in [pick('\\/') for i in 'quilt']]

quilt = ['='*24, innerline, diag[0], line4, diag[1], innerline] + diag[2:]
print '\n'.join(quilt + [change('/', '\\', d) for d in quilt[::-1]])

========================
------------------------
\\\\/\////\\//\\\\/\////
<><<>>>><><><><><<<<>><>
/\\\\////\\\///\\\\////\
------------------------
\\\\//\///\\//\\\/\\////
//\//\\\\/\/\/\////\\/\\
\/\\\\/\//\/\/\\/\////\/
/\////\/\\/\/\//\/\\\\/\
\\/\\////\/\/\/\\\\//\//
////\\/\\\//\\///\//\\\\
------------------------
\////\\\\///\\\////\\\\/
<><<>>>><><><><><<<<>><>
////\/\\\\//\\////\/\\\\
------------------------
========================

9

2

5

6

# 杀伤人员地雷（53 58）

L←+,3-⌽⋄'==--/\<><'[↑(732451451260688⊤⍨18/8)+L{L?12⍴2}¨⍳9]

• L←+,3-⌽：L是一个返回其参数的函数，后跟3-其参数的反函数
• L{L?12⍴2}¨⍳9：从[1,2]生成9行12个随机值加上它们的反向，然后对这9行进行反向
• 732451451260688⊤⍨18/8：生成列表0 2 4 6 4 2 4 4 4 4 4 4 2 4 _7_ 4 2 0（那是该死的不对称之处）
• +：对于每行，将相应的数字添加到每个值
• ：格式为矩阵
• '==--/\<><'[... ]：对于矩阵中的每个数字，请从该位置的字符串中选择字符

========================
------------------------
///////\\///\\\//\\\\\\\
<<><<><><<<<>>>><><>><>>
\\\\\//\/\\\///\/\\/////
------------------------
/\///\\/\/\/\/\/\//\\\/\
\////////\//\\/\\\\\\\\/
\\/\\\//\///\\\/\\///\//
//\///\\/\\\///\//\\\/\\
/\\\\\\\\/\\//\////////\
\/\\\//\/\/\/\/\/\\///\/
------------------------
/////\\/\///\\\/\//\\\\\
<<><<><><<<<>>>><><>><>>
\\\\\\\//\\\///\\///////
------------------------
========================

1

FryAmTheEggman 2015年

@FryAmTheEggman：该死，没注意到。我现在可能不得不取消整个算法，因为有一条线与其他线不一样。好吧，感谢您告诉我，而不仅仅是拒绝投票。
marinus

@FryAmTheEggman：好，它是固定的（通过<在字符串的末尾添加另一个，然后再次增加第二行，从而将其交换两次）。甚至不必废弃整个东西，尽管现在不再赢了。（也许下次我不应该发布输出：P）
marinus

2

FryAmTheEggman

@ Calvin'sHobbies：解决一件事，打破另一件事。现在，它真的很固定。
marinus

6

# PHP，408，407，402，387，379个字节

<?\$a=str_replace;\$b=str_repeat;function a(\$l,\$a,\$b){while(strlen(\$s)<\$l){\$s.=rand(0,1)?\$a:\$b;}return\$s;}\$c=[\$b('=',12),\$b('-',12),a(12,'/','\\'),a(12,'<','>'),a(12,'/','\\'),\$b('-',12)];while(count(\$c)<9){\$c[]=a(12,'/','\\');}for(\$d=9;\$d--;){\$c[\$d].=strrev(\$a(['/','<','\\','>',1,2],[1,2,'/','<','\\','>'],\$c[\$d]));\$c[]=\$a(['/','\\',1],[1,'/','\\'],\$c[\$d]);}echo implode("
",\$c);

<?php

function randomString(\$length, \$a, \$b) {
\$string = '';
while(strlen(\$string) < \$length) {
\$string .= rand(0, 1) ? \$a : \$b;
}
return \$string;
}

if(isset(\$argv[1])) {
srand(intval(\$argv[1]));
}

\$lines = [
str_repeat('=', 12),
str_repeat('-', 12),
randomString(12, '/', '\\'),
randomString(12, '<', '>'),
randomString(12, '/', '\\'),
str_repeat('-', 12)
];
while(count(\$lines) < 9) {
\$lines[] = randomString(12, '/', '\\');
}

for(\$index = count(\$lines); \$index--;) {
\$lines[\$index] .= strrev(str_replace(['/', '<', '\\', '>', 1, 2], [1, 2, '/', '<', '\\', '>'], \$lines[\$index]));
\$lines[] = str_replace(['/', '\\', 1], [1, '/', '\\'], \$lines[\$index]);
}

echo implode("\n", \$lines) . "\n";

?>

php quilt.php 48937

========================
------------------------
/\\\////\\\/\///\\\\///\
><>><>><<<><><>>><<><<><
/\\\///\//\/\/\\/\\\///\
------------------------
/\\/\\\/\\/\/\//\///\//\
/\\\\/\//\\/\//\\/\////\
\/\\/\/\////\\\\/\/\//\/
/\//\/\/\\\\////\/\/\\/\
\////\/\\//\/\\//\/\\\\/
\//\///\//\/\/\\/\\\/\\/
------------------------
\///\\\/\\/\/\//\///\\\/
><>><>><<<><><>>><<><<><
\///\\\\///\/\\\////\\\/
------------------------
========================

1

Ismael Miguel

Ismael Miguel 2015年

4

1

Ismael Miguel 2015年

@IsmaelMiguel感谢您的帮助。我接受了您的一些建议，但其中一些导致引发通知。

4

# 的JavaScript（ES6）169 195 201

Edit2简化了行的建立，不需要reverseconcat

F=(o='')=>[...z='/\\/-/<\\-='].map((c,i,_,y=[z,'\\/\\-\\>/-='],q=[for(_ of-z+z)Math.random(Q=k=>q.map(v=>r=y[v^!k][i]+r+y[v^k][i],r='')&&r+`
`)*2])=>o=Q()+o+Q(i!=5))&&o

F=(o='')=>[...z='/\\/-/<\\-='].map((c,i,_,y=[z,'\\/\\-\\>/-='],q=[for(_ of-z+z)Math.random(Q=k=>q.map(v=>r=y[v^!k][i]+r+y[v^k][i],r='')&&r+`
`)*2])=>o=Q()+o+Q(i!=5))&&o

Q.innerHTML=F()
<pre id=Q style='font-size:9px;line-height:9px'>

1
-6个字节：删除定义周围的括号z。移动呼叫Q内部的定义Math.random。用'\n'换行符的模板字符串替换。|0不需要整数转换，因为稍后将对值进行异或。
nderscore 2015年

for(_ of-z+z)是什么意思？

@Derek我需要迭代12次，最好的是9个字符的字符串。z不是数字，以便-z是NaN（非数字）的NaN转换成字符串是“南”和3个字符+ 9个字符是12
edc65

4

# 红宝石， 162 155

a='/\\'
b='\\\/'
t=Array.new(9){x=''
12.times{x+=a[rand(2)]}
x+x.reverse.tr(a,b)}
t[0]=?=*24
t[1]=t[5]=?-*24
t[3].tr!a,'<>'
puts t,((t.reverse*'
').tr a,b)

2

@MartinBüttner感谢您的欢迎和提示！字符文字和join同义词为我节省了6个字节。我无法删除括号，x+x.reverse.tr(a,b)因为尽管+优先,。我实际上也没有在字符串中转义斜线-我无法在每个转义符中转义一个反斜线。第二个\是必要的，b因为这样的tr作品，但我现在认识到了第一\a是多余的，所以有一个字节。
ezrast 2015年

3

# 佩斯，57 59 61

J"\<>/"K"\/"L+b_mXdK_Kbjbym+d_XdJ_JmsmOk12[\=\-K-JKK\-KKK

J"\<>/"K"\/"jbu++HGXHK_Km+d_XdJ_JmsmOk12[KKK\-K-JKK\-\=)Y

=G"\<>/"                            : Set G to be the string "\<>/"
K"\/"                               : Set K to be the string "\/"
Jm+d_XdG_GmsmOk12[\=\-K"<>"K\-KKK;  : Set J to be the top half of the carpet
[\=\-K"<>"K\-KKK;  : Make a list of possible chars for each row
msmOk12                   : for each element of that list,
: make a list of 12 randomly chosen characters
: from it, then join them
Jm+d_XdG_G                          : for each element of that list,
: make a new list with the old element,
: and its horizontal reflection
jb+J_mXdK_KJ                        : Print the whole carpet
mXdK_KJ                        : For each element of J make its vertical reflection

Jakube 2015年

Jakube 2015年

@Jakube谢谢！两者都是非常聪明的优化。我非常喜欢lambda如何让您将列表放在最后。
FryAmTheEggman 2015年

2

# J，56 54字节

'=/\<>--></\'{~(-,|.)0,(3(2})8\$5,3#1)+(,1-|.)"1?8 12\$2

'=/\<>--></\'{~(-,|.)0,(3(2})8\$5,3#1)+(,1-|.)"1?8 12\$2
========================
------------------------
///\\\\/\///\\\/\////\\\
><<><><>><>><<><<><><>><
\\/\//\\/\//\\/\//\\/\//
------------------------
\/\/\//////\/\\\\\\/\/\/
/\/\\//\//\\//\\/\\//\/\
//\\\\/////\/\\\\\////\\
\\////\\\\\/\/////\\\\//
\/\//\\/\\//\\//\//\\/\/
/\/\/\\\\\\/\//////\/\/\
------------------------
//\/\\//\/\\//\/\\//\/\\
><<><><>><>><<><<><><>><
\\\////\/\\\///\/\\\\///
------------------------
========================

1个字节，感谢@FUZxxl。

Save one character: Replace 5 1 3 1 5 1 1 1 with (3(2})8\$5,3#1).
FUZxxl

@FUZxxl Great! I tried a ton of alternatives but haven't found this. (CJam got away in score overnight so J will not reach them. :P)
randomra

1

# Python 295287 227 bytes

Not that great but I'll post it anyway:

from random import*
m,d=[],dict(zip("\/<>=-","/\><=-"))
v=lambda w:[d[x]for x in w]
for e in '=-/>/-///':f=[choice([e,d[e]])for x in[0]*12];t=''.join(f+v(f[::-1]));print t;m+=''.join(e=='/'and v(t)or t),
print'\n'.join(m[::-1])

If you want an explanation just ask me.

@Sp3000 Thanks for pointing it out, I fixed it. A shame it came if even longer though...
Def

Here's a bunch of golfs which are too long to fit in a comment. You might be able to get it down even more if you put = and - in d.
Sp3000

@Sp3000 Thanks a lot for all the advice. A lot of it was pretty obvious (spaces, removing reversed) as I'm not the greatest golfer (both code and irl), but I learned some new python too (so again thanks). Removing the if statement by including = and - in the dict turned out to be a very good idea. P.S. would you mind explaining how a recursive quilt is made in so few code (decrypting translate sucks)
Def

1

# Javascript (ES7 Draft) 174168 146

Some inspiration taken from @edc65

Edit: Thanks to edc65 for some ideas to optimize building of rows.

F=(o='')=>[for(i of'555357531')(z=[for(_ of c='==--/\\<>golf')Math.random(r=x=>z.reduce((s,y)=>c[w=i^y^x]+s+c[w^1],'')+`
`)*2],o=r()+o+r(i<7))]&&o

Demonstration: (Firefox only)

F=(o='')=>[for(i of'555357531')(z=[for(_ of c='==--/\\<>golf')Math.random(r=x=>z.reduce((s,y)=>c[w=i^y^x]+s+c[w^1],'')+`
`)*2],o=r()+o+r(i<7))]&&o

document.write('<pre>' + F() + '</pre>');

Commented:

// define function, initialize output to ''
F = (o = '') =>
// loop through character indexes of first 9 lines
[
for (i of '555357531')(
// build array of 12 random 0/1's, initialize character list
z = [
for (_ of c = '==--/\\<>golf')
Math.random(
// define function for generating lines
// if x is true, opposite line is generated
r = x =>
z.reduce(
(s, y) =>
c[w = i ^ y ^ x] + s + c[w ^ 1],
''
) + `\n`
) * 2
],
// build new lines and wrap output in them
// x true in the second line for indexes < 7 (not character '>')
o = r() + o + r(i < 7)
)
]
// return output
&& o

1
See my edit, it's good for your solution too
edc65

@edc65 nice idea! I've implemented something similar now.
nderscore

0

## Pharo 4, 236

|s i f v h|s:='====----/\/\/<><<>'.f:=[:c|s at:(s indexOf:c)+i].v:=#(),'=-/</-///'collect:[:c|h:=(String new:12)collect:[:x|i:=2atRandom.f value:c].i:=1.h,(h reverse collect:f)].i:=3.String cr join:v,(v reverse collect:[:k|k collect:f])

or formatted normally:

|s i f v h|
s:='====----/\/\/<><<>'.
f:=[:c|s at:(s indexOf:c)+i].
v:=#() , '=-/</-///'
collect:[:c|
h:=(String new:12)collect:[:x|i:=2atRandom.f value:c].
i:=1.
h,(h reverse collect:f)].
i:=3.
String cr join:v,(v reverse collect:[:k|k collect:f])

Explanation:

The string s:='====----/\/\/<><<>' together with the block f:=[:c|s at:(s indexOf:c)+i] are here both for tossing the characters and for reversing the patterns...

• For i=1, it performs horizontal reversion (/ <-> \ , < <-> > ).

• For i=3, it performs vertical reversion (/ <-> \)

• For i=1 or 2 atRandom, it toss among / or \, < or >

'=-/</-///' encodes the character type c that will feed the block f for the 9 first lines.

#() , '=-/</-///' is a concatenation trick for transforming the String into an Array and thus collecting into an Array.

The rest is simple concatenation after applying the horizontal/vertical symetry.

========================
------------------------
\\/\/\\\\/\/\/\////\/\//
>>>><><><>><><<><><><<<<
\/\/\//\///\/\\\/\\/\/\/
------------------------
/\//\/\/////\\\\\/\/\\/\
\\//\//\\\/\/\///\\/\\//
////\\/\/\//\\/\/\//\\\\
\\\\//\/\/\\//\/\/\\////
//\\/\\///\/\/\\\//\//\\
\/\\/\/\\\\\/////\/\//\/
------------------------
/\/\/\\/\\\/\///\//\/\/\
>>>><><><>><><<><><><<<<
//\/\////\/\/\/\\\\/\/\\
------------------------
========================

0

## Squeak 4.X, 247

|r s h m n p|s:='==--/\<>'.r:=(1<<108)atRandom.h:=''.m:=0.(16to:0by:-2),(0to:16by:2)do:[:i|n:=3bitAnd:28266>>i.p:=0.(11to:0by:-1),(0to:11)do:[:j|h:=h,(s at:n*2+1+(r-1>>(6*i+j)+(101>>(3-n*4+m+p))bitAnd:1)).j=0and:[p:=1]].i=0and:[m:=2].h:=h,#[13]].h

Or formatted:

|r s h m n p|
s:='==--/\<>'.
r:=(1<<108)atRandom.
h:=''.
m:=0.
(16to:0by:-2),(0to:16by:2) do:[:i|
n:=3 bitAnd: 28266>>i.
p:=0.
(11to:0 by:-1),(0to:11) do:[:j|
"h:=h,(s at:n*2+1+((r-1bitAt:6*i+j+1)bitXor:(101bitAt:3-n*4+m+p))). <- originally"
h:=h,(s at:n*2+1+(r-1>>(6*i+j)+(101>>(3-n*4+m+p))bitAnd:1)).
j=0and:[p:=1]].
i=0and:[m:=2].
h:=h,#[13]].
h

Explanations:

s:='==--/\<>'. obviously encodes the four posible pairs.

r:=(1<<108)atRandom. toss 108 bits (in a LargeInteger) for 9 rows*12 columns (we toss the == and -- unecessarily but performance is not our problem).

h:=''is the string where we will concatenate (Schlemiel painter because a Stream would be too costly in characters).

(16to:0by:-2),(0to:16by:2)do:[:i| is iterating on the rows (*2)

(11to:0by:-1),(0to:11) do:[:j| is iterating on the columns

28266 is a magic number encoding the pair to be used on first 9 lines.
It is the bit pattern 00 01 10 11 10 01 10 10 10, where 00 encodes '==', 01 '--', 10 '/\' and 11 '<>'.

101 is a magic number encoding the horizontal and vertical reversion.
It is the bit pattern 0000 0000 0110 1010, encoding when to reverse (1) or not (0) the first (0) or second (1) character of each pair '==' '--' '/\' and '<>', for the vertical symetry and horizontal symetry.

n:=3 bitAnd: 28266>>i gives the encoding of character pair for row i/2 (0 for '==', 1 for '--', 2 for '/\' and 3 for '<>').

(r-1 bitAt: 6*i+j+1) pick the random bit for row i/2 column j (1 is the rank of lowest bit thus we have a +1, k atRandom toss in interval [1,k] thus we have a -1).

(101 bitAt: 3-n*4+m+p) pick the reversal bit: (3-n)*4 is the offset for the group of 4 bits corresponding to the pair code n, m is the vertical reversion offset (0 for first 9, 2 for last 9 rows), p is the horizontal reversion offset (0 for first 12, 1 for last 12 columns)+1 because low bit rank is 1.

bitXor: performs the reversion (it answer an offset 0 or 1), and s at:2*n+1+bitXor_offset pick the right character in s.

But (A>>a)+(B>>b) bitAnd: 1 costs less bytes than (A bitAt:a+1)bitXor:(B bitAt:b+1)thus the bitXor was rewritten and offset +1 on p is gone...

h,#[13] is an ugly squeakism, we can concatenate a String with a ByteArray (containing code for carriage return).

========================
------------------------
/\/\\\/\//\/\/\\/\///\/\
><>><<>>>><<>><<<<>><<><
////\\////\\//\\\\//\\\\
------------------------
/\\\/\\/\\//\\//\//\///\
\/\\/\//////\\\\\\/\//\/
\\\//\\\////\\\\///\\///
///\\///\\\\////\\\//\\\
/\//\/\\\\\\//////\/\\/\
\///\//\//\\//\\/\\/\\\/
------------------------
\\\\//\\\\//\\////\\////
><>><<>>>><<>><<<<>><<><
\/\///\/\\/\/\//\/\\\/\/
------------------------
========================