brainf_ck not operation in a list

46 views Asked by At

I'm having a problem with the not operation (and nearly all operations) in a list. What I mean with a list is 0 i1 i2 i3 ... in-1 in 0 with a unknown n

In my program I'm at an unknown index in that list and I need to check if it is 0 For the not algorithm you need a temporary value but you can only get to that value with a [<] or a [>] but then you will lose the value in the list.

reminder: the a = 0 algorithm goes like this:

t0[-]+ a[t0-] t0[ <code> ]
The only thing I could come up with is leaving a 1 between each index but that seems extremely un-elegant.

so my questions is : is there a better way to do this?

1

There are 1 answers

0
Cedric Mamo On BEST ANSWER

Actually the 1 between each element thing is really one of the more efficient ways to do it. Then you simply walk back and forth until you meet a zero and you know at which end of the sequence you are, and also how many there are. And they're really easy to clear up after each operation as well.

There are ways to use only one cell per element, but it would require moving all elements to the left of the one you want one position to the left, and then moving them all back, for each operation. In some cases this might be faster if you only store small values in each element and you have a lot of elements.

Depends what you want to achieve. Personally I think the first option of leaving a trail of 1s and clearing them afterwards is the better option, even though it requires twice the space, as it is usually significantly faster in the general case.