Advertisement
bf17

BF snippets

Mar 29th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. Brainfuck code snippets.
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- calculate base 256 checksum of a bunch of text
  3.   use a BF interpreter that allows entering input into a buffer (i.e. not interactive)
  4. +,[[[>+<-]],]
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- distribute N into memory: n, n-1, n-2, n-3, ... , n - n
  6. ++++++++ preload m0 with n
  7. [[>+>+<<-]>[<+>-]>[<+>-]<-]
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ breadcrumbs
  9. fill contiguous memory with a trail N of breadcrumbs '1s'
  10. N+
  11. [>>[>]+[<]<-]
  12. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. load memory with variable length text string (for display module)
  14. ,[>>[>] +[<]<-
  15.  [>>[>]<+[<]<-]
  16. ,]
  17. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. - Not N (not my discovery or code)
  19.   !N = 255 - N
  20. ,'A' load N into m0 in this case 65
  21. >-<[>-<-]
  22. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. - move data pointer n cells to the right
  24. ++++++ load m0 with n
  25. [>[-]<[>+<-]>-]
  26. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. - delay/countdown timer, because sometimes brainfuck is just too fast
  28. +[>-[-]<-]
  29. +[>- [>-[-]<-] <-]
  30. +[>- [>- [>- [>- [>- [>- [>- [>- [>-[-]<-] <-] <-] <-] <-] <-] <-] <-] <-]
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. - A + B with carry, 2 digit base 256 addition
  33. A[B +
  34. [temp0[B + temp0 -]
  35. temp1[- zero - temp1] + zero [temp1 - zero[-]]
  36. temp1[- carry + temp1]
  37. A -]
  38. for:
  39. memory0 = A
  40. m1 = carry
  41. m2 = B
  42. m3 = temp0
  43. m4 = temp1
  44. m5 = zero: value is always 0
  45. ++++>>--<<        start with m0=4 m2 = 254
  46. [>>+            start add loop
  47. [>+>+<<-]        move m3 back to m2
  48. >[->-<]+>[<->[-]]    set m4 = (m4 == m5)
  49. <[-<<<+>>>]        add m4 which is either a 1 or 0 to m1
  50. <<<<-]            end main add loop
  51. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. n/2
  53. >>n
  54. [-<+>[-<->>+>]<<]
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. check if N = 0
  57. memory:
  58. m0 = flag
  59. m1 = N
  60. m2 = 0: landing zone
  61. m3 m4: breadcrumbs
  62. >++++++        load N
  63. >>+>+<<<       create breadcrumbs
  64. [<+>-          assume that N will be 0 and set flag
  65. [<-]           but N isn't 0 so reset flag
  66. >              go to N
  67. ]              continue working on N
  68. >>[>]<[-<]<<   place data pointer at the flag ~ clean up breadcrumbs
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. - A - B using A + Two's Compliment(B)
  71. preload A
  72. preload B
  73. preload B2C with 255
  74. B[B2C - B -]        compute Not B and put in B2C
  75. B2C +            increment Not B to make it Two's Compliment
  76. B2C [A + B2C -]        add A to Two's Compliment of B
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78.  
  79. Truth Machine
  80. +++++++[>+++++++>
  81. +++++++<<-]>-<,>[
  82. <->-]<[>>[.]]>>-.
  83. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. This is just a bat.
  85.  
  86. ,             ,
  87. |\   |\~/|   /|
  88. | \__)o o(__/ |
  89. \  \ / ^ \ /  /
  90.  \=_=\   /=_=/
  91.   \ /\\~//\ / dmc
  92.    '  " "  '
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. find non-zero
  95. >>>-<<<          put a non zero value out in memory P
  96. +[-[   <-<]>+]>  someone else
  97. ||||::::||||||:    similarities
  98. +[-[[-]>-<]>+]<  mine
  99.                  does not maintain original value
  100.                  stops at P for value 1~254
  101.                  stops at P-1 for value = 255
  102. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. load fibonacci series in memory
  104. >+[[>+>+<<-]>>[<<
  105. +>>-]<<<[>>+>+<<<
  106. -]>>>[<<<+>>>-]<]
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. >+[<[>>+>+<<<-]>>
  109. >[<<<+>>>-]<<[>+>
  110. +<<-]>>[<<+>>-]<]
  111. >+            load the first two cells with 0 and 1
  112. [            start perpetual loop
  113.     <        go to A
  114.     [>>+>+<<<-]    dup A into A+B and Temp0
  115.     >>>        go to Temp0
  116.     [<<<+>>>-]    move Temp0 back to A
  117.     <<        go to B
  118.     [>+>+<<-]    dup B into A+B and Temp0
  119.     >>        go to Temp0
  120.     [<<+>>-]    move Temp0 back to B
  121. <            go to new B shifting all operations one to the right
  122. ]            close loop
  123. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124. +[[>>+>+<<<
  125. -]>>[<<+>>-
  126. ]<[<+>-]>>[
  127. <<+>>-]<<<#]
  128. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  129. Fibonacci sequence
  130. >-[[<+>>>-<-<+]>]
  131. http://inversed.ru/InvMem.htm#InvMem_7
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. count up
  134. >>>++++
  135. [<<<        start with n in m3
  136. [>+>+<<-]
  137. >>[<<+>>-]
  138. <+
  139. >>-
  140. [>+<-]>
  141. ]
  142. <<<[<]        go back to m0
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. >>>++++++
  145. [<<<[>+
  146. >+<<-]>
  147. >[<<+>>
  148. -]<+>>-
  149. [>+<-]>
  150. ]<<<[<]
  151. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152. is N even or odd
  153. >+++++++++++++++    load m1 with N
  154. >>+>+<<<        create trail of breadcrumbs to locate position of P
  155. [-[->]<]+        set m0 = 1 if odd
  156.             set m1 = 1 if even
  157. >>>[>]            resolve position
  158. <[<]             of data pointer P
  159. >->-<<            clean up breadcrumbs
  160. <            set P to m1 if N is even
  161.               or conversly 1 = even: 0 = odd
  162. <<            use this one to set P to m0 if N is odd
  163. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  164. N odd or even: bare minimum
  165.     >,             load m1 with N
  166.     
  167.     [-[->]<]+      set m0 = 1 if odd
  168.                    set m1 = 1 if even
  169. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. N odd or even? clean version that resolves P
  171.  
  172.     >,              ~load m1 with N (not counted for golf scoring)
  173.     >>+>+<<<        ~set 'trail of breadcrumbs' so we can figure out
  174.                        where P is
  175.     [-[->]<]+       ~if N is odd set m0 = 1
  176.     >>>[>]          ~figure out where P is
  177.     <[-<]<[-]<      ~go back to m1 and zero out if N is even
  178. Pointer P ends on m0
  179.   odd: m0 = 1
  180.  even: m0 = 0
  181. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  182. [m2 = (m0 > m1)]
  183. m0 = d1
  184. m1 = d2
  185. m2 = G
  186. m3 = landing zone (always zero)
  187. m4 = copy of d2 used for calculation
  188. m5 = start of breadcrumbs equal to d1
  189. +++++>+++<             load d1 and d2
  190. [>>>+>+<<<<-]         dup d1 to mem
  191. >>>[<<<+>>>-]         restore d1
  192. >[>[>]+[<]>-]         place breadcrumbs
  193. <<<[>>+>+<<<-]        dup d2 to mem
  194. >>[<<+>>-]>           restore d2
  195. [>[-]<[>+<-]+>-]     compare d2 with breadcrumbs
  196. >[<<[<]<+>>[>]]      set G (m2) to 1 if there are more breadcrumbs to the right
  197. >[-]<<[-]<[[-]<]<    clean up breadcrumbs and stop on G
  198. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  199.  
  200.  
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement