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

发布于 2024-11-15 09:00:27 字数 136 浏览 6 评论 0原文

例如,如果数组类似于 0 0 0 0 ... 0 0[n]somedata 4 9 9 9 9 9 9 8 3 7 ...,如何将指针移动 n,指针移动后不改变 somedata 4 9 9 9 ...

For example, if the array is like 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 ..., how to move the pointer by n, without changing s o m e d a t a 4 9 9 9 ... after the pointer is moved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-22 09:00:27

很好的挑战。我喜欢这种语言:)

我的解决方案(我认为)有点正确,它将光标移动到长度为 n 的数据之后而不改变它,但是它将数据向左移动两个单元格(或一个,前提是满足一定的条件)。这是代码,希望您觉得有用。

要求是:

  • 数据左侧有一个空单元格并且
  • 指针指向该空单元格并且
  • 数据本身没有空单元格。

代码如下:

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

示例:数组 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

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

0 7 9 6 0 0 ?
        ^

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

>>[>] gives finally
0 0 7 9 6 0 ?
          ^

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

免责声明代码中可能有错误,但想法本身应该是清晰的。只要代码与示例不匹配,请随时更正该代码。

Nice challenge. I love this language :)

My solution is (I think) somewhat correct, it moves the cursor after the data of length n without changing it, but it moves the data two cells to the left (or one, provided certain conditions are met). Here is the code, hope you find it useful.

The requirements are:

  • there is an empty cell to the left of the data and
  • the pointer is pointing to that empty cell and
  • there are no empty cells in the data itself.

Here is the code:

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

Example: an array 7 9 6, so 3 tells how far to move the cursor.

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

Now, if there is an additional empty cell to the left of the whole sequence, then we can move the data one cell to the right.

0 7 9 6 0 0 ?
        ^

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

>>[>] gives finally
0 0 7 9 6 0 ?
          ^

So the cursor is moved behind the data of an arbitrary length, however shifting the data one cell to the left.

Disclaimer There might be errors in the code, but the idea itself should be clear. Feel free to correct the code whenever it does not match the example.

Saygoodbye 2024-11-22 09:00:27

这可能太明显了,但您可以使用 [>][<] 将指针滑动到下一个零。那么你可以在数据单元格中加 1(打包一个 peano)并在输出之前减 1 ([-.>]) 吗?

遗憾的是,这可能无法提供足够精细的控制。

This may be too obvious, but you can slide the pointer to the next zero, with [>] or [<]. So can you add 1 to your data cells (packing a peano) and subtract 1 before output ([-.>])?

Sadly, this may not offer fine enough control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文