Quantcast
Channel: Write a Stack Exchange compliant brainfuck explainer - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 17

Answer by Dom Hastings for Write a Stack Exchange compliant brainfuck explainer

$
0
0

Perl 5+-nF -M5.10.0, 93 bytes

Thanks to @Fmbalbuena for noticing I'd missed printing the unaltered string! Thanks to @emanresu A and @Neil's regex wizardry saving 14 (!!) bytes!

@@=<>;say$"x4,$_,map$"x(4+$-).$_.$"x(@F-($-+=y///c)).$@[index'><+-.,[]',$_],/[^]<>+-.[]+|./g

Try it online!

Explanation

First print4x (string repetition operator) $" (which defaults to space), then store the length of the input (implicitly obtained via -F into @F) in $=, and store the explanations in @@. Then, for each matching piece of code (either one char from -<>+.,[], or any number of any other chars) concatenate the following with $\: 4 +$- (which starts out as 0) x$", followed by the piece of code ($_), followed by the original length, minus $- (to which we add the current length of this code piece) x$" and finally, using the index function on the string ><+-.,[], the message associated to the current piece of code (index returns -1 for missing index, for which Perl returns the last element of the list). $_ is output followed by $\ thanks to -p flag.


Perl 5+-F/([^]<>+-.[]+|.)/ -M5.10.0, 92 bytes

The regex used in the above, can be provided via -F to save a byte.

$==y///c;@@=<>;say$"x4,$_,map/./&&$"x(4+$-).$_.$"x($=-($-+=y///c)).$@[index'><+-.,[]',$_],@F

Try it online!


Viewing all articles
Browse latest Browse all 17

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>