画一个随机的ASCII Art被子


31

对于此挑战,ASCII艺术棉被将是一块24个字符宽,18行高的文本块,其中包含=-<>/\水平和垂直对称的类似棉被样式的字符。

被子示例:

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

所有被子都具有相同的形式:

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

请注意,将被子完全垂直或水平对折时,字符的形状完全匹配。不要将此与字符本身匹配起来混淆。例如线3和线16是相同的,它们是垂直镜像。

挑战

编写程序或函数,以打印或返回随机的ASCII艺术被子。

由于许多硬编码的行和对称性,唯一真正的随机性来自第3、4、5、7、8、9行的前12个字符:

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

以字节为单位的最短答案将获胜。Tiebreaker是较早的帖子。

您可以使用伪随机数生成器。(不,你不需要证明所有的12字符的字符串<>/\可以与你的语言的PRNG产生。)

输出可以选择包含尾随换行符,但是除了被子必需外,不包含尾随空格或其他字符。


我们可以将输入作为随机种子吗?
破坏的柠檬

Answers:


15

CJam,61 60 58 55 54 52 51字节

在Sp3000和Optimizer的帮助下有所缩短。

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

在这里测试。

说明

与这些对称ASCII艺术挑战一样,我通常会生成一个象限,然后通过两个适当的镜像操作将其扩展为完整的象限。

对于这个解释,我应该从函数开始,在函数的F某个地方定义它,因为它在三个地方用于三种不同的事物:

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

这期望在堆栈的顶部有一个整数,在堆栈的下面有一个字符串。其目的是反转字符串并交换一些字符,以获得正确的镜像。整数是1或,3并指示是否1应同时交换(3)方括号和斜杠或()仅交换方括号。下面是它的工作原理:

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

在@xnor的帮助下,我们终于赶上了JS!

样本输出:

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

说明

(稍有过时,将在以后更新)

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

第四行扩展的简化示例如下:

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

这将产生><>><><<><

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

整个被子也由内而外建造,因为施工从第9排/第10排开始,向外进行。为此,我们先从一个空列表开始L,然后在通过时将行添加到行的前后

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

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

最后,*_,=是要强制评估是否map发生打印,这只是一种较短的方法print("\n".join(L))

很长一段时间我有功能

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

这需要一个字符串,并转换/\><\/<>分别,但我终于成功地摆脱它:)


规范说,只要产生所有可能的地毯,就可以了。

6

Python 2,300字节

该程序使用join, lambda, replace, sample, import和其他详细功能,因此不会赢得任何高尔夫奖项。

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
不是最短的,但是,嘿,还有7个字节,您就有了一个值得您赞扬的程序:D
卡尔文的爱好2015年

我知道你在那里做了什么。
逻辑骑士

2
自动打高尔夫球?是不是没有人有时间手工打高尔夫球?
拉尔斯·埃伯特

5
你知道我们的黑客。如果我必须执行一次3分钟以上的任务,那么我将花10个小时编写一个程序来使其自动化。我全都是关于效率的;-)
逻辑骑士

6

杀伤人员地雷(53 58)

这不是为对称的,因为我认为这是,很遗憾。修复程序花了我5个字符,现在我已经无法运行了。

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
我之所以+1是因为您发布的算法有趣且新颖,但是我刚刚注意到,<>当您在制作垂直镜像时也使用交换表,因此您的行不是垂直对称的。(感谢发布输出btw,从而弄清了APL是否更容易工作; p)
FryAmTheEggman 2015年

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

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

2
该解决方案非常聪明,您可以保留+1 :)
FryAmTheEggman

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

6

PHP,408407402387,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";

?>

非高尔夫版本有一些好处:您可以将其传递给整数以进行播种,rand()并每次获得相同的被子作为种子:

php quilt.php 48937

例如,这将导致这种美丽的手工编织被子:

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

编辑:原来我的第一个版本没有返回正确的被子。所以我修好了。有趣的是,解决方法甚至更短。


1
您可以为此做很多事情:['/','<','\\','>','a','b']可以替换为['/','<','\\','>',a,b](请注意a和周围缺少的引号b), @$s可以替换为$s,可以存储str_repeat('-',12)并存储str_repeat('=',12)在全局变量/常量中,for($b=8;$b>=0;$b--)可以替换为for($b=9;$b--;)str_repeat并且可以通过以下方式缩短重复功能:它们的名称为全局变量(例如global$R,$V;$R=str_repeat;$V=strrev;$V($R('=',12)):)和换行符(\n)可以由多行字符串代替。
Ismael Miguel

这是一个较短的版本:pastebin.com/2TabUqbA(373字节)。我更改了一些技巧:删除了全局变量,strrev保持不变,删除了1个空格和一些小更改。
Ismael Miguel 2015年

4
我认为您需要第4行和第15行(各<>><><行)相同。
逻辑骑士

1
抱歉,这是一个357字节长的解决方案:pastebin.com/TugNDjjL我忘了减少一些事情。
Ismael Miguel 2015年

@IsmaelMiguel感谢您的帮助。我接受了您的一些建议,但其中一些导致引发通知。
拉尔斯·艾伯特

4

的JavaScript(ES6)169 195 201

编辑 6个字节保存的@nderscore。当心,反引号内的换行符很重要且很重要。

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

运行代码片段进行测试(在Firefox中)

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)是什么意思?
德里克·朕会功夫2015年

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

4

红宝石, 162 155

我喜欢这一行,因为它使我学会了在字符串文字和中使用反斜杠String#tr。否则,代码并不是非常聪明,只是紧凑。

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
欢迎使用编程难题和代码高尔夫球堆栈交换!以下是一些特定于Ruby的技巧:我认为您不需要对/in a和进行转义b。第一个tr也可能没有括号。'='可以编写类似单字符的字符串?=。并.join可以替换为*
马丁·恩德

@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

非常感谢@Jakube提出了这57个字节的版本。

算法与马丁算法非常相似。(修订)解释。

在线尝试

说明:

=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

非常好。不应该扔毛巾。替换"<>"为1个字符即可保存-GK
Jakube 2015年

还有一个通过使用lambda J"\<>/"K"\/"L+b_mXdK_Kbjbym+d_XdJ_JmsmOk12[\=\-K-JKK\-KKK或reduceJ"\<>/"K"\/"jbu++HGXHK_Km+d_XdJ_JmsmOk12[KKK\-K-JKK\-\=)Y
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 295 287 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) 174 168 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).

========================
------------------------
/\/\\\/\//\/\/\\/\///\/\
><>><<>>>><<>><<<<>><<><
////\\////\\//\\\\//\\\\
------------------------
/\\\/\\/\\//\\//\//\///\
\/\\/\//////\\\\\\/\//\/
\\\//\\\////\\\\///\\///
///\\///\\\\////\\\//\\\
/\//\/\\\\\\//////\/\\/\
\///\//\//\\//\\/\\/\\\/
------------------------
\\\\//\\\\//\\////\\////
><>><<>>>><<>><<<<>><<><
\/\///\/\\/\/\//\/\\\/\/
------------------------
========================
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.