Brainfuck encoder
python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pybfenc.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/python
  2. #
  3. # Copyright 2014 Yann Weber <yannweb@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. if delta > OPTIDELTAMIN:
  51. res+=nb2bf(delta,plus)
  52. elif delta > 0:
  53. if plus:
  54. chrAdd='+'
  55. else:
  56. chrAdd='-'
  57. for _ in range(delta):
  58. if plus:
  59. res+=chrAdd
  60. else:
  61. res+=chrAdd
  62. res+='.'
  63. self.prev = nc
  64. return res
  65. #Function defined only for non generator purpose
  66. def str2bf(inStr):
  67. bg = bfGen()
  68. return bg.next(inStr)
  69. #Return the brainfuck ops to add (or sub) delta to the current val
  70. def nb2bf(delta, plus):
  71. primes = primeFact(delta)
  72. #we will test if when adding or substracting 1 is not shorter
  73. primesP1 = primeFact(delta+1)
  74. primesM1 = primeFact(delta-1)
  75. p1 = False
  76. m1 = False
  77. if sum(primes) > sum(primesP1):
  78. primes = primesP1
  79. p1=True
  80. if sum(primes) > sum(primesM1) and delta-1>1:
  81. primes = primesM1
  82. p1=False
  83. m1=True
  84. res = ""
  85. mv=0
  86. if len(primes) > 1:
  87. for prime in primes:
  88. mv+=1
  89. res+='>'
  90. for _ in range(prime):
  91. res+='+'
  92. res+='['
  93. for _ in range(mv):
  94. res+='<'
  95. if plus:
  96. res+='+'
  97. else:
  98. res+='-'
  99. for _ in range(mv):
  100. res+='>'
  101. res+='-]'
  102. for _ in range(mv-1):
  103. res+='<-]'
  104. res+='<'
  105. else:
  106. for foo in range(primes[0]):
  107. if plus:
  108. res+='+'
  109. else:
  110. res+='-'
  111. #Correcting the optimisation add 1 or sub 1
  112. if plus:
  113. if p1:
  114. res+='-'
  115. elif m1:
  116. res+='+'
  117. else:
  118. if p1:
  119. res+='+'
  120. elif m1:
  121. res+='-'
  122. return res
  123. #Prime factor decomposition
  124. def primeFact(n):
  125. nc = n
  126. res = []
  127. for i in PRIMES:
  128. if nc == 0:
  129. return res
  130. while nc%i == 0:
  131. #optimization
  132. if len(res) > 0 and res[-1] * i < OPTIPRIMEMAX:
  133. res[-1]*=i
  134. else:
  135. res.append(i)
  136. nc/=i
  137. return res
  138. def main():
  139. if (len(sys.argv) == 2):
  140. print BrainfuckGenerator(sys.argv[1])
  141. else:
  142. bg = BrainfuckGenerator()
  143. for line in sys.stdin:
  144. sys.stdout.write(bg.next(line))
  145. sys.stdout.flush()
  146. if __name__ == "__main__":
  147. main()