5
\$\begingroup\$

Usually each cell in brainfuck has 256 states, which is not enough to tell whether input is EOF or a character (usually \xFF). To fix it we can add one more state to it, and EOF (256) isn't mistake from a byte.

Since outputting EOF is also not defined, . on 256 works as reading, and therefore , is removed from the modified version.

Given a program in the 256-wrap brainfuck, provide a 257-wrap brainfuck program with same behavior when an infinite stream is provided as input. Memory needn't remain same though.

Sample:

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

This is , shortest code (converter) in each language wins.

\$\endgroup\$
11
  • \$\begingroup\$ Do we need to "simulate wrapping". Or in other words, does -. translate to --.or -.? \$\endgroup\$
    – AnttiP
    Mar 6, 2022 at 16:14
  • 1
    \$\begingroup\$ This doesn't seem like it would be possible. I feel like there's going to be some halting-problem stuff, depending on the specifics of how the wrapping works. \$\endgroup\$ Mar 6, 2022 at 16:26
  • \$\begingroup\$ @RadvylfPrograms It's definitely possible, just that there isn't any obvious non-annoying way to do this (at least to me) \$\endgroup\$
    – AnttiP
    Mar 6, 2022 at 16:51
  • \$\begingroup\$ @RadvylfPrograms I think the last sample show it possible \$\endgroup\$
    – l4m2
    Mar 6, 2022 at 16:52
  • \$\begingroup\$ Problem is, if "memory needn't remain same though.", the control flow's going to get all messed up. Is that fine? \$\endgroup\$ Mar 6, 2022 at 16:55

1 Answer 1

5
\$\begingroup\$

Python 3.8 (pre-release), 87 86 bytes

lambda s:"".join([3*c,p:="++[->>+<]>[-<]<","[-]-.",p*255,c][ord(c)%31%11%7]for c in s)

Try it online!

Basically the idea is that we only use every third cell to store the memory that the input program uses. The other cells are simply zero. This gives space to implement the addition code. The addition code is as follows:

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

Let's see how this works. There are two possibilities, either the cell has value of 255 or it has a lower value.

If the cell has value 255 then, after the first additions the value of the cell is now 0 (since it's mod 257). This means that the next loop won't execute at all. So we skip the first loop and move right. This square is 0, so we won't execute the next loop and we finally move left, back to the starting square, which has now value 0, meaning that we succeeded in doing 255->0.

If the cell has some other value x, the first two additions will make the value of the cell x+2 which is not zero. This means that the next loop will execute. We decrement this cell (value now is x+1) and move twice to the right. We increment that cell (now has value 1) and move left. The cell we are on right now has also value 0, so the loop stops. We then move right, to find our cell which has value 1. We decrement it to leave it in a clean state and move left. Again, this cell has value 0, so the loop stops. Finally we move left once to get back to where we started, with the cell having value x+1

Decrementation is just addition 255 times. [, . and ] don't need to be changed. We triplicate > and < since every third cell stores the "original" program values. Actually, we also triplicate [ and ], but this doesn't have any effect on the execution of the program. For , we just zero the cell with [-] and then decrement once more to get value 256 and then use the dot.

\$\endgroup\$
3
  • \$\begingroup\$ Do you assume no nop in source? \$\endgroup\$
    – l4m2
    Mar 6, 2022 at 17:51
  • \$\begingroup\$ @l4m2 Yes, should they be handled? \$\endgroup\$
    – AnttiP
    Mar 6, 2022 at 17:51
  • \$\begingroup\$ I guess it's fine but if someone cost some more bytes to handle them then this don't win that \$\endgroup\$
    – l4m2
    Mar 6, 2022 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.