将RoboCritters转换为Brainf ***


15

RoboCritters(词源学)是一种崭新的深奥编程语言(不必费心去寻找它,我只是发明了它)。它是Brainfuck(BF)变体,比通常的运算符替换方案稍微复杂一些。RoboCritters中的每个程序都是一个矩形的文本网格,该文本网格仅包含七个字符. []/\|以及用于换行的换行符。

示例RoboCritters程序:

|\/||./|[]||
[..][][]   |
|/\|[..][..]
[..]    \/\/

要将RoboCritters程序转换为BF,请按正常阅读顺序(从左到右,然后从上到下)查看网格中的每个非换行符|\/||./|[]||[..][][] ||/\|[..][..][..] \/\/

如果从当前角色向右和向下延伸的网格的4×2部分与下面列出的八个机器人生物之一完全匹配,请附加相应的BF命令><+-.,[])到(最初为空)BF程序。

如果4×2网格部分任何机器人生物都不匹配超出范围则不向BF程序添加任何内容。

  1. Joybot,>命令:

    [..]
    \][/
    
  2. Calmbot,<命令:

    [..]
    /][\
    
  3. Squidbot,+命令:

    [..]
    //\\
    
  4. Spiderbot, -命令:

    [..]
    ||||
    
  5. Bunnybot,.命令:

    [][]
    [..]
    
  6. Toothbot,,命令:

    [..]
    |/\|
    
  7. Foxbot,[命令:

    |\/|
    [..]
    
  8. Batbot,]命令:

    [..]
    \/\/
    

因此,请阅读示例程序

|\/||./|[]||
[..][][]   |
|/\|[..][..]
[..]    \/\/

我们可以看到我们首先遇到了Foxbot(在第1列第1行),然后是Toothbot(c1,r2),然后是Bunnybot(c5,r2),最后是Batbot(c9,r3)。这对应于BF程序[,.]

请注意Foxbot和Toothbot重叠。这是故意的;重叠的机器人生物没有任何不同的解释

挑战

编写最短的程序,该程序可以接收RoboCritters程序并输出等效的BF。您无需运行BF或检查其是否有效,只需将RoboCritters代码转换为BF代码即可。

细节

  • 所有输入的RoboCritters程序都是有效的,也就是说,它们将是一个完全矩形的文本块,仅包含七个字符. []/\|以及换行符。换行符可以采用任何方便的通用表示形式。您可以选择假定程序只有一个尾随换行符。

  • 您必须支持小于4×2的RoboCritters程序,包括0×0空(或单个换行符)程序。这些都对应于空的BF程序(空字符串)。

  • 输出的BF程序应该是仅包含八个BF命令字符的单行字符串><+-.,[]。可能有一个结尾的换行符。

  • 以任何常用方式(stdin /文本文件/命令行)进行输入,然后输出到stdout或您语言的最接近替代品。

  • 您可以编写一个将RoboCritters程序作为字符串并打印或返回BF程序字符串的函数,而不是程序。

例子

  1. 输入:(上面示例的变体)

    |\/|[][]
    [..][][]
    |/\|[..]
        \/\/
    

    输出: [,.]

  2. 输入:(测试所有机器人生物)

    [..][[[[[..]]. ]|\/|
    \][/[..]//\\[..][..]
    [..]/][\[][]|/\|[..]
    ||||/\| [..]|  |\/\/
    

    输出: >+[<,-.]

  3. 输入:

    [..] [..] [..] [..] [..] [..] [..] [..] |\/| [..] [..] [..] [..] [..] |\/| [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] [..] |\/| [..] [..] [..] [..] [..] [..] [..] [][] [..] [..] [..] [..] [][] [..] [..] [..] [..] [..] [..] [..] [][] [][] [..] [..] [..] [][] [..] [..] [][] [..] [..] [][] [..] [][] [..] [..] [..] [][] [..] [..] [..] [..] [..] [..] [][] [..] [..] [..] [..] [..] [..] [..] [..] [][] [..] [..] [..] [][] [..] [..] [..] [][]
    //\\ //\\ //\\ //\\ //\\ //\\ //\\ //\\ [..] \][/ //\\ //\\ //\\ //\\ [..] \][/ //\\ //\\ \][/ //\\ //\\ //\\ \][/ //\\ //\\ //\\ \][/ //\\ /][\ /][\ /][\ /][\ |||| \/\/ \][/ //\\ \][/ //\\ \][/ |||| \][/ \][/ //\\ [..] /][\ \/\/ /][\ |||| \/\/ \][/ \][/ [..] \][/ |||| |||| |||| [..] //\\ //\\ //\\ //\\ //\\ //\\ //\\ [..] [..] //\\ //\\ //\\ [..] \][/ \][/ [..] /][\ |||| [..] /][\ [..] //\\ //\\ //\\ [..] |||| |||| |||| |||| |||| |||| [..] |||| |||| |||| |||| |||| |||| |||| |||| [..] \][/ \][/ //\\ [..] \][/ //\\ //\\ [..]
    

    输出:(BF Hello World程序

    ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
    
  4. 输入:(不存在机器人生物)

    /\\\[].
    ]..[..]
    \\//||\
    

    输出:(空字符串)

计分

以字节为单位的最短提交获胜。(方便的字节计数器。)决胜局是最高投票的职位。

Answers:


9

CJam,86 85字节

qN/S4*f+_W<\1>]z:z4few:~"¨Ý³5&ágûò{wÉ](Ý"296b6b"|[\.]/"f=2/4/"><+-.,[]":Ler{L&},

在这里测试。

说明

qN/    e# Read input and split into lines.
S4*f+  e# Append four spaces to each line. This has two purposes. a) We can later join all
       e# the lines together without worrying about critters appearing across the line
       e# edges because no critter contains spaces. b) It works around a bug in CJam where
       e# the command `ew` crashes when the substring length is longer than the string.
_W<    e# Copy the lines and discard the last one.
\1>    e# Swap with the other copy and discard the first one.
]z     e# Wrap both in an array and zip them up. Now we've got an array which contains
       e# all consecutive pairs of lines.
:z     e# Zip up each of those pairs, such it becomes an array of two-character strings.
       e# We can now find the critters as 4-element subarrays in each of those arrays.
4few   e# Turn each of those arrays into a list of its (overlapping) 4-element subarrays.
:~     e# Flatten those lists, such that we get one huge array of all 4x2 blocks, in order.
"gibberish"296b6b
       e# This is an encoded form of the critters. The above produces a string of the
       e# critters in column-major order, all joined together, where the characters are
       e# represented by numbers 0 to 5.
"|[\.]/"f=
       e# Turn each number into the correct character.
2/4/   e# Split first into columns, then into critters. Now all we need to do is find these
       e# the elements of this array in the processed input.
"><+-.,[]":L
       e# A string of all BF characters in the same order as the critters. Store this in L.
er     e# Do an element-wise replacement. This will leave non-critter blocks untouched.
{L&},  e# Filter the result. The elements are now either characters, or still full 4x2
       e# blocks. We take the set intersection with the array (string) of BF characters.
       e# If the current element is itself a character, it will be coerced into an array
       e# containing that character, such that we get a non-empty intersection. If the
       e# current element is a block instead, if contains arrays itself, so the set
       e# intersection will always be empty.

       e# The resulting array of characters is the desired BF program and will be printed
       e# automatically at the end of the program.

小动物用这个脚本编码。我发现使用以下较幼稚的Mathematica脚本进行编码的基础296(仍在运行以寻找更好的基础):

b = 256;
While[True,
  If[
    FreeQ[
      d = IntegerDigits[15177740418102340299431215985689972594497307279709, b], 
      x_ /; x > 255
    ], 
    Print@{b, Length@d}
  ];
  b += 1;
]

3

JavaScript的ES6,209个 198 192字节

f=c=>{s=''
b=o=>c.substr(o,4)||1
for(i in c)s+=!~(d='1\\][/1/][\\1//\\\\1||||[][]11|/\\||\\/|11\\/\\/'.replace(/1/g,'[..]').indexOf(b(i)+b(++i+c.search(`
`))))|d%8?'':'><+-.,[]'[d/8]
return s}

下面的堆栈摘录包含未分类的代码,您可以在任何浏览器中轻松运行它们。

var f = function(c) {
  var s = '';
  var b = function(o) {
    // If it is looking on the last line, this will return an empty string
    // the second time, which could cause an inaccurate match.
    // `||1` makes it return 1 instead of '', which won't match.
    return c.substr(o, 4) || 1;
  }
  for (var i in c) {
    r = b(i) + b(++i + c.search('\n'));
    d = '1\\][/1/][\\1//\\\\1||||[][]11|/\\||\\/|11\\/\\/'.replace(/1/g, '[..]').indexOf(r);
    s += !~d || d % 8 ? '' : '><+-.,[]' [d / 8];
  }
  return s;
}

// GUI code below
function run(){document.getElementById('s').innerHTML=f(document.getElementById('t').value);};document.getElementById('run').onclick=run;run()
<textarea id="t" cols="30" rows="10">
[..][[[[[..]]. ]|\/|
\][/[..]//\\[..][..]
[..]/][\[][]|/\|[..]
||||/\| [..]|  |\/\/</textarea><br /><button id="run">Run</button><br /><code id="s"></code>

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.