如何在BrainFuck中以任意数量移动指针?

Jim*_*inP 2 brainfuck

例如,如果数组是这样的0 0 0 0 ... 0 0[n]s o m e d a t a 4 9 9 9 9 9 9 8 3 7 ...,如何将指针移动n,而不是s o m e d a t a 4 9 9 9 ...在指针移动后改变?

Sor*_*row 5

好挑战.我喜欢这种语言:)

我的解决方案(我认为)有点正确,它将光标移动到长度为n的数据之后而不更改它,它将数据向左移动两个单元格(或者,如果满足某些条件,则移动一个数据).这是代码,希望你发现它很有用.

要求是:

  • 存在对数据的左侧的空单元,并
  • 指针所指向的空单元,并
  • 数据本身没有空单元格.

这是代码:

>[<->+]<[>>[-<+>]<<[->>+<<]>[-<+>]<-]
Run Code Online (Sandbox Code Playgroud)

示例:一个数组7 9 6,因此3告诉您移动光标的距离.

0 3 7 9 6 ?   this line represents data; values are numbers in cells
^             this shows the pointer location

>[<->+]< gives

3 0 7 9 6 ?
^

now the loop; while the pointer is not zero
[

>>[-<+>] gives in first iteration
3 7 0 9 6 ?
    ^

<<[->>+<<] gives in first iteration
0 7 3 9 6 ?
^

>[-<+>] gives in first iteration
7 0 3 9 6 ?
  ^

>[-<+>] gives in first iteration
7 3 0 9 6 ?
    ^

<- decreases the loop pointer and gives in first iteration
7 2 0 9 6 ?
  ^

which means the loop can continue giving
7 2 9 0 6 ?
7 0 9 2 6 ?
7 9 0 2 6 ?
7 9 2 0 6 ?
7 9 1 0 6 ? after the second iteration and finally
    ^
7 9 6 0 0 ? after the last
      ^
] which is where the loop will end
Run Code Online (Sandbox Code Playgroud)

现在,如果整个序列左侧还有一个空单元格,那么我们可以将数据向右移动一个单元格.

0 7 9 6 0 0 ?
        ^

<[[>+<-]<] gives
0 0 7 9 6 0 ?
^

>>[>] gives finally
0 0 7 9 6 0 ?
          ^
Run Code Online (Sandbox Code Playgroud)

因此,光标移动到任意长度的数据后面,然而将数据向左移动一个单元格.

免责声明代码中可能存在错误,但这个想法本身应该很清楚.只要与示例不匹配,请随意更正代码.