6
\$\begingroup\$

I'm currently learning as a challenge for golfing, and I'd like a code review to make sure I'm understanding things properly. For starters, here's my snippet:

[
  The purpose of this program is to add two
  predetermined values together and print the result.

  For this program the values 15 and 20 have been
  chosen to add together which should give a
  result of 35 and print # at the end.
]

+++++           Iterate 5 times
[
    >+++        Add 3 to cell one
    >++++       Add 4 to cell two
    <<-         Subtract 1 from cell zero
]

Currently cell one is 15 and cell two is 20
The resulting output should be 35

>[              Move to cell one and iterate until cell one is zero
    >+          Add 1 to cell two
    <-          Subtract 1 from cell one
]
>.              Print cell two

The objective of this snippet is to take two predetermined values 15 and 20, add them together (resulting in 35), and print the result to the console. Since brainfuck works with ASCII values, the octothorp should be printed to the console at the end:

#

I'm sure there are shorter ways to do this, but I'm looking for feedback on how well I've documented what it's doing, the formatting/style of the breakdown, and the overall approach to the problem.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Shorter ways

I'm sure there are shorter ways to do this

What makes you so sure?

There is no shorter way to shorten the moving of one cell to another. I doubt there are any shorter and more efficient ways to setup the numbers 15 and 20. See https://esolangs.org/wiki/Brainfuck_constants for how to setup any particular number in Brainfuck, but when setting up multiple numbers at the same time it is much faster and performant to not set them up individually. For more on this topic, see one of my previous Brainfuck answers


Documentation

I'm looking for feedback on how well I've documented what it's doing, the formatting/style of the breakdown, and the overall approach to the problem.

I think it is very well-documented. I like that you are having a section of documentation at the start, that is very common and a very safe thing to do, as all values are zero the code inside (you did have two print instructions inside) will never run.

For those who are used to reading brainfuck (believe it or not, we do exist), it's not necessary to write the details about iteration and how much you are adding and substracting at each cell.

I normally structure my brainfuck programs in three sections: Setup, logic, output.

The only documentation needed in the beginning would be something like "Setup the values 0 15 20 on the tape".


The documentation of what you are doing is also very well-documented, but the [>+<-] pattern is very common in Brainfuck so it would be enough to document "Move value at cell 2 to cell 1". It's very clear that you are moving values (again, for those of us who are used to reading brainfuck), but which values are being moved is the interesting part here for the one who reads the documentation.

Approach to the problem

You have a very clear and very solid approach to the problem.

Next steps...

Adding hard-coded values is quite easy. How about letting the user input numbers and adding them together? That would force you to convert between actual values and ASCII characters.

See also my brainfuck questions

\$\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.