Created
April 28, 2020 22:56
-
-
Save cosinekitty/815ce0fd1c1b59b706349cd5961ec053 to your computer and use it in GitHub Desktop.
Second decompressor, using common prefix counts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| Repeat=((5,4),((((10,(11,(1,((((15,((((18,19),17),16),0)),14),13),12)))),8),6),(3,((9,2),7)))) | |
| Tail=(((5,(6,(7,(8,(((((((16,(17,((20,23),(15,18)))),14),13),12),11),10),9))))),2),(1,(4,3))) | |
| Char=(((('d',(('k',('w','v')),'g')),'i'),((('m',('b','p')),'r'),('l','o'))),(('e',('t',('u','c'))),((((('f',('z',(('j','q'),'x'))),'h'),'y'),'s'),('n','a')))) | |
| NumWords=67527 | |
| Bits=r''' | |
| jjvjG9QF+ojhTtgjSXb83952mERR72W27AD9nBWMxnfU5y/PfpghOLNuB+9gfkTqx5WjezfRfs7CAyJ1 | |
| NiMAyJ1KfK+ahkTqeeGWuwx7K+jp3K7Yc+3VL1vl1H2vv7W3UXdbOXrGWCz9SEHOX7nss7XxPZnQl3h7 | |
| YRTUxG/pNK9Van8v3rq6nXNt2dE4x728+951TTsjbunI+zLbYbpuyYXNN/be15XqKry/ei5V1756MdeB | |
| ... | |
| ojPfzvU250ZPp3Eni6R+9bafNRM38JT9hqbrMasfT0690Z71LYn/ai/lP9FzqOjai/+vPndbxc34N7s6 | |
| fjVule+J49Wt3Kb4xjyKhitCOL0jEvuJht8Z0Ysj6I/yr7z5ztNxnv96tqQfessqPCdU8TcLN5U3+9en | |
| qeOWb7z5xsg1RfWpXoJv3I/Zs+nX0b7WUeQ | |
| ''' | |
| class BitReader: | |
| def __init__(self, encoded): | |
| self.encoded = encoded.replace('\n', '') | |
| self.position = 0 | |
| self.accum = 0 | |
| self.nbits = 0 | |
| def GetNextBit(self): | |
| if self.nbits == 0: | |
| c = self.encoded[self.position] | |
| self.position += 1 | |
| self.accum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.index(c) | |
| self.nbits = 6 | |
| bit = 1 & (self.accum >> 5) | |
| self.accum = 0b111111 & (self.accum << 1) | |
| self.nbits -= 1 | |
| return bit | |
| def Huffman(reader, node): | |
| while isinstance(node, tuple): | |
| node = node[reader.GetNextBit()] | |
| return node | |
| def Expand(): | |
| reader = BitReader(Bits) | |
| pw = '' | |
| wlist = [] | |
| for w in range(NumWords): | |
| repeatLen = Huffman(reader, Repeat) | |
| tailLen = Huffman(reader, Tail) | |
| w = pw[:repeatLen] + ''.join(Huffman(reader, Char) for _ in range(tailLen)) | |
| wlist.append(w) | |
| pw = w | |
| return '\n'.join(wlist) | |
| if __name__ == "__main__": | |
| print(Expand()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment