首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写符合Stack Exchange的brainfuck解释程序

编写符合Stack Exchange的brainfuck解释程序
EN

Code Golf用户
提问于 2021-11-18 02:26:05
回答 3查看 4.8K关注 0票数 20

您将使用表示一段brainfuck代码和说明的字符串,其中只包含可打印的ASCII字符和换行符( to ~、ASCII 10和32至126)作为输入和输出该代码的说明,格式为符合堆栈Exchange标记。

也就是说,解释必须满足:

  • 每一行都有一个额外的前导空间,如果上面的行有多个代码字符,则为多行,使其保持垂直对齐。
  • 所有命令都在自己的行上,而任何连续的非操作(任何不是<>+-.,[]之一的操作)都在一行上分组。
  • 每个命令的“解释”都对成一列,在代码本身之后有一个空格。
  • 整件事都在一个“代码块”中。
    • 或者整个解释有前面和后面的```行,或者
    • 整个解释有前面的<pre><code>行和尾随的</code></pre>
    • 解释的每一行都有4个前导空格,除了它可能已经有的任何前导空间。
    • 您不必转义输出中可能会破坏标记的部分。

  • 代码块的第一行是BF输入代码。

例如,如果代码是+[]Loop,则说明(没有“解释”)将被格式化为

代码语言:javascript
复制
    +[]Loop
    +      
     [     
      ]    
       Loop

代码语言:javascript
复制
<pre><code>+[]Loop
+      
 [     
  ]    
   Loop
</code></pre>

代码语言:javascript
复制

+[]环

[

]

循环播放

代码语言:javascript
复制

Explanations

每个字符的说明将作为输入提供:

代码语言:javascript
复制
Move the pointer to the right
Move the pointer to the left
Increment the memory cell at the pointer
Decrement the memory cell at the pointer
Output the character signified by the cell at the pointer
Input a character and store it in the cell at the pointer
Jump past the matching ] if the cell at the pointer is 0
Jump back to the matching [ if the cell at the pointer is nonzero
No Operation

输入始终包含9个解释字符串。

答案可以选择如何将每个brainfuck命令与每个解释相关联。

您可以使用数组。

现在,我们可以将这些解释添加到示例代码中,以获得

代码语言:javascript
复制
    +[]Loop
    +       Increment the memory cell at the pointer
     [      Jump past the matching ] if the cell at the pointer is 0
      ]     Jump back to the matching [ if the cell at the pointer is nonzero
       Loop No Operation

(使用4个前导空格,而不是反勾选围栏或HTML标记)。

这是密码-高尔夫,所以以字节为单位的最短代码获胜。

测试案例

输入:

代码语言:javascript
复制
[]<>{}()++--..,,
Move the pointer to the right
Move the pointer to the left
Increment the memory cell at the pointer
Decrement the memory cell at the pointer
Output the character signified by the cell at the pointer
Input a character and store it in the cell at the pointer
Jump past the matching ] if the cell at the pointer is 0
Jump back to the matching [ if the cell at the pointer is nonzero
No Operation

输出:

代码语言:javascript
复制
    []<>{}()++--..,,
    [                Jump past the matching ] if the cell at the pointer is 0
     ]               Jump back to the matching [ if the cell at the pointer is nonzero
      <              Move the pointer to the left
       >             Move the pointer to the right
        {}()         No Operation
            +        Increment the memory cell at the pointer
             +       Increment the memory cell at the pointer
              -      Decrement the memory cell at the pointer
               -     Decrement the memory cell at the pointer
                .    Output the character signified by the cell at the pointer
                 .   Output the character signified by the cell at the pointer
                  ,  Input a character and store it in the cell at the pointer
                   , Input a character and store it in the cell at the pointer

输入:

代码语言:javascript
复制
Brainfuck Power.
Move the pointer to the right
Move the pointer to the left
Increment the memory cell at the pointer
Decrement the memory cell at the pointer
Output the character signified by the cell at the pointer
Input a character and store it in the cell at the pointer
Jump past the matching ] if the cell at the pointer is 0
Jump back to the matching [ if the cell at the pointer is nonzero
No Operation

输出:

代码语言:javascript
复制

Brainfuck Power。

Brainfuck电源无操作

代码语言:javascript
复制
           . Output the character signified by the cell at the pointer
代码语言:javascript
复制

输入:

代码语言:javascript
复制
>++++++[{{<,.>-}}]
Move the pointer to the right
Move the pointer to the left
Increment the memory cell at the pointer
Decrement the memory cell at the pointer
Output the character signified by the cell at the pointer
Input a character and store it in the cell at the pointer
Jump past the matching ] if the cell at the pointer is 0
Jump back to the matching [ if the cell at the pointer is nonzero
No Operation

输出:

代码语言:javascript
复制
<pre><code>>++++++[{{<,.>-}}]
>                  Move the pointer to the right        
 +                 Increment the memory cell at the pointer           
  +                Increment the memory cell at the pointer
   +               Increment the memory cell at the pointer
    +              Increment the memory cell at the pointer
     +             Increment the memory cell at the pointer
      +            Increment the memory cell at the pointer
       [           Jump past the matching ] if the cell at the pointer is 0
        {{         No Operation
          <        Move the pointer to the left
           ,       Input a character and store it in the cell at the pointer
            .      Output the character signified by the cell at the pointer
             >     Move the pointer to the right
              -    Decrement the memory cell at the pointer
               }}  No Operation
                 ] Jump back to the matching [ if the cell at the pointer is nonzero
</code></pre>

输入:

代码语言:javascript
复制
><+-.,NOP[]
Move
Move
Increment
Decrement
STD
STD
While
While
NOP

输出:

代码语言:javascript
复制
    ><+-.,NOP[]
    >           Move
     <          Move
      +         Increment
       -        Decrement
        .       STD
         ,      STD
          NOP   NOP
             [  While
              ] While

输入:

代码语言:javascript
复制
-\
Blah
Blah
Blah
Decrement
Blah
Blah
Blah
Blah
Nothing happens here

输出:

代码语言:javascript
复制
-\
-  Decrement
 \ Nothing happens here
EN

回答 3

Code Golf用户

回答已采纳

发布于 2021-11-18 16:39:02

斯塔克斯,45 字节数

代码语言:javascript
复制
å☺m█Q╗½§ÄN!╢┼♫mù}Jt→v)╢+Θ☼î╔↓bÉü╡⌐CÇ-♠•¿♪FM¥2

运行并调试它

在Fmbalbuena在TNB的挑战之后。也会尝试在Pip中发布一些东西。

尼尔指出了一个错误后找到了一个漂亮的扑救。

解释

代码语言:javascript
复制
LBc4NtPY{"><+-.,[]"XI^i*}/zsFs%Ntcaay%^(x_Iasn@as+4NtPs
LBc4NtPY{"                                              No Operation
          >                                             Move the pointer to the right
           <                                            Move the pointer to the left
            +                                           Increment the memory cell at the pointer
             -                                          Decrement the memory cell at the pointer
              .                                         Output the character signified by the cell at the pointer
               ,                                        Input a character and store it in the cell at the pointer
                [                                       Jump past the matching ] if the cell at the pointer is 0
                 ]                                      Jump back to the matching [ if the cell at the pointer is nonzero
                  "XI^i*}/zsFs%Ntcaay%^(x_Iasn@as       No Operation
                                                 +      Increment the memory cell at the pointer
                                                  4NtPs No Operation
票数 4
EN

Code Golf用户

发布于 2021-11-18 06:03:50

JavaScript (ES2022),141个字节

代码语言:javascript
复制
s=>a=>"    "+s+s.replace(/(?:[^+-.<>[\]]+|.)(?=(.*))/g,(c,e)=>`
`+(c+e.replace(/.|$/g,' ')).padStart(s.length+5)+a.at('><+-.,[]'.indexOf(c)))

在网上试试!

代码语言:javascript
复制
f: (source_code: string) => (explain_array: string[]) => string
票数 6
EN

Code Golf用户

发布于 2021-11-20 03:06:57

果冻,46字节

代码语言:javascript
复制
®Ṭ€z0aḷżṚ⁸ṭṚŻ€4¡o⁶ð“><+-.,[]”iⱮxṠoŒQÄ©ŒQƲ$ịŻ€

在网上试试!

程序在左边,描述列表在右边。我怀疑这可能会很容易地丢失几个字节,但是我现在发布它是为了保存我为后代写过的最可怕的东西之一。

票数 4
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/237408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档