Brainf ** k一元及后退


15

在受限来源和其他此类挑战中非常有用的语言是 一元,其中程序与只有一个字符写在一个brainfuck衍生物。您的工作是编写一个程序,将程序从Brainfuck转换为一元,并编写一个相反的程序,两个程序都使用相同的语言。您的分数将是两个程序的长度之和。

您如何从Brainfuck转换为一元?

  • 首先根据此表将您的Brainfuck代码转换为二进制代码:

换算表

  • 现在按代码顺序将代码连接成一个巨大的二进制数。
  • 前置一个 1字符串,以确保一个唯一的二进制数。
  • 使用任何字符将二进制数转换为一元数。
  • 例如:+.将为000000000000000000000000000000000000000000000000000000000000000000000000000000000000(84个零)。

Brainfuck->一元规格

  • 由于生成的程序不可能太大,因此,请不要打印实际的程序,而仅打印生成的程序的长度。
  • 通过stdin,arg函数等将Brainfuck程序作为字符串,并输出长度。
  • 该程序将始终有效,并且其中仅包含这8个字符。

一元-> Brainfuck规格

  • 您将必须实现与上述算法相反的方法。
  • 再次由于存在巨大的问题,输入将是一个描述Unary代码长度的数字。
  • 一如既往的I / O规则。
  • 该程序将始终有效,并且其中仅包含这8个字符。

测试用例

  • 你好世界- ++++++[>++++++++++++<-]>.>++++++++++[>++++++++++<-]>+.+++++++..+++.>++++[>+++++++++++<-]>.<+++[>----<-]>.<<<<<+++[>+++++<-]>.>>.+++.------.--------.>>+.=239234107117088762456728667968602154633390994619022073954825877681363348343524058579165785448174718768772358485472231582844556848101441556
  • 斐波那契- ++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>++++++++++++++++>>+<<[>>>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[<+>-]>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<.>.>>[>>+<<-]>[>+<<+>-]>[<+>-]<<<-]<<++...=13067995222095367150854793937817629722033205198624522624687536186118993888926522550140580142585590431635487113180955099384652678100247403485397450658564826143160529351955621991895221530908461364045400531236124980271740502887704217664044858614821622360156740992393765239123681327824577149595724956207165558106099868913919959549896553103116795519592552089266360725543244154867904980260

这是代码高尔夫球,因此最低得分(以字节为单位)将获胜!

有人在Unary寻找解决方案吗?; P


7
一个更合适的标题可能是“ Brainfuck to Golunar and Back”
Sp3000

@ Sp3000是个好点,但是我认为大多数人还没有真正听说过这个(包括我自己)。
Maltysen

@Maltysen我认为您的测试用例不正确。例如,二进制数的第一个数字的前两位为10101010101010,则应为1010010010010
isaacg 2015年

@isaacg对不起,将他们从使用其他翻译机制的网站上删除,将可以解决。
Maltysen

1
我们可以将它们转换为一个不完全相同但完全相同的程序吗?
jimmy23013

Answers:


12

Pyth,17 + 17 = 34个字节

BF->一元,17字节

i+1xL"><+-.,[]"z8

一元-> BF,17个字节

s@L"><+-.,[]"tjQ8

7

Brainfuck563335318316296 + 529373366336 = 632字节

由于显然缺少相关语言的解决方案,因此这里是Brainfuck和Golunar的解决方案。我无法用一元形式发布答案,因为那将需要比宇宙中存在的原子多出几千亿倍的内存^^

“后退”例程不会检查Golunar /一元代码是否有效。如果位计数mod 3!= 1,则它将陷入打印很多“>”的无穷循环。

感谢Nitrodon帮助我将不足300个字符的bf转换为一元代码

一元的脑袋

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

在线尝试!

然后回来

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

在线尝试!

Golunar /一元位数,509 303 288 286 268 + 478 337 331 304 = 572个,字节

一元的脑袋

2845581296974449674357817038179762273623136917867627972159702240227366875240878616687779429553529795902322625321040063298921498529640547483869509829184440577052825434462245755576011912505085065586076069824710351537537205287083477698633592357950165322060367940923703887

然后回来

3775574485023133646619269732540391678811443648964274086227256847322821618228135493733703990523803451383315165001915937932498966394771849173263120467073642011339214182483748816052890450078070151307011943625602391238338941712116968736593594971620990210178757280976709140113340322124688909388916094040773207

源代码

一元的脑袋

[
unary:
><+-.,[]
01234567

62 > 62
60 < -2
45 - 15
43 + 2
44 , 1
46 . 2
91 [ 45
93 ] 2

tape (while reading input): Left tape end marker/LTE, [binary data], input, 15, (15 multiplicator)
tape (while base conversion): LTE, [binary data], Value Start/VS, [decimal digits]

decimal digits: digit used/DU, value
]

-                       set LTE
>+                      set leading 1
>>>>,[                  while input
  >+++[>+++++<-]        set 15 (multiplicator)
  >[<+<---->>-]         set 15 and decrease input by 60

                    check for greater than
                        set current bits = 000 (greater than)
  <<--[                 if input != 62 try next char

                    check for less than
  <+>                   set current bits = 001 (less than)
  ++[                   if input != 60 try next char

                    check for minus
  <<+>>                 set current bits = 011 (minus)
  >[<+>-]<[             if input != 45 try next char

                    check for plus
  <->                   set current bits = 010 (plus)
  ++[                   if input != 43 try next char

                    check for comma
  <<<+>->+>             set current bits = 101 (comma)
  -[                    if input != 44 try next char

                    check for dot
  <->                   set current bits = 100 (dot)
  --[                   if input != 46 try next char

                    check for left bracket
  <<+>>                set current bits = 110 (left bracket)
  >+++++++++[<----->-]<[   if input != 91 go to next char


                    use right bracket
  <+>                   set current bits = 111 (right bracket)
  --                    decrease input by 2 / should be 0 now

  ]]]]]]]               close all ifs
  >[-]>>                delete 15 if still existant
  ,                     input next character
]
<<<<+[<+]>[>]           add one to each bit and delete LTE (for shorter search routine)

                    Start of binary to decimal routine

-                       set value start marker (VS)
>+                      set digit used marker (DU)
[<]                     go to LTE

                    binary to decimal loop: use "double and add algorithm" to calculate the digits of the decimal value
>+[                     if not on VS then
  [->+]-                restore current bit value and go to VS
  >                     go to first DU
  [                     digit doubling loop
    ->                  remove DU and go to corresponding digit
    [
      <++>-             decrement current value and add 2 to temp value four times
      [
        <++>-
        [
          <++>-
          [
            <++>-
            [                   if value was greater than 4 then
              <---- ----        subtract 8 from temp
              >>[-]++           set next digit temp = 2 (DU plus 1)
              <-                decrement current digit
              [<++>-]           set temp = remaining value * 2
            ]
          ]
        ]
      ]
    ]
    <[>+<-]             set current digit = temp
    +                   set DU
    >>                  go to next digit
  ]                     end of digit doubling loop
  <<[<<]>[<]>           go to current bit
  -[                    if bit is 2 (used plus 1)
    [->+]-              delete bit and go to VS
    >>+                 increment least significant digit
    <[<<]>[<]           go to current bit
  ]
  >+                    if not on VS then repeat  
]                   end of binary to decimal loop

>[>>]<                  go to most significant digit
<[                  printing loop: for each DU print corresponding value
  +++++++[>++++++<-]>.  add 48 to value (ASCII 0) and print
  <<<                   go to next DU
]

然后回来

[
tape: left tape end marker/LTE(-1), [digits], digit end marker/DE(0), carry, SB(-1), [binary data], 60, 15
digits: digit used marker/DU(1), digit, remainder, solution, 0
        else]                                    [exit else, exit if
binary data: value (, else, exit if, exit else)
]

                    input decimal value
->>                     set LTE
,[                      while input
  <++++++[>--------<-]  decrease input by 48
  +                     set DU
  >>>>> >,              input next digit
]
>->-                    set start of bits (SB) and first CCB
<<<+[-<+]>              delete LTE and go to first DU

                    division loop: calculate the remainders of the input divided by 2 repeatedly to get the (inverted) bits
[
                        divide each digit by 2
  [                     for each DU
    -                   delete DU (for exit if)
    >                   go to digit
    [->+<               dec digit / set remainder
      [->->+>]          if digit gt 0: dec digit / del remainder / inc solution / goto 0
                        pointer: (value(0) remainder is set) or (0 solution gt 1)
      <[<<<]            go to DU
      >                 go to digit
    ]
    <+                  set DU
    >>>[-<<+>>]         move solution to digit
    <[                  if remainder
      >>>>              go to next digit
      +++++ +++++       add 10 to digit/carry
      <<<<-             go back and delete remainder
    ]
    >>>                 go to next DU
  ]

                    append new bit
  >>>+[->+]             go to and delete CCB
  >-                    set new CCB
  <+[-<+]-<             go to carry
  [                     if carry
    >>+[->+]-<+         set last bit
    +[-<+]-<[-]         go to and clear carry
  ]

                    check if first digit became 0 / neccessary to check if value has been completely processed
  < <<<<<[<<<<<]>>>>>   go to first DU
  >[                    if digit gt 0
    <<                  go to exit if
  ]<[                   else
    -                   delete DU
    >>>                 go to exit else of next digit
  ]
  >>                    go to DU / DE if all digits processed
]                   end of division loop

                    decode binary values
>>>+[->+]               go to and delete CCB (after last bit)
<-                      delete leading 1
<                       go to first bit


                    Start of bit decoder
[
unary:
><+-.,[]
01234567

62 > 62
60 < -2
43 + -17
45 - 2
46 . 1
44 , -2
91 [ 47
93 ] 2

tape: start of bytes marker/SB(-1), [binary data], 60(print char/PC), 15
]

+[-                     while not SB

                    Set least significant to octal value of three bits
  [<++>-]               if first bit set add 2 to second bit
  <[<++>-]              for each second bit add 2 to third bit

  >+++[>+++++<-]        multiplier 15
  >[<+<++++>>-]         setup table 60 15

                    run through the 8 possibilities

                    0 greater than
  <<++                  set PC = 62 (greater than)
  <[                    if value gt 0 go to next char

                    1 less than
  >--                   set PC = 60 (less than)
  <-[                   if value gt 1 go to next char

                    2 plus
  >>[<->-]<--           set PC = 43 (plus)
  <-[                   if value gt 1 go to next char

                    3 minus
  >++                   set PC = 45 (minus)
  <-[                   if value gt 1 go to next char

                    4 dot
  >+                    set PC = 46 (dot)
  <-[                   if value gt 1 go to next char

                    5 comma
  >--                   set PC = 44 (comma)
  <-[                   if value gt 1 go to next char

                    6 left bracket
  >+[>+<-]>[<++>-]<+    set PC = 91 (left bracket) (inc (45) / double (90) / inc (91))
  <-[                   if value gt 1 go to next char

                    7 right bracket
  >++                   set PC = 93 (right bracket)
  <-                    decrease value the last time to exit if

  ]]]]]]]               close all ifs
  >.[-]                 print char and clear PC
  >[-]                  clear 15 if still existant

  <<<                   go to next bits
  +                     repeat if not SB
]

1
转换为一进制时,您可以直接从输入单元格中减去60,而不是先将其放在自己的单元格中,从而节省16个字节。通过不立即创建45个字节可以进一步节省4个字节(因此进一步压缩了磁带布局)。此外,按01325467的顺序检查输入字节也比较容易
。– Nitrodon

我的意思是在将15加到输入单元格时创建45。
Nitrodon

6

Python 2,80 79 63 55 + 86 64 = 119字节

感谢Sp3000的大量建议,节省了大量字节。

Brainfuck to Unary,78 77 61 53 + 2 = 55字节

添加了两个字节以解决输入中的“ s”问题。

print int(`[1]+map("><+-.,[]".find,input())`[1::3],8)

Unary to Brainfuck,86 64字节

print''.join("><+-.,[]"[int(i)]for i in oct(input())[2:]if'L'>i)

在此处检查ideone。



3

CJam,35个字节

Brainfuck一元,17字节

1r"><+-.,[]"f#+8b

在线尝试。

怎么运行的

 r                e# Read a whitespace-separated token from STDIN.
            f     e# For each character in the token:
  "><+-.,[]" #    e#     Find its index in this string.
1             +   e# Prepend a 1 to the results.
               8b e# Convert to integer, using base 8 conversion.

一元到Brainfuck,18个字节

ri8b"><+-.,[]"f=1>

在线尝试。

怎么运行的

r                  e# Read a whitespace separated token from STDIN.
 i                 e# Interpret as integer.
  8b               e# Convert to array using base 8 conversion.
              f    e# For each digit:
    "><+-.,[]" =   e#     Select the corresponding character from the string.
                1> e# Discard the first character.

2

Bash + coreutils,39 + 47 = 86

b2u.sh

dc -e8i1`tr '<>+-.,[]' 0-7`p|tr -dc 0-9

u2b.sh

dc -e8o?p|tr -dc 0-9|tr 0-7 '<>+-.,[]'|cut -c2-

测试输出:

$ echo "++++++[>++++++++++++<-]>.>++++++++++[>++++++++++<-]>+.+++++++..+++.>++++[>+++++++++++<-]>.<+++[>----<-]>.<<<<<+++[>+++++<-]>.>>.+++.------.--------.>>+." | ./b2u.sh
239234206933197750788456456928845900180965531636435002144714670872282710109774487453364223333807054152602699434658684117337034763550216789 
$ echo 239234206933197750788456456928845900180965531636435002144714670872282710109774487453364223333807054152602699434658684117337034763550216789 | ./u2b.sh
++++++[>++++++++++++<-[>.>++++++++++[>++++++++++<-[>+.+++++++..+++.>++++[>+++++++++++<-[>.<+++[>----<-[>.<<<<<+++[>+++++<-[>.>>.+++.------.--------.>>+.
$

1
tr -dc 0-9 (并且在代码高尔夫球中,您可以认为这?
未曾逃脱的

1

杰普特,13 + 13 = 26个字节

一元到一元

i< n"><+-.,[]

尝试一下!

说明:

i<               :Insert a "<" at the start of the string (representing 1)
   n             :Convert string to decimal by interpreting as:
    "><+-.,[]    : A base 8 number represented by the 8 characters of BF

一元脑筋急转弯

s"><+-.,[]" Å

尝试一下!

说明:

s                :Convert decimal to string representation of:
 "><+-.,[]"      : Base 8 using the BF characters to represent the 8 digits
            Å    :Remove the extra "<" at the front

笔记

我找不到元帖子,但是如果我的记忆正确地提供答案,则可以将I / O限制为他们的语言可以支持的数字,只要他们实现的算法在该语言开始支持更大的数字时就可以使用。就是这种情况,Japt能够将字符串视为“ n使用这些n字符作为数字的基数”的功能只能使用number操作另一侧的数据类型,因此测试用例实际上不会成功运行。第一个程序的输出和的输入第二个程序会将数字强制为可以表示为的数字number,而不是使用实际数字。对于可以由Japt完美表示的数字number数据类型,这些程序将根据需要运行,如果number数据类型更改为支持更大的数字,则这些程序也将开始支持这些数字。


0

05AB1E,33(17 + 16)字节

一臂之力:

"><+-.,[]"sSk1š8β

在线尝试验证所有测试用例

说明:

"><+-.,[]"           # Push string "><+-.,[]"
          s          # Swap to take the (implicit) input
           S         # Convert it to a list of characters
            k        # Check for each the index in the string
             1š      # Prepend a 1 to the list of indices
               8β    # Convert the list to Base-8 (and output implicitly)

一元长度到Brainfuck

8в¦"><+-.,[]"sèJ

在线尝试验证所有测试用例

说明:

8в                  # Convert the (implicit) input-list from Base-8 to Base-10
  ¦                 # Remove the first 1
   "><+-.,[]"       # Push string "><+-.,[]"
             s      # Swap the list and string on the stack
              è     # Index each integer into this string
               J    # Join everything together (and output implicitly)

0

飞镖,77 + 142 = 219字节

f(s)=>BigInt.parse('1'+s.split('').map('><+-.,[]'.indexOf).join(''),radix:8);

F(n)=>BigInt.parse(n).toRadixString(8).split('').map((s)=>'><+-.,[]'.substring(int.parse(s),int.parse(s)+1)).join('').toString().substring(1);

在线尝试!


0

C(GCC),254个字节

#include"gmp.h"
f(i,o)char*i,*o;{mpz_t l;char*c="><+-.,[]";if(*i>47&*i<58)for(mpz_init_set_str(l,i,0),mpz_get_str(o,8,l);*o;*o++=o[1]?c[o[1]-48]:0);else for(mpz_init_set_si(l,1);mpz_get_str(o,10,l),*i;mpz_mul_si(l,l,8),mpz_add_ui(l,l,strchr(c,*i++)-c));}

在线尝试!

根据输入(i)确定要走的方向,并将结果存储在传递的缓冲区(o)中。请注意,某些编译器允许根据o ++的实现定义的顺序节省4个字节。在这些情况下,提供的解决方案将从Unary-> BF转换中截断一个多余的字符,并且o[1]所有都可以替换为*o来恢复行为。


此处的语言应为“ C(gcc)+ GMP”
仅限ASCII

另外,这是一个程序比两个程序短吗?而且我还建议放入#include <string.h>页脚而不是页眉,以显示它无需导入即可工作。同样,由于C ++的运算符重载,它不会更短吗?:P
仅ASCII

而且并不重要,但我每次更改siui可能
ASCII-仅

*i>47&*i<58-> *i%48<10
仅ASCII格式,

mpz_init_set_str->mpz_set_str
仅使用ASCII
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.