문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top