Advertisement
bf17

BF compress 50%

Apr 14th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. # python 3
  2.  
  3. # compress brainfuck program by encoding each two-command group into a single character
  4. # programs with an odd number of commands will have a '.' prepended to the front
  5. # results in 50% compression
  6.  
  7. counter = h = v = 0
  8. prog = comp = ""
  9. cs = "><+-,.[]" # command set
  10. load_list = "0@123456!789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11. # '><' = '@', '<>' = '!' should not happen very often
  12.  
  13. f = open(input("bf file: "), 'r')
  14.  
  15. while True:
  16.    nextch = f.read(1)
  17.    if(not nextch):
  18.        f.close()
  19.        break
  20.    if(nextch in cs):
  21.        prog += (nextch)
  22.  
  23. if(len(prog) % 2 == 1):
  24.     prog = '.' + prog # make program list even
  25.  
  26. print()
  27. for i in range(0, int(len(prog)), 2):
  28.     comp += load_list[cs.find(prog[i]) * 8 +
  29.                     cs.find(prog[i + 1])]
  30.  
  31. print(comp)
  32. '''
  33. H/V >   <   +   -   ,   .   [   ]
  34. >   0  (!)  e   m   u   C   K   S
  35. <  (@)  7   f   n   v   D   L   T
  36. +   1   8   g  (o)  w   E   M   U
  37. -   2   9  (h)  p   x   F   N   V
  38. ,   3   a   i   q  (y)  G   O   W
  39. .   4   b   j   r   z   H   P   X
  40. [   5   c   k   s   A   I   Q   Y
  41. ]   6   d   l   t   B   J  (R)  Z
  42. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement