bf17 icon

BF compress 50%

bf17 | PRO | 04/15/19 02:39:15 AM UTC | 0 ⭐ | 451 👁️ | Never ⏰ | []
Python |

1.01 KB

|

None

|

0 👍

/

0 👎

# python 3
 
# compress brainfuck program by encoding each two-command group into a single character
# programs with an odd number of commands will have a '.' prepended to the front
# results in 50% compression
 
counter = h = v = 0
prog = comp = ""
cs = "><+-,.[]" # command set
load_list = "0@123456!789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# '><' = '@', '<>' = '!' should not happen very often
 
f = open(input("bf file: "), 'r')
 
while True:
   nextch = f.read(1)
   if(not nextch):
       f.close()
       break
   if(nextch in cs):
       prog += (nextch)
 
if(len(prog) % 2 == 1):
    prog = '.' + prog # make program list even
 
print()
for i in range(0, int(len(prog)), 2):
    comp += load_list[cs.find(prog[i]) * 8 + 
                    cs.find(prog[i + 1])]
 
print(comp)
'''
H/V >   <   +   -   ,   .   [   ]
>   0  (!)  e   m   u   C   K   S
<  (@)  7   f   n   v   D   L   T
+   1   8   g  (o)  w   E   M   U
-   2   9  (h)  p   x   F   N   V
,   3   a   i   q  (y)  G   O   W
.   4   b   j   r   z   H   P   X
[   5   c   k   s   A   I   Q   Y
]   6   d   l   t   B   J  (R)  Z
'''

Comments