bfcat-将文件编码为Brainf ***程序


18

挑战

编写一个给定字符串的程序或函数,返回一个有效的Brainfuck程序,该程序在作为Brainfuck编译并执行后将返回该字符串。

  1. 假设所有输入均编码为ASCII。

  2. 假设输出的BF程序将在具有无限磁带的环境中执行。

  3. 假定指针从单元格0开始,每个单元格都初始化为零值。

  4. 下面的每个示例代表给定输入的一种可能的正确输出。尤其是,这些示例包括额外的换行符和空格,以帮助人类阅读。解决方案可以自由地以任何方式格式化输出的BF代码。

  5. 磁带是双重无限的。

  6. 解释器提供的所有单元都是8位单元。上溢和下溢围绕着可预测且理智的问题。

例子

空格串

给定输入,您的程序/函数可能返回:

+++++ +++++
+++++ +++++
+++++ +++++
++ .

感叹号

给定输入!,您的程序/函数可能返回:

+++++ +++++
+++++ +++++
+++++ +++++
+++
.

两个字母

给定输入hi,您的程序/函数可能返回:

+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++

+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++

++++
.
+
.

这是,因此字节数最少的代码将获胜。祝好运。


10
请提供更多的测试用例。
Leaky Nun

1
修订版9中确切提到了哪些准则?
user8397947 '16

4
我正在重新打开。我认为Peter的担忧已得到解决,我们在什么可以重复上达成了相当明确的共识,并且这一挑战不符合这些标准。
马丁·恩德

“上溢和下溢以可预测且理智的方式进行换行”-因此127 + 1换行为0,-128-1换行为0,对吗?这是一种可能的可预测和理智的方式。
user253751 '16

1
@immibis正是。
user8397947 '16

Answers:


18

果冻,8 字节

O”+ẋp“.>

在线尝试!

样品运行

对于输入hi,此程序将打印

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++>

(不包含换行符),依次打印hi

怎么运行的

O”+ẋp“.>  Main link. Argument: s (string)

O         Ordinal; convert each character in s into its code point.
 ”+       Yield the character '+'.
   ẋ      Repeat '+' k times, for each k in the code points.
     “.>  Yield the string ".>".
    p     Take the Cartesian product of both results.

您的内存不能工作,我的可以。
Leaky Nun

5
并不是的。我们都在打印之前将整个输出存储在内存中。对于具有足够大磁带的BF口译员,这将比磁带早很多。
丹尼斯

为什么没有.>出现在输出中?

2
@cat,因为笛卡尔乘积p)比附加到每个;€)的短。这些输出程序在打印后和切换之前将单元格加倍。由于我们从不重新访问该单元,因此它不会影响输出。
丹尼斯

@丹尼斯啊,有趣。

55

Brainfuck,55 51字节

,[>+++[>+++++++<-]>[<++>>+++<-]<+<[>.<-]>+++.>>-.,]

在线尝试!

输出示例hi(无换行):

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>

说明

在编写程序时,它会在磁带上移动。周围,[...,]是标准的输入回路。对于每个字符,我们使用四个单元格:

[... x a b c ...]

x我们将输入写入的单元格在哪里。

>+++[>+++++++<-]

这部分使用单元格a21成细胞b通过一个标准的乘法37

>[<++>>+++<-]

现在我们使用21来写入42a63c通过乘以23分别。然后<+<移回单元格,x同时将42变为43(的代码点+)。概括:

[... x 43 21 63 ...]

现在主要输出循环:

[>.<-]

也就是说,在递减的同时,x我们+每次打印一个。

>+++.

完成后,我们+通过添加3to来重复使用单元格.

>>-.

最后,我们移至,将其63减为62>)并输出。下一次迭代将使用此单元格作为x


5
如果我有更多代表,我会为此提供赏金。
user8397947 '16

34

Brainfuck,39 33 32 31字节

-[-[>]<--<--],[[>.<+]>+.--.+<,]

在磁带上放置45的算法取自Esolang的Brainfuck常数

这个答案假设输出程序的解释器具有包装的,有界的单元格;并且,将当前单元格清零(这意味着输出程序在没有输入的情况下运行)。在线尝试!

对于(更长)无条件工作的解决方案,请参阅我的其他答案

测试运行

对于input Code Golf,将生成以下输出。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.,-------------------------------------------------------------------------------------------------------------------------------------------------.,------------------------------------------------------------------------------------------------------------------------------------------------------------.,-----------------------------------------------------------------------------------------------------------------------------------------------------------.,--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.,-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.,-------------------------------------------------------------------------------------------------------------------------------------------------.,----------------------------------------------------------------------------------------------------------------------------------------------------.,----------------------------------------------------------------------------------------------------------------------------------------------------------.,

在线尝试!

怎么运行的

我们首先将整数45(的字符代码-)放入磁带的单元格中。以下代码实现了这一点。

-      Decrement cell 0, setting it to 255.
[      While the cell under the head in non-zero:
  [>]    Advance to the next zero cell.
  <--    Decrement the cell to its left.
  <--    Decrement the next cell to the left.
]

在进入循环之前,磁带看起来像这样。

         v
000 000 255

这三个单元--2-10-是我们将在此程序中使用的唯一单元。

在循环的第一个每次迭代中,最右边的单元是,然后该单元和中间单元递减两次,从而保留以下状态。

     v
000 254 252

在接下来的126次迭代中,初始值-递减中间的单元格,[>]<跳到最右边的单元格,并--<--递减中间和右边的单元格。结果,从中间单元中减去3(模256),从最右边的单元中减去2

由于254÷3(mod 256)=(254 + 256)÷3 = 510÷3 = 170252÷3 = 84,因此最右边的单元在中间的单元之前被清零,从而保持以下状态。

     v
000 132 000

与循环的第一次迭代类似,下一次迭代现在从中间单元格中减去3,从最左边的单元格中减去2,将头放在最左边的单元格上。

 v
254 129 000

随后的迭代(如之前的126次迭代)从最左边的单元格减去3,从最右边的单元格减去2

由于未定义254÷3(mod 256)= 170129÷2(mod 256),因此执行了170次,并保持以下状态。

 v
000 045 000

头部下方的像元为零;循环结束。

现在我们准备生成输出。

,      Read a character from STDIN and put it the leftmost cell.
[        While the leftmost cell is non-zero:
  [        While the leftmost cell is non-zero:
    >.     Print the content of the middle cell ('-').
    <-     Increment the leftmost cell.
  ]      If the leftmost cell held n, the above will print 256 - n minus signs
         which, when executed, will put n in cell 0 of the output program.
  >      Increment the middle cell, setting it to 46 ('.').
  .      Print its content ('.').
  --     Decrement the middle cell twice, setting it to 44 (',').
  .      Print its content (',').
         When executed, since the output program receives no input, the above
         will zero cell 0 of the output program.
  +      Increment the second cell, setting it back to 45 ('-').
  <,     Go back to the leftmost cell and read another character from STDIN.
]      Once EOF is reached, this will put 0 in the leftmost cell, ending the loop.

为什么生成的BF无法为我转换回输入字符串?这是我使用的口译员,它已经用于其他答案。
疯狂的2016年

2
那个解释器有很多设置。对于32字节版本,您需要动态内存输入结尾:\0
丹尼斯

10

Brainfuck,35 13 43字节

++[>+<------],[[>.>+<<-]>+++.->[<.>-]<--<,]

该答案不对输出程序的解释器做任何假设在线尝试!

对于更短的解决方案(仅适用于某些口译员),请参见我的其他答案

测试运行

对于input Code Golf,将生成以下输出。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.----------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++.--------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------------------------------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------------------------------------------------------------------------------------------------+++++++++++++++++++++++++++++++++.---------------------------------

在线尝试!

怎么运行的

我们首先将整数43(的字符代码+)放入磁带的第二个单元中。以下代码实现了这一点。

++         Increment the first cell twice, setting it to 2.
[          While the first cell in non-zero:
  >+         Increment the second cell.
  <------    Decrement the first cell six times.
]

这实质上执行了模块除法2÷6(模数256)。由于(2 + 256)÷6 = 258÷6 = 43,因此结果为43

现在我们准备生成输出。

,      Read a character from STDIN and put it the first cell.
[        While the first cell is non-zero:
  [        While the first cell is non-zero:
    >.     Print the content of the second cell ('+').
    >+     Increment the third cell.
    <<-    Decrement the first cell.
  ]      If the first cell held n, the above prints n plus signs
         and puts n in the third cell.
  >+++   Add three to the second cell, setting it to 46 ('.').
  .      Print its content ('.').
  -      Decrement, setting it to 45 ('-').
  >      Advance to the third cell.
  [      While the third cell is non-zero:
    <.     Print the content of the second cell ('-').
    >-     Decrement the third cell.
  ]      If the first cell held n, the above prints n minus signs,
         thus negating the plus signs and zeroing the cell of the output program.
  <--    Subtract 2 from the second cell, setting it back to 43.
  <,     Go back to the first cell and read another character from STDIN.
]      Once EOF is reached, ',' will put 0 in the first cell, ending the loop.

真是的 你不能打破掉44-is-44模因,快来吧!反正你尝试击败它悲惨地失败了,因为它看起来不剔除,它看起来像有我的屏幕上的一些污垢(PHP,也许?):P

2
任何避免该评论的内容……
Dennis,2016年


4

05AB1E,12 11字节

vyÇ`'+ׄ.>J

讲解

v             # for each char in input string
 yÇ`          # convert to its ascii value
    '+×       # push the char '+' that many times
       „.>J   # push string ".>" and join with the plus signs
              # implicitly print combined string

在线尝试

@Adnan节省了1个字节


'+×而不是F'+}保存一个字节。
阿德南

1
@Adnan:谢谢!我在寻找“重复”命令。
Emigna

4

Java,98个字节

class a{String A(char[]b){String z="";for(char B:b){for(int c=B;c-->0;)z+="+";z+=".>";}return z;}}

Strings只是char[]带有一堆实用程序方法的不可变s,所以让我们使用数组!

取消高尔夫:

class a {
    void A(char[] b) {
        for (char B : b) {
            for (int c = B; c-- > 0;)
                 System.out.print('+');
            System.out.print(".>");
        }
    }
}

138个字节长的等效独立程序:

interface a{static void main(String[]A){for(char b:A[0].toCharArray()){for(int B=b;B-->0;)System.out.print('+');System.out.print(".>");}}}

奖金:

interface a{static void main(String[]A){for(char b:new java.util.Scanner(new java.io.File(A[0])).useDelimiter("\\Z").next().toCharArray()){for(int B=b;B>0;B--)System.out.print('+');System.out.print(".>");}}}

正如标题中所述,此207字节的应用程序实际上将文件编码为BF程序。


2
是我自己,还是非高尔夫球程序缩进的方式看起来很棒?
user8397947'6

在我发布答案之前就意识到,我的答案基本上是您的答案。
疯狂的2016年

2

Vitsy,19个 17字节

I\[&'>.+'i1-\Du]Z

I                     Get the length of the input stack.
 \[            ]      Pop n, repeat this block of code n times.
   &                  Push a new stack and move to it.
    '>.+'             Push the string '+.>' to the stack.
         i            Pop an item from the input stack and push it to the current
                      program stack.
          1-          Subtract one from the top program stack item.
            \D        Duplicate the '+' in the stack that many times.
              u       Merge the current program stack with the previous.
                Z     Output the entire current stack as a string.

请注意,此答案是我曾经使用过I和的几次u。:D

在线尝试!


2

O,13个字节

i{'+n#*".>"}d

说明:

i              Read standard input into a string and push it onto the stack.
 {         }d  For each character in the string...
  '+           Push "+".
    n#         Convert the character (n) to its ASCII code.
      *        Multiply the "+" by the character code to get the number of +'s needed.
       ".>"    Push the string ".>".
               In O, the stack is printed after execution, so this prints out the result.

2

K6,16个字节

,/{|">.",x#"+"}'

用法

,/{|">.",x#"+"}'"some string"

说明

  {           }'  For each character in the string...
         x#"+"    Repeat "+" as many times as the character's ASCII code.
        ,         Combine that with...
    ">."          The code to print it and move on.
   |              But we put the string together backwards...so now it gets reversed.
,/                Combine all the substrings into one BIG string.

说明plz:D
艾迪生·克伦普

@VTCAKAVSMoACE完成!:)
kirbyfan64sos

2

Python 3,43个字节

lambda s:''.join('+'*ord(c)+'.>'for c in s)

Python放置了与每个字符的ASCII码等效的多个加号,然后.>打印并移至下一个单元格。头脑操增加到正确的值,打印并继续到下一个单元格。

输出hi(用换行符表示):

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>

该程序的输出:

hi

2

Perl,25个字节

s/./"+"x ord($&).".>"/eg

用法

echo -n hi | perl -pe 's/./"+"x ord($&).".>"/eg'

说明

使用正则表达式替换操作将标准输入上给定的每一行中的每个字符替换为+根据该字符的序数计算得出的数量,然后输出.>以打印并前进到下一个字符。

使用perl -p标志自动读取输入并打印结果,从而在字节数上增加1。


2

Java,91个字节

String b(char[] f){String k="";for(char b:f){for(int u=b;u>0;u--)k+="+";k+=".>";}return k;}

dorukayhan击败我的道具:)


您不能只删除样板并更改所有变量名称,然后声称它是新答案。
Leaky Nun

@LeakyNun我没有,但是我可以。
疯狂

该答案与您链接的答案有何不同?
Leaky Nun

@LeakyNun Downvote并继续前进
疯狂

2

C,72 64 60字节

main(c){for(;c=~getchar();)for(;printf(~c++?"+":".>")^2;);}

非高尔夫版本:

main( c )
{
    for ( ; c = ~getchar( ); )
        for ( ; printf( ~c++ ? "+" : ".>" ) ^ 2; );
}

编译和测试:
gcc -o bfcat bfcatgolf.c && cat 1.txt | ./bfcat > o.txt && beef o.txt

结果


为什么c的按位getchar求逆,尤其是当您再次求反时?

@cat c = ~getchar( )在上的计算结果为0 EOFc = ~getchar( )~c++仅仅是短于~( c = getchar( ) )c--
Jacajack

2

CJam,12个字节

将每个字符转换为其ASCII值,并在打印之前将当前单元格增加该次数。由于我们有无限的磁带,我们可以在处理完每个字符后向右移动。

q{i'+*'.'>}%

在线尝试!


2

Lua,67 66 61字节

只需遍历参数中的每个字符,并用n +s 为每个字符打印一行,然后在ASCII表中此字符的值.>在哪里n

按照注释中的@LeakyNun使用gmatch,以比gsub解决方案节省1个字节

for c in(...):gmatch"."do print(("+"):rep(c:byte())..".>")end

使用gsub的旧解决方案

(...):gsub(".",function(c)print(c.rep("+",c:byte())..".>")end)

旧的67

(...):gsub(".",function(c)print(string.rep("+",c:byte())..".>")end)

要运行它,只需将其另存为文件(golf.lua例如)并使用即可运行lua golf.lua "hi"。对于hi,它应该输出

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>

我该如何使用?只需将其粘贴在REPL给出attempt to index a nil value,并f = ...给出了一致,并function f ... end给出了unexpected symbol near ':'

@cat只需将其保存在文件中并以身份运行lua file.lua "argument",我将更新该帖子以提供说明。
Katenkyo

那怎么工作?...argv的标识符是什么?

1
@cat ...包含表中arg未打包的值。这意味着它将始终与之对应,arg[1]除非您在函数调用中将其用作最后一个参数,否则它将扩展。
Katenkyo

n=...for n:gmatch"."do print(("+"):rep(c:byte())..".>")endfor 48 bytes
Leaky Nun

1

J,28个字节

[:;[:('.>',~'+'#~])"0[:]3&u:

很简单。3&u:将字符转换为字符代码。其余的仅重复'+'该次数,然后.>在每行的末尾与并置,然后;将结果展平。

一些结果

   bf =: [:;[:('>.',~'+'#~])"0[:]3&u:
   bf 'hi'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>.
   bf ' !'
++++++++++++++++++++++++++++++++>. +++++++++++++++++++++++++++++++++>.
   bf ' ~'
++++++++++++++++++++++++++++++++>.
                 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>.


1

其实13个字节

O`'+*".>"@`MΣ

在线尝试!

此处使用的策略与许多其他解决方案中的策略相同-对于每个字符,输出足够的+s以将零初始化的单元格递增到适当的ASCII序数,并用输出.,然后用移至下一个单元>

说明:

O`'+*".>"@`MΣ
O              list of ordinals (technically this is Unicode code points, but code points 0-127 are the same for Unicode and ASCII)
 `'+*".>"@`M   for each ordinal:
  '+*            push a string containing that many +s
     ".>"@       push ".>" and swap (putting it before the +s)
            Σ  concatenate all the strings (if "[", "]", and "," weren't meaningful characters in BF, this technically wouldn't be necessary)

1

Mouse-2002,27个字节

(?'l:l.^(l.^"+"l.1-l:)".>")

从理论上讲并根据该语言的文档,这是可行的,但是Mouse的解释器的参考实现似乎有一个错误,其中字符串输入附加了a ',因此对于a此输出

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++.>

依次输出a'。那可能也可能不行,所以这是一个39字节 长的字节'永远不会输出,因此可能无效。

(?'l:l.39=l.0=+0=^(l.0>^"+"l.1-l:)".>")

在参考隐含中给出正确的输出。只要没有's :)

解释:

(              ~ while (true) {
  ?' l:        ~   l = getchar() 
  l. ^         ~   if (! (l) ) { break }
  (            ~   while (true) {
    l. ^       ~     if (! (l) ) { break }
    "+"        ~     puts("+")
    l. 1 - l:  ~     l-- // l = l - 1
  )            ~   }
  ".>"         ~   puts(".>")
)              ~ }

1

因子58字节

[ [ [ 43 ] ""replicate-as ".>"append ] { } map-as ""join ]

像这样工作:

[ 
  [ 
    [ 43 ] "" replicate-as  ! ``+'' * code point, clone-like a string ""
    ".>" append             ! append ".>"
  ] 
  { } map-as                ! for each char in string and clone-like an array { }
  "" join                   ! join array of str into str by ""
]

由于Factor附带了Brainfuck解释器,因此测试很简单。

bfcat.factor

USING: sequences ;
IN: bfcat

: string>brainfuck ( s -- bf )
  [ [ 43 ] "" replicate-as ".>" append ]
  { } map-as "" join ;

bfcat-tests.factor

USING: tools.test bfcat brainfuck ;
IN: bfcat.tests

{ "a"  } [ "a"  string>brainfuck get-brainfuck ] unit-test
{ "ab" } [ "ab" string>brainfuck get-brainfuck ] unit-test
{ "+++++++++++++" } [ "+++++++++++++" string>brainfuck get-brainfuck ] unit-test
{ "Code Golf" } [ "Code Golf" string>brainfuck get-brainfuck ] unit-test

输出

Unit Test: { { "a" } [ "a" string>brainfuck get-brainfuck ] }
Unit Test: { { "ab" } [ "ab" string>brainfuck get-brainfuck ] }
Unit Test: {
    { "+++++++++++++" }
    [ "+++++++++++++" string>brainfuck get-brainfuck ]
}
Unit Test: {
    { "Code Golf" }
    [ "Code Golf" string>brainfuck get-brainfuck ]
}

好极了!他们都通过了。


1

红宝石, 40 38字节

gets.chop.each_byte{|o|puts"+"*o+".>"}

我不知道红宝石,但我知道您可以使用puts代替print,因为输出的格式并不重要,只要它是有效的brainfuck并且brainfuck不在乎其他字符即可
cat

@cat哦,我不知道bf会忽略其他字符,谢谢!
CocoaBean

1

Sidef,38个字节

嘿,和露比一样长!只是Sidef不是Ruby:D

read().bytes.each{|c|say"+"*c;say".>"}

阅读一些字符,然后为每个字节执行该操作。


1

GNU Bash,100 85字节

while IFS= read -rn1 c;do printf '+%.0s' $(seq 1 $(printf %d \'$c));echo '.>';done<$1

感谢@cat为我节省了15个字节!

后漫步

  1. 假设输入字符串在作为第一个参数传递的文件中按原样表示。
  2. 用法: bash bfcat.sh <path to file containing string>
  3. 用法(使用命名管道): bash bfcat.sh <(echo -n '<string>')

不打高尔夫球

while IFS= read -r -n1 c # Read file byte by byte [1]
do
    n=$(printf "%d" \'"$c") # `ord` of a char in bash [2]
    printf '+%.0s' $(seq 1 $n) # emit character $n times [3]
    echo '.>' # Emit output and shift command for bf. I love infinite memory.
done <"$1"

非高尔夫版本中的参考

  1. 逐字节读取文件

  2. ord bash中的一个字符

  3. 发射字符$ n次


1
我已经改善了答案中的某些格式和内容,并从链接中删除了多余的信息。如果您不喜欢它,可以随时撤消。另外,它是GNU Bash,而不是GNU / Linux之类的GNU / Bash。:)

1
高尔夫技巧(我没有bash的高尔夫球手)read -rn1,获得摆脱后的空间;,在摆脱了空间的done <"$1"一共有9个字节保存

1
@cat看起来很棒!我真的需要养成
edit-

1

ES6,119个 115字节

f=s=>{a='';for(i in[...s]){b=s[c='charCodeAt'](i)-(s[c](i-1)|0);a+=(b>0?'+'.repeat(b):'-'.repeat(-b))+'.'}return a}

由于@Leibrug,节省了4个字节


1
您可以分配charCodeAt给某个var(比如说c),并像这样使用:s[c](i)缩短1个字节,还删除一些字符(我发现3:在空格之前[...s],将逻辑OR替换为按位1,在前面加上分号return)。
Leibrug '16

1

Sesos(非竞争)

十六进制转储:

0000000: 28cbf6 02e83d 655bae 243901                       (....=e[.$9.

Size   : 12 byte(s)

在线尝试!

组装工

add 43,fwd 1,get
jmp
  jmp,sub 1,rwd 1,put,fwd 1,jnz
  add 46,put,add 16,put
  get

0

Ruby,26个字节

gsub(/./){?+*$&.ord+'.>'}

p命令行选项+ 1个字节。例如,获取ABC xyz可以运行的Brainfuck代码

$ ruby -p -e 'gsub(/./){?+*$&.ord+".>"}' <<< "ABC xyz"

并得到

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>

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.