The cell at the end of the rainbow

31

2

We all know of, or at least have heard of, brainfuck, one of the most famous and influential esoteric languages. While these days most implementations use an infinite tape, the original compiler by Urban Müller had a tape of 30000 cells. A little known fact* is that there is actually a special value at the end of the tape, something that is almost never interacted with in a typical brainfuck program.

While you could print this value with 29999 >s and a ., brainfuck is known for its short and concise solutions, so your aim is to print this value with the minimum number of characters.

*fiction

Rules:

  • A refresher on the brainfuck instructions:
    • +/-: Increment/decrement the current cell, wrapping from 255 to 0 when 255 is incremented and vice-versa
    • </>: Move the pointer left/right on the tape
    • .: Print the value of the current cell.
    • [: Jump to the corresponding ] if the value at the cell is 0
    • ]: Jump to the corresponding [ if the value at the cell is not 0
  • In this case , (Get input) does nothing, as the original compiler leaves the cell unchanged on EOF, and this program should receive no input.
  • The tape is initially filled with all 0s, except for cell 30000, which contains an unknown value
  • The pointer starts at cell 1 and should end on cell 30000, having printed it.
  • Cells to the left of 1 and to the right of 30000 have undefined behaviour, so solutions should not visit these cells.
  • Note that the value at 30000 is may be a 0, so simply looping until you hit a non-zero cell will not work.
  • You should print only the value at cell 30000
  • The shortest solution wins!
    • Tie-breaker is the more efficient program (executes in the least steps), followed by the fastest submission time.

Tip: Esolanging Fruit suggests using https://copy.sh/brainfuck to test your code. The specs are as described in this challenge, and you can select the abort option so your program halts if it goes out of bounds.

Jo King

Posted 2018-03-01T04:32:19.653

Reputation: 38 234

Sandbox (deleted) – Jo King – 2018-03-01T04:32:40.817

Can we assume the implementation allows for holding values up to and including 30000 in a cell? – hakr14 – 2018-03-01T05:18:20.950

@hakr14 "+/-: Increment/decrement the current cell, wrapping from 255 to 0 when 255 is incremented and vice-versa" – Jo King – 2018-03-01T05:20:11.087

31 byte: .. I win. – Esolanging Fruit – 2018-03-01T05:23:50.720

1@EsolangingFruit Let me have my delusions please... – Jo King – 2018-03-01T05:38:29.520

@JoKing oh, whoops. Well in that case, I have no idea. Here's a basic (not golfed) solution for 16-bit cells, if anyone wants to use the ideas for a real solution.

– hakr14 – 2018-03-01T05:39:21.650

@hakr14 Your link doesn't seem to show anything – Jo King – 2018-03-01T05:48:19.930

@JoKing Oh. Yeah, that's cool. Not like I worked hard on that or anything.

This link should work. Sorry about that.

– hakr14 – 2018-03-01T05:58:24.517

What happens if the memory pointer moves past the cell boundary? – user202729 – 2018-03-01T06:06:59.593

@user202729 Your solution becomes invalid – Jo King – 2018-03-01T06:07:56.453

1I suggest changing the rule to "initially all the cells have the value of 0, except the last cell which have an unknown value". – user202729 – 2018-03-01T06:07:58.027

Done, thanks @user202729 – Jo King – 2018-03-01T06:09:16.950

4

Tip for anyone doing this: copy.sh/brainfuck is probably the best site for doing this, as it implements BF as described in this challenge and has a 30k tape by default. Moving right past the end of the tape causes the interpreter to hang.

– Esolanging Fruit – 2018-03-01T06:33:04.087

@EsolangingFruit That's my favorite BF interpreter. The input capabilities are nice, you can choose cell size and wrapping, and the memory dumping/inspection is helpful as well. – mbomb007 – 2018-03-09T22:51:23.590

Answers

12

50 bytes

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

Try It Here!

The first section (>+[>+[<]>->+]>-<<) sets up the tape as

0*12 121' 0 110

The next section ([>>-------[[>]+[<]>-]<<-]) iterates 121 times, appending 249 1s to the end of the tape each time. If you're quick with math, you might realise this 121*249 results in 30129 1s, but the first iteration has the leftover 110 to deal with and only appends (110-7) 103 1s to the tape, meaning there's only 29983 1s. The final tape looks like:

0*12 0' 0 0 1*29983 0 ???

And a final >>>[>]>. to print the value.

Some other algorithms of note:

  • 252*119 = 29988 (also 50 bytes)
  • 163*184 = 29992 (56 bytes)
  • 204*147 = 29988 (60 bytes)
  • 153*196 = 29988 (56 bytes)
  • 191*157 = 29987 (57 bytes)
  • 254*118 = 29972 (56 bytes)
    • This one is the one I consider the next closest to beating 50, since the extra bytes are basically just travelling the extra distance. If I could find an algorithm that generates 118 and travels further than 14 cells, this could beat it.

Jo King

Posted 2018-03-01T04:32:19.653

Reputation: 38 234

17

54 Bytes

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

119 x 252 = 29988. Straightforward nested loop.

I used this tool to verify that the pointer stops at 29999.

Bubbler

Posted 2018-03-01T04:32:19.653

Reputation: 16 616

3

81

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

This moves over by 150 cells 200 times.

This is much longer than I'd like it to be, and there's a serious obstacle to golfing:

I no longer know why I works.

This is littered with compensations for off-by-ones to the point where some of them could probably cancel out. I've added and deleted various segments so much that I no longer remember why I put certain things where they are (for example, why does the first part initialize the tape cells to 199 and 151 instead of 200 and 150?).

Here is my commented version of this monstrosity anyways, in the hope that I (or someone else) might find them useful:

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

0 0 0 199' 155

[
  n' k 0
  >[
    n k' j
    ->+[[->+<]<]>
    0' n k-1 j+1 OR
    n 0 0' j+1
    >>
  ]
  0*k n 0 0 k 0'
  <[-<<+>>]<<<-
  0*k n-1' k
]
>-------[[->+<]>-]>.

Esolanging Fruit

Posted 2018-03-01T04:32:19.653

Reputation: 13 542

This doesn't actually print the value, and if the value is positive it overflows. But a great attempt – Jo King – 2018-03-01T06:34:47.637

Then add a ., would it help? – user202729 – 2018-03-01T11:56:12.003

1@JoKing Fixed that. – Esolanging Fruit – 2018-03-01T16:38:27.030