BenBE's humble thoughts Thoughts the world doesn't need yet …

02.08.2011

FizzBuzz BrainFuck

Filed under: Fun,GeSHi — Schlagwörter: , , — BenBE @ 01:17:35

Well yeah, after some nasty person dropped a link about why programmers are so bad at programming I somehow got to have a look at the Rosetta Code project’s site detailling this task and found (not to my surprise) that noone had solved that task — yet! So I sat down and implemented it. Usually this task should take you only about a few minutes but since I hardly ever programm anything in BrainFuck it took me roughly 45 minutes to complete. But well: Here’s the result (Beware: Ugly code)!

FizzBuzz
 
Memory:
  Zero
  Zero
  Counter 1
  Counter 2
 
  Zero
  ASCIIDigit 3
  ASCIIDigit 2
  ASCIIDigit 1
 
  Zero
  Digit 3
  Digit 2    
  Digit 1    
 
  CopyPlace
  Mod 3
  Mod 5
  PrintNumber
 
  TmpFlag
 
Counters for the loop
++++++++++[>++++++++++[>+>+<<-]<-]
 
Number representation in ASCII
>>>>
++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ [>+>+>+<<<-]
<<<<
 
>>
[
    Do hundret times:
 
    Decrement counter
    ->->
 
    Increment Number
    > >>+> 
        > >>+>
        <<<<
    <<<<
 
    Check for Overflow
    ++++++++++
    >>> >>>>
    >++++++++++<
    [-<<< <<<<->>>> >>> >-<]
    ++++++++++ 
    <<< <<<<
 
    Restore the digit
    [->>>> >>>-<<< <<<<]
    >>>> [-]+ >>>>[<<<< - >>>>[-]]<<<< <<<< 
 
    If there is an overflow
    >>>>[
        <<<<
 
        >>>----------> >>>----------<+<< <<+<<
 
        Check for Overflow
        ++++++++++
        >> >>>>
        >>++++++++++<<
        [-<< <<<<->>>> >> >>-<<]
        ++++++++++ 
        << <<<<
 
        Restore the digit
        [->>>> >>-<< <<<<]
        >>>> [-]+ >>>>[<<<< - >>>>[-]]<<<< <<<< 
 
        If there (again) is an overflow
        >>>>[
            <<<<
            >>---------->> >>----------<+< <<<+<
 
            >>>>
            [-]
        ]<<<<
 
        >>>>
        [-]
    ]<<<<
 
    >>>> >>>> 
 
    Set if to print the number
    >>>[-]+<<<
 
    Handle the Mod 3 counter
    [-]+++
 
    >>>>[-]+<<<<
    >+[-<->]+++<
    [->->>>[-]<<<<]
    >>>>[
        <[-]>
 
        [-]
        Print "Fizz"
        ++++++++ ++++++++ ++++++++ ++++++++
        ++++++++ ++++++++ ++++++++ ++++++++
        ++++++.
 
        ++++++++ ++++++++ ++++++++ ++++++++
        +++.
        
        ++++++++ ++++++++ +..
 
        [-]
        <<<--->>>
    ]<<<<
 
    Handle the Mod 5 counter
    [-]+++++
 
    >>>>[-]+<<<<
    >>+[-<<->>]+++++<<
    [->>->>[-]<<<<]
    >>>>[
        <[-]>
 
        [-]
        Print "Buzz"
        ++++++++ ++++++++ ++++++++ ++++++++
        ++++++++ ++++++++ ++++++++ ++++++++
        ++.
 
        ++++++++ ++++++++ ++++++++ ++++++++
        ++++++++ ++++++++ +++.
        
        +++++..
 
        [-]
        <<----->>
    ]<<<<
 
    Check if to print the number (Leading zeros)
    >>>[
        <<< <<<< <<<<
        >.>.>.<<<
        >>> >>>> >>>>
        [-]
    ]<<<
 
    <<<< <<<<
 
    Print New Line
    <<<<[-]++++ ++++ ++++ +.---.[-]>>
]
<<

I didn’t have a brainfuck interpreter at hand but I believe this should work. If it does not feel free to comment below pointing out the bugfixes. And I hope the documentation is appreciated; since that took extra time to write 😉

Share and enjoy!

Flattr this!

4 Comments »

  1. Hi!
    Where i can find something like installation guide for GeSHi 1.0.8.10 to phpbb 3.0.9?
    Or how can i do that?
    Thank you.

    Kommentar by leaf — 05.09.2011 @ 09:53:02

  2. I hope you don’t mind but I used your code in my forum post here:
    http://www.dreamincode.net/forums/topic/248759-obfuscater-challenge%232-what-the-fizz-buzz-is-that/page__gopid__1447115&#entry1447115

    It seems that your code works perfectly. Quite an achievement for never having tested it!

    Kommentar by cfoley — 29.09.2011 @ 03:43:37

  3. Regarding I didn’t test this: The raw structure of this code was written without a interpreter at hand, but due to the complexity there were 3 bugs left when I first run it later. Those three bugs required me to run it in an interpreter due to some wierd indexing problems.

    By the way thanks for your forum post. Looks interesting 😉

    Kommentar by BenBE — 29.09.2011 @ 12:39:36

  4. I was just at a job interview where a FizzBuzz test was part of the interview. I was also asked to identify snippets of code in several different languages, one of which was Brainfuck. (They were impressed as I was the only candidate they’d interviewed who recognized it.) In speaking about it with some friends afterwards one of them said „Someone should write FizzBuzz in Brainfuck“ and I figured someone had to have done it.

    Kommentar by Bill Duetschler — 30.04.2014 @ 17:14:26

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress