Skip to content

Instantly share code, notes, and snippets.

@cammckinnon
Created April 12, 2012 17:12
Show Gist options
  • Save cammckinnon/2369233 to your computer and use it in GitHub Desktop.
Save cammckinnon/2369233 to your computer and use it in GitHub Desktop.
FizzBuzz (brainfuck)
Write fizz to memory using cells 0 to 5 inclusive
[-]>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]<<<<<
Write buzz to memory using cells 6 to 11 inclusive
>>>>>>
[-]>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>[-]<<<<<
Cell 12 will be used for ANDing the results of the fizz and buzz tests
Put up counter in cell 14 and down counter in cell 13
>>>>>>
>
[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>[-]+<
while counter GE 0
[
Reset cell 12
<[-]>
Compute mod 3
>>[-]>[-]<<[->+>+<<]>>[-<<+>>]<<>>[-]<>>[-]+++[<<>>>>>>>>>[-]>[-]>[-]>[-]><<<<<<<<<<<<<>>>>>>>>>[-]>[-]<<<<<<<<<<[->>>>>>>>>+>+<<<<<<<<<<]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<>>>>>>>>><<<<<<<<<>>>>>>>>>>[-]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>>>+>+<<<<<<<<<<<]>>>>>>>>>>>[-<<<<<<<<<<<+>>>>>>>>>>>]<<<<<<<<<<<>>>>>>>>>>[>[-]>[-]<<[->+>+<<]>>[-<<+>>]<<>>[-]<>>[-]++++++++++[<<>>>>>>>>>[-]>[-]>[-]>[-]><<<<<<<<<<<<<>>>>>>>>>[-]>[-]<<<<<<<<<<[->>>>>>>>>+>+<<<<<<<<<<]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<>>>>>>>>><<<<<<<<<>>>>>>>>>>[-]+>[-]<<[<<<<<<<<<>>-<<->>>>>>[-]>[-]>[-]>[-]><<<<<<<<>>>>[-]>[-]<<<<<[->>>>+>+<<<<<]>>>>>[-<<<<<+>>>>>]<<<<<>>>><<<<>>>>>[-]+>[-]<<[<<<<>>>>>[-]]>[>]<[<<<<<[-]++++++++++<<>+<>>>>>>>[-]]<[-]>[-]>[-]>[-]<<<<<<<<<>>>>>>>>>>[-]]>[>]<[<<<<<<<<<<>>[>>>>[-]>[-]>[-]>[-]><<<<<<<<>>>>[-]>[-]<<<<<[->>>>+>+<<<<<]>>>>>[-<<<<<+>>>>>]<<<<<>>>>----------<<<<>>>>>[-]+>[-]<<[<<<<+<<+>>>>>>>[-]]>[>]<[<<<<<[-]>>>>>[-]]<[-]>[-]>[-]>[-]<<<<<<<]<<>>>>>>>>>>[-]]<[-]>[-]>[-]>[-]<<<<<<<<<<<<>>]<<><<[-]>>[-<<+>>]<<<<<<<<<>[>]++++++++++++++++++++++++++++++++++++++++++++++++<[<]>>>>>>>>[-<<<<<<<<>[>]<+<[<]>>>>>>>>]<]<<<<<<<<<<>>>>[>]<[.<]>[>]<[[-]<]<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>[-]]<[-]>[-]>[-]>[-]<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
Print a linebreak using cell 12 as a scratch cell
<[-]++++++++++.[-]>
-
Add 1 to up counter
>+<
end while
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment