Brainfuck encoder
python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pybfenc.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/python
  2. #
  3. # Copyright 2014 Zered <zered@member.fsf.org>
  4. #
  5. # bfstr is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. #( at your option) any later version.
  9. #
  10. # bfstr is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with bfstr. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. """
  19. Brainfuck encoder
  20. """
  21. import sys
  22. from time import sleep
  23. PRIMES = [ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251 ]
  24. OPTIPRIMEMAX = 21
  25. OPTIDELTAMIN = 26
  26. # Using the generator pattern (an iterable)
  27. class BrainfuckGenerator(object):
  28. def __init__(self,inStr=None):
  29. self.prev = 0
  30. self.end = False
  31. if inStr != None:
  32. self.next(inStr)
  33. end = True
  34. def __iter__(self):
  35. return self
  36. # Python 3 compatibility
  37. def __next__(self):
  38. return self.next()
  39. def next(self,inStr):
  40. if self.end:
  41. raise StopIteration()
  42. res=""
  43. for c in inStr:
  44. nc = ord(c)
  45. plus = True
  46. delta = nc-self.prev
  47. if delta < 0:
  48. plus = False
  49. delta*=-1
  50. """
  51. if delta > 1:
  52. res+=nb2bf(delta,plus)
  53. elif delta == 1:
  54. if plus:
  55. res+='+'
  56. else:
  57. res+='-'
  58. """
  59. if delta > OPTIDELTAMIN:
  60. res+=nb2bf(delta,plus)
  61. elif delta > 0:
  62. if plus:
  63. chrAdd='+'
  64. else:
  65. chrAdd='-'
  66. for _ in range(delta):
  67. if plus:
  68. res+=chrAdd
  69. else:
  70. res+=chrAdd
  71. res+='.'
  72. self.prev = nc
  73. return res
  74. #Function defined only for non generator purpose
  75. def str2bf(inStr):
  76. bg = bfGen()
  77. return bg.next(inStr)
  78. #Return the brainfuck ops to add (or sub) delta to the current val
  79. def nb2bf(delta, plus):
  80. primes = primeFact(delta)
  81. #we will test if when adding or substracting 1 is not shorter
  82. primesP1 = primeFact(delta+1)
  83. primesM1 = primeFact(delta-1)
  84. """
  85. #print "delta",delta
  86. primes = optiPrime(primeFact(delta))
  87. #we will test if when adding or substracting 1 is not shorter
  88. primesP1 = optiPrime(primeFact(delta+1))
  89. primesM1 = optiPrime(primeFact(delta-1))
  90. """
  91. p1 = False
  92. m1 = False
  93. if sum(primes) > sum(primesP1):
  94. primes = primesP1
  95. p1=True
  96. if sum(primes) > sum(primesM1) and delta-1>1:
  97. primes = primesM1
  98. p1=False
  99. m1=True
  100. res = ""
  101. mv=0
  102. if len(primes) > 1:
  103. for prime in primes:
  104. mv+=1
  105. res+='>'
  106. for _ in range(prime):
  107. res+='+'
  108. res+='['
  109. for _ in range(mv):
  110. res+='<'
  111. if plus:
  112. res+='+'
  113. else:
  114. res+='-'
  115. for _ in range(mv):
  116. res+='>'
  117. res+='-]'
  118. for _ in range(mv-1):
  119. res+='<-]'
  120. res+='<'
  121. else:
  122. for foo in range(primes[0]):
  123. if plus:
  124. res+='+'
  125. else:
  126. res+='-'
  127. #Correcting the optimisation add 1 or sub 1
  128. if plus:
  129. if p1:
  130. res+='-'
  131. elif m1:
  132. res+='+'
  133. else:
  134. if p1:
  135. res+='+'
  136. elif m1:
  137. res+='-'
  138. return res
  139. #Prime factor decomposition
  140. def primeFact(n):
  141. nc = n
  142. res = []
  143. """
  144. for i in PRIMES:
  145. if nc == 0:
  146. return res
  147. while nc%i == 0:
  148. res.append(i)
  149. nc/=i
  150. """
  151. for i in PRIMES:
  152. if nc == 0:
  153. return res
  154. while nc%i == 0:
  155. #optimization
  156. if len(res) > 0 and res[-1] * i < OPTIPRIMEMAX:
  157. res[-1]*=i
  158. else:
  159. res.append(i)
  160. nc/=i
  161. return res
  162. def main():
  163. if (len(sys.argv) == 2):
  164. print BrainfuckGenerator(sys.argv[1])
  165. else:
  166. bg = BrainfuckGenerator()
  167. for line in sys.stdin:
  168. sys.stdout.write(bg.next(line))
  169. sys.stdout.flush()
  170. if __name__ == "__main__":
  171. main()