3
\$\begingroup\$

The challenge is to write a brainf*** program that searches sequentially through memory for the first NON-zero cell.

The program should start somewhere in fairly low memory (there will probably be some overhead/counters/etc.). Once the program starts checking cells, it should continue to the right, checking every cell in sequence until it reaches the first non-zero cell, then stop.

Testing will be performed for several random cells and values. Examples might look like: m(13) = 1, or m(144) = 137, or m(983) = 231. The actual values will be randomly generated once and used for all entries. The cell values of 1 and 255 will also be tested.

Scoring - different from the usual
- code golf, lowest score wins
- in the case of a tie, earlier entry wins
- any continuous series of identical commands that changes pointer position or cell value counts as 1 point.

>                           1 point  
> >                         1 point  
> > > > > > > > > > > > > > > > > > > > > > > > > > 1 point  
< < < < < < < < < < <       1 point  
+ + + + + +                 1 point  
- -                         1 point  

[       opening a loop = 1 point  

]       closing a loop required, so 0 points  

one point for EACH input or output  
. .             2 points  
. . . . . .     6 points  
,               1 point  
, ,             2 points (not that anyone would ever do this)  

The python 3 program I will use for scoring is found here:
http://pastebin.com/RJi72BHa

\$\endgroup\$
10
  • 4
    \$\begingroup\$ Welcome to PPCG. We tend to prefer challenges that are not language specific. The language restriction means this challenge has a most definite shortest answer, which is rather limiting. Also I'm a little confused by the idea of a brainfuck program searching memory. Brainfuck is normally run as a virtual machine whose memory is set to all zeroes at the start. Do you have or know of a brainfuck interpreter that allows data to be poked into the memory before running? also, where in the memory can the submitted brainfuck program store its own variables? \$\endgroup\$ Aug 10, 2015 at 16:03
  • \$\begingroup\$ Thanks for your questions. I knew this one would be a confusing one to ask. So I will try to clarify. This question only makes sense for BF, any other language and it wouldn't be an issue. What got me wondering about this is: you have probably seen the simple example of BF code [>] that is go through memory and stop on the first zero cell. Well I wanted to write the opposite version. Go through memory with an unknown series of zero cells and stop on the first non-zero cell. As for testing programs, I will place a random value (1-255) somewhere in a random cell. All of the cells before it ... \$\endgroup\$
    – a62
    Aug 10, 2015 at 16:16
  • \$\begingroup\$ ...will be zero. So a valid program will start at the first available cell (to the right of the program itself) and if zero, continue checking to the right, maybe for hundreds of cells, until the first non-zero cell is found. \$\endgroup\$
    – a62
    Aug 10, 2015 at 16:17
  • 3
    \$\begingroup\$ @vihan Consensus is that we do not censor language names. Brainfuck is the correct name for this language, and it is only profane if you insist on viewing it that way. In any case the profane part of the name is not insulting to any particular group, so should not cause offence among adults. meta.codegolf.stackexchange.com/q/1136/15599 . This type of censorship makes the questions less searchable. Anyway I have decided not to roll back your edit as it would be yet another pointless edit, but please bear this in mind in future. \$\endgroup\$ Aug 11, 2015 at 5:14
  • 1
    \$\begingroup\$ @Thrax that point was mentioned in one of the answers to the meta question I linked. I'm still against censoring it. You shouldn't be using your work computer to access Programming Puzzles and Code Golf anyway. I never do ;-) \$\endgroup\$ Aug 25, 2015 at 13:13

5 Answers 5

6
\$\begingroup\$

11 24 Points

Edit: Now, we do not change the value that is there and do not output it. This now behaves as expected for 255 as well.

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

This program searches for the first non-zero byte and exits. We expect the first value pointed at to be 0, and we require that the pointer be at least at cell 1 to begin with ("The program should start somewhere in fairly low memory"). Here's a little test, no funky interpreter required:

[ Search for the first non-zero cell            ]
[ http://codegolf.stackexchange.com/q/54432/31054   ]

[We require moving one cell to the right from the left end ] >

[Testing code: ]
>>>>>>>>> [... any number of > ...] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
[ASCII code '0'] ++++++[-<++++++++>]
<<<<<<<<< [... the same number of < ...] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


[Find it and stop:  ]
+[-[[-<<+>>]<<+>>-<]>+]-<<[->>+<<]>>



>[Output, for checking: ]<
.   

Example run:

~ Me$ bf f.bf
0
\$\endgroup\$
12
  • \$\begingroup\$ It's not entirely clear from the spec whether or not you can guarantee that the head starts at a 0-byte, but this solution will output nothing if the head starts at a 255 byte. \$\endgroup\$
    – ymbirtt
    Aug 10, 2015 at 21:09
  • \$\begingroup\$ @ymbirtt Yeah, I noticed that. I'm still not entirely sure what should be output (if anything) anyway, so I decided to leave it unless a62 told me it was broken. I may make a longer solution for all the edge cases when I have time. \$\endgroup\$
    – BrainSteel
    Aug 10, 2015 at 21:54
  • \$\begingroup\$ Nothing needs to be output or input, just find. The description in the code section is just to explain how the scoring works. \$\endgroup\$
    – a62
    Aug 11, 2015 at 4:32
  • \$\begingroup\$ The value should not change the value once it is found. \$\endgroup\$
    – a62
    Aug 11, 2015 at 4:32
  • 1
    \$\begingroup\$ @a62 regarding "the value shound not change the value once it is found": this was not stated anywhere when this answer was posted, and still isn't edited into the question. This very clever answer was valid according to the rules at the time. Changing or adding rules is considered bad form. This actually makes writing a challenge very hard. You can post your next one at meta.codegolf.stackexchange.com/q/2140/15599 to try to get some feedback before posting. \$\endgroup\$ Aug 11, 2015 at 5:47
4
\$\begingroup\$

Command count: 12
Golf score: 10
+[-[<-<]>+]>
This is my first attempt for a Brainfuck program
This seems to work fine. the program was inspired from the answer of Brainsteel(Though seems to be highly optimized and hardly the same program)
My first attempt(Without brainsteels answer also it solves the negative 1 problem in brainsteel and my answer) was:
+[>[[<+>-]>]<[->+<]>]<<[->+<]>-
With Command count: 31
Golf score: 25

\$\endgroup\$
2
\$\begingroup\$

18 bytes, 15 points

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

Code requires to have at least two 0s before searched value. It finds any number, even -1 and doesn't change it.

\$\endgroup\$
1
\$\begingroup\$

12 bytes, 8 points

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

  • Does not change the non-0 cell value
  • Does not work if the non-0 cell is the 1st or 2nd cell
  • Stops on the second cell before the non-0 cell (e.g. [][][pointer][][non-0][][])

or... +[-->>[<]<+]>> (14b 9pts) which will stop on the non-0 cell

\$\endgroup\$
0
\$\begingroup\$

Here is my attempt. After doing it, I see that it is similar to other entries.

+[-[[-]>-<]>+]<   has 15 commands not including part that puts number in memory

             does not maintain original value
             stops at P for value 1~254
             stops at P-1 for value = 255
\$\endgroup\$

Your Answer

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

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