Skip to main content
Commonmark migration
Source Link

brainfuck - 169 146 142 bytes

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

#brainfuck - 169 146 142 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<,<<,>[->>>+<<<]>>>---------->]<-[->.+<] LimitationsLimitations:

#brainfuck - 8 bytes ,[->.+<]

brainfuck - 8 bytes

,[->.+<]

#brainfuck - 169 146 142 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<,<<,>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

#brainfuck - 8 bytes ,[->.+<]

brainfuck - 169 146 142 bytes

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

Limitations:

brainfuck - 8 bytes

,[->.+<]
added 45 characters in body
Source Link

#brainfuck - 169 146146 142 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<[-]<<[-]>[]]<,<<,>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

It's quite a difference! The real program uses 139135 instructions to collect the input (over 95% of the program!), just because it's a human typing it. Store the number as a byte and give that to me, and it only takes one.

-[+>+[+<]>+]>+               abuse 8 bit wrapping to put 47 in cell 4

>>,[>,]                      starting in cell 6; get each character of input

<[<]<[->>[->]<[<]<]          subtract the value of cell 4 from each input character
                             '0' has an ascii value of 47 so subtracting 47 from each
                             digit gives you that digit's value plus 1

>>>[                         if the number is in more than one cell
                             (when the program first starts this means "if the input has
                             more than one digit")

[<[-<+<+<+>>>]               copy first input cell to 3 new cells

+++++++++[<[-<+>]<<          do some fancy addition magic to multiply that value by 10
[-<+>>>+<<]<[->+<]>>>>-]]

<[-]<<[-]><,<<,>                       clean up a bit (abusing comma to set cells to 0)

[->>>+<<<]>>>                add the value to the next cell of input

----------                   because we multiplied (the digit plus 1) by 10; the answer
                             is 10 too high; so subtract 10

>]                           if the input is still in multiple cells; do the song and
                             dance again (multiply by 10; add to next cell; subtract 10)

<-                           we never got a chance to fix the final digit; so it's still 1
                             too high

               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
               ;;         we have now finished processing input         ;;
               ;;     the tape is empty except for the current cell     ;;
               ;;  the current cell contains the number that was input  ;;
               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[                            while the cell containing input != 0

-                            subtract 1 from it

>.+                          go a cell to the right; output that cell; then add 1

<]                           repeat

#brainfuck - 169 146 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<[-]<<[-]>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

It's quite a difference! The real program uses 139 instructions to collect the input (over 95% of the program!), just because it's a human typing it. Store the number as a byte and give that to me, and it only takes one.

-[+>+[+<]>+]>+               abuse 8 bit wrapping to put 47 in cell 4

>>,[>,]                      starting in cell 6; get each character of input

<[<]<[->>[->]<[<]<]          subtract the value of cell 4 from each input character
                             '0' has an ascii value of 47 so subtracting 47 from each
                             digit gives you that digit's value plus 1

>>>[                         if the number is in more than one cell
                             (when the program first starts this means "if the input has
                             more than one digit")

[<[-<+<+<+>>>]               copy first input cell to 3 new cells

+++++++++[<[-<+>]<<          do some fancy addition magic to multiply that value by 10
[-<+>>>+<<]<[->+<]>>>>-]]

<[-]<<[-]>                   clean up a bit

[->>>+<<<]>>>                add the value to the next cell of input

----------                   because we multiplied (the digit plus 1) by 10; the answer
                             is 10 too high; so subtract 10

>]                           if the input is still in multiple cells; do the song and
                             dance again (multiply by 10; add to next cell; subtract 10)

<-                           we never got a chance to fix the final digit; so it's still 1
                             too high

               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
               ;;         we have now finished processing input         ;;
               ;;     the tape is empty except for the current cell     ;;
               ;;  the current cell contains the number that was input  ;;
               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[                            while the cell containing input != 0

-                            subtract 1 from it

>.+                          go a cell to the right; output that cell; then add 1

<]                           repeat

#brainfuck - 169 146 142 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<,<<,>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

It's quite a difference! The real program uses 135 instructions to collect the input (over 95% of the program!), just because it's a human typing it. Store the number as a byte and give that to me, and it only takes one.

-[+>+[+<]>+]>+               abuse 8 bit wrapping to put 47 in cell 4

>>,[>,]                      starting in cell 6; get each character of input

<[<]<[->>[->]<[<]<]          subtract the value of cell 4 from each input character
                             '0' has an ascii value of 47 so subtracting 47 from each
                             digit gives you that digit's value plus 1

>>>[                         if the number is in more than one cell
                             (when the program first starts this means "if the input has
                             more than one digit")

[<[-<+<+<+>>>]               copy first input cell to 3 new cells

+++++++++[<[-<+>]<<          do some fancy addition magic to multiply that value by 10
[-<+>>>+<<]<[->+<]>>>>-]]

<,<<,>                       clean up a bit (abusing comma to set cells to 0)

[->>>+<<<]>>>                add the value to the next cell of input

----------                   because we multiplied (the digit plus 1) by 10; the answer
                             is 10 too high; so subtract 10

>]                           if the input is still in multiple cells; do the song and
                             dance again (multiply by 10; add to next cell; subtract 10)

<-                           we never got a chance to fix the final digit; so it's still 1
                             too high

               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
               ;;         we have now finished processing input         ;;
               ;;     the tape is empty except for the current cell     ;;
               ;;  the current cell contains the number that was input  ;;
               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[                            while the cell containing input != 0

-                            subtract 1 from it

>.+                          go a cell to the right; output that cell; then add 1

<]                           repeat
rewrote the whole post because i didn't like it.
Source Link

Brainfuck - 169 bytes

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

Requires 8#brainfuck -bit wrapping cells and EOF = 0 169 146 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<[-]<<[-]>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

  • EOF must be 0
  • Requires 8-bit wrapping cells
  • Because of ^, mods input by 256

Not the shortest answer here, but hey, brainfuck! This was fun. Surewould be a really, it's long. Surereally good brainfuck challenge, it's incredibly slowexcept for large inputsthe fact that it requires human readable input without guaranteeing the number of digits. Sure,I could have required input to have leading zeroes to make it probably chokes on large inputs (always chokes on 2563 characters long, will probably chokebut what fun is that? :D One major problem with taking input this way is that brainfuck's only branching or looping structure checks if youthe current cell is zero or not. When the input something closecan contain zeroes, it can cause your code to the tape size)take branches it shouldn't be taking. But give me a break: It'sTo solve this problem, I store each digit of input brainfuckplus 1, then subtract the excess at the last possible second. That way, I did it in brainfuckalways know where my zeroes are. :P

I'm notI did say that this would have been a master at golfing ingreat brainfuck so there are probably some byteschallenge without having to be saved in here somewhereparse input. In fact, over 80% of this programWhy is simply interpretingthat? Well, let's pretend that we don't take a numeric input. We'll say the challenge is "Given a byte of input, which is very hard when you don't know how many digits t's going tooutput all ASCII characters below that byte". Here's what my answer would be and don't want the:


#brainfuck - 8 bytes ,[->.+<]


It's quite a difference! The real program uses 139 instructions to die if one ofcollect the digits is 0. There are a lotinput (over 95% of values in thisthe program stored 1 greater than their actual value!), with 1 subtracted atjust because it's a human typing it. Store the very last second,number as a byte and give that to get around theme, and it only branch/loop structure being "is this cell 0?"takes one.   

(This is why it chokes on 256Fun fact: If you understood the hypothetical program, actuallythen congratulations! You understand brainfuck in its entirety. 255 + 1 = 0The whole language has only eight commands, and that program happens to use each one exactly once.)

ExplainedExplanation (to the best of my ability)

-[+>+[+<]>+]>+  the ascii representation of 0 is 48
             abuse 8 bit thiswrapping codeto putsput 47 in a cell
    4

>>,[>,]            it's very slow. if you're testing this i'd recommend replacing it with
  starting in cell 6; get each character of input

<[<]<[->>[->]<[<]<]      47 plus signs

>>,[>,]<[<]<  getsubtract allthe charactersvalue of input, then move thecell cursor4 backfrom toeach theinput 47character
              in hindsight i should probably have done this first
        '0' has an ascii value of maybe47 i'llso fixsubtracting it47 laterfrom buteach
 right now i am not touching this code

[->>[->]<[<]<]  subtract 47 from character digit of input
             digit gives you sincethat thedigit's asciivalue ofplus 01

>>>[ is 48; 1 is 49; 2 is 50; etc
                weif havethe eachnumber digitis plusin onemore inthan aone rowcell
                the first digit is two cells to our right

>>>[[<[-<+<+<+>>>]
+++++++++[<[-<+>]
<<[-<+>>>+<<]<[->+<]>>>>-]]
<[-]<<[-]>[->>>+<<<]
>>>---------->]<-        (when the program first woooahstarts this one's ameans doozy
"if the input has
                          check that there ismore athan digitone threedigit")

[<[-<+<+<+>>>] cells to our right
           copy first input cell to 3 new cells

+++++++++[<[-<+>]<<          do (thatsome is;fancy checkaddition ifmagic thereto ismultiply morethat thanvalue oneby digit)10
   [-<+>>>+<<]<[->+<]>>>>-]]

<[-]<<[-]>                   clean up a bit

[->>>+<<<]>>>    if so; multiply the first digit by 10
     add the value to the next cell of input

----------                then subtract 10 (because we were storingmultiplied (the digit plus 1) higher
by 10; the answer
                          than it actually was)
is 10 too high; so subtract 10

>]                       add the result to theif cellthe containinginput theis secondstill digit
in multiple cells; do the song and
                       align ourselves so that cell is twodance toagain our(multiply right
by 10; add to next cell; subtract 10)

<-                      repeat until the whole number iswe containednever ingot onea cell
chance to fix the final digit; so it's still 1
                    subtract 1 (because the final digit was also 1 too high)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;         we are now done interpreting the input;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
         ;
;a string was read from stdin and;; converted to an integer;
;     we have now finished processing input         ;;
      huzzah         ;;     the tape is empty except for the current cell   ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;
[[->+>+<<]>>[-<<+>>]<-]  copy the number to the next cell; leaving it intact
    ;;  the current cell contains the number that was input  ;;
         subtract 1 from the copy  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[                         do this until itwhile hitsthe cell containing input != 0 

-                         tape now looks like
subtract 1 from it

>.+                      (n)(n minus 1)(n minus 2)go alla thecell wayto downthe toright; (2)(1)(0)

<[-.+<]output that startingcell; atthen theadd 1

<] on the tape; subtract 1; output; add one; move left
         repeat until you run out of numbers

done!  repeat

Brainfuck - 169 bytes

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

Requires 8-bit wrapping cells and EOF = 0.

This was fun. Sure, it's long. Sure, it's incredibly slow for large inputs. Sure, it probably chokes on large inputs (always chokes on 256, will probably choke if you input something close to the tape size). But give me a break: It's brainfuck. I did it in brainfuck. :P

I'm not a master at golfing in brainfuck so there are probably some bytes to be saved in here somewhere. In fact, over 80% of this program is simply interpreting the input, which is very hard when you don't know how many digits t's going to be and don't want the program to die if one of the digits is 0. There are a lot of values in this program stored 1 greater than their actual value, with 1 subtracted at the very last second, to get around the only branch/loop structure being "is this cell 0?".  (This is why it chokes on 256, actually. 255 + 1 = 0.)

Explained (to the best of my ability)

-[+>+[+<]>+]>+  the ascii representation of 0 is 48
                this code puts 47 in a cell
                it's very slow. if you're testing this i'd recommend replacing it with
                47 plus signs

>>,[>,]<[<]<  get all characters of input, then move the cursor back to the 47
              in hindsight i should probably have done this first
              maybe i'll fix it later but right now i am not touching this code

[->>[->]<[<]<]  subtract 47 from character digit of input
                since the ascii of 0 is 48; 1 is 49; 2 is 50; etc
                we have each digit plus one in a row
                the first digit is two cells to our right

>>>[[<[-<+<+<+>>>]
+++++++++[<[-<+>]
<<[-<+>>>+<<]<[->+<]>>>>-]]
<[-]<<[-]>[->>>+<<<]
>>>---------->]<-            woooah this one's a doozy
                             check that there is a digit three cells to our right
                             (that is; check if there is more than one digit)
                             if so; multiply the first digit by 10
                             then subtract 10 (because we were storing the digit 1 higher
                             than it actually was)
                             add the result to the cell containing the second digit
                             align ourselves so that cell is two to our right
                             repeat until the whole number is contained in one cell
                             subtract 1 (because the final digit was also 1 too high)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;         we are now done interpreting the input         ;
;a string was read from stdin and converted to an integer;
;                         huzzah                         ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[[->+>+<<]>>[-<<+>>]<-]  copy the number to the next cell; leaving it intact
                         subtract 1 from the copy
                         do this until it hits 0
                         tape now looks like
                         (n)(n minus 1)(n minus 2) all the way down to (2)(1)(0)

<[-.+<]  starting at the 1 on the tape; subtract 1; output; add one; move left
         repeat until you run out of numbers

done!

#brainfuck - 169 146 bytes -[+>+[+<]>+]>+>>,[>,]<[<]<[->>[->]<[<]<]>>>[[<[-<+<+<+>>>]+++++++++[<[-<+>]<<[-<+>>>+<<]<[->+<]>>>>-]]<[-]<<[-]>[->>>+<<<]>>>---------->]<-[->.+<] Limitations:

  • EOF must be 0
  • Requires 8-bit wrapping cells
  • Because of ^, mods input by 256

Not the shortest answer here, but hey, brainfuck! This would be a really, really good brainfuck challenge, except for the fact that it requires human readable input without guaranteeing the number of digits. I could have required input to have leading zeroes to make it 3 characters long, but what fun is that? :D One major problem with taking input this way is that brainfuck's only branching or looping structure checks if the current cell is zero or not. When the input can contain zeroes, it can cause your code to take branches it shouldn't be taking. To solve this problem, I store each digit of input plus 1, then subtract the excess at the last possible second. That way, I always know where my zeroes are.

I did say that this would have been a great brainfuck challenge without having to parse input. Why is that? Well, let's pretend that we don't take a numeric input. We'll say the challenge is "Given a byte of input, output all ASCII characters below that byte". Here's what my answer would be:


#brainfuck - 8 bytes ,[->.+<]


It's quite a difference! The real program uses 139 instructions to collect the input (over 95% of the program!), just because it's a human typing it. Store the number as a byte and give that to me, and it only takes one. 

(Fun fact: If you understood the hypothetical program, then congratulations! You understand brainfuck in its entirety. The whole language has only eight commands, and that program happens to use each one exactly once.)

Explanation

-[+>+[+<]>+]>+               abuse 8 bit wrapping to put 47 in cell 4

>>,[>,]                      starting in cell 6; get each character of input

<[<]<[->>[->]<[<]<]          subtract the value of cell 4 from each input character
                             '0' has an ascii value of 47 so subtracting 47 from each
                             digit gives you that digit's value plus 1

>>>[                         if the number is in more than one cell
                             (when the program first starts this means "if the input has
                             more than one digit")

[<[-<+<+<+>>>]               copy first input cell to 3 new cells

+++++++++[<[-<+>]<<          do some fancy addition magic to multiply that value by 10
[-<+>>>+<<]<[->+<]>>>>-]]

<[-]<<[-]>                   clean up a bit

[->>>+<<<]>>>                add the value to the next cell of input

----------                   because we multiplied (the digit plus 1) by 10; the answer
                             is 10 too high; so subtract 10

>]                           if the input is still in multiple cells; do the song and
                             dance again (multiply by 10; add to next cell; subtract 10)

<-                           we never got a chance to fix the final digit; so it's still 1
                             too high

               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
               ;;         we have now finished processing input         ;;
               ;;     the tape is empty except for the current cell     ;;
               ;;  the current cell contains the number that was input  ;;
               ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[                            while the cell containing input != 0 

-                            subtract 1 from it

>.+                          go a cell to the right; output that cell; then add 1

<]                           repeat
Source Link
Loading