input = (open('input.txt', 'r', encoding='ansi').read()) import math, copy de = 0 decode = str(int((open('compressed.txt', 'rb').read()), 2)) tree = ['', [], []] low = 1 high = 1 middle = 1 compressed = '' ggg = 0 for count2 in range(100000): window = input[count2: count2 + 15] if count2 > 306: ggg = 280 energy = input[count2 - ggg : count2 + 14] char_location = 15 remove = 2 char_index = 1 predictions = [[""" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+-=[]{}\|';":/.,?><€ƒ„†…‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝޝ ‚\t\nßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ""", [0.00001] * 226]] node = 1 while char_index != 0 and char_location != 0 and count2 != 0: if char_location != 15: char = window[char_location - 1] char_index = tree[node - 1].find(char) + 1 char_location = char_location + 1 if char_index != 0 and char_location == 16: predictions.insert(1, tree[node - 1: node + 1]) node = 1 char_location = char_location - remove remove = remove + 1 elif char_index != 0: node = tree[(node + 2 - 1)][char_index - 1] predict = ['', []] remaining = 1 for q in range(len(predictions)): j = [copy.copy(predictions[q][0]), copy.copy(predictions[q][1])] sum2 = sum(j[1]) w = sum2 / (226 if len(j[1]) == 226 else 7 * len(j[1]) * [0.72, 0.86, 0.95, 0.93, 0.97][len(j[1]) - 1] if len(j[-1]) < 6 else len(j[1]) * 7) w = 0.96 if w > 20 else 0.92 if w > 10 else 0.89 if w > 3 else 0.75 if w > 1.5 else 0.64 if w > 1 else 0.57 if w > 0.7 else 0.5 if w > 0.5 else 0.44 if w > 0.4 else w _25ofRoof = (w * [0.91, 0.88, 0.78, 0.96, 0.9, 0.83, 0.82, 0.54, 0.49, 0.25, 0.25, 0.27, 0.33, 0.53, 0.5, 0.53, 0.72][len(predictions) - 1 - q]) * remaining remaining = remaining - _25ofRoof minn = min(j[1]) maxx = 400 if q == (len(predictions) - 1) else 100 if q != 0 and (minn * maxx) < max(j[1]): for o in range(len(j[0])): freq = ((j[1][o] + 0.00001) - minn) / (minn * maxx) if q == (len(predictions) - 1): freq = 3.5 if freq > 4 else 2.1 if freq > 2 else freq else: freq = 0.87 if freq > 1.4 else 0.65 if freq > 1 else freq j[1][o] = ((freq * (minn * maxx)) + minn) sum2 = sum(j[1]) if q == (len(predictions) - 1): eng = ['', []] for n in range(len(energy)): y = 130 if n > 260 else 60 if n > 240 else 20 if n > 200 else 6 inflate = count2 / 8000 y = y * inflate eng_index = eng[0].find(energy[n]) + 1 if eng_index == 0: eng[0] = eng[0] + (energy[n]) eng[-1].append(y) else: eng[-1][eng_index - 1] = eng[-1][eng_index - 1] + y for c in range(len(eng[0])): j_index = j[0].find(eng[0][c]) + 1 if j_index != 0: j[1][j_index - 1] = j[1][j_index - 1] + eng[1][c] if q == (len(predictions) - 2) and len(j[0]) != 226: eng = ['', []] for n in range(len(energy) - 1): if energy[n] == window[13]: y = 130 if n > 260 else 60 if n > 240 else 20 if n > 200 else 6 inflate = count2 / 2000000 y = y * inflate eng_index = eng[0].find(energy[n + 1]) + 1 if eng_index == 0: eng[0] = eng[0] + (energy[n + 1]) eng[-1].append(y) else: eng[-1][eng_index - 1] = eng[-1][eng_index - 1] + y for c in range(len(eng[0])): j_index = j[0].find(eng[0][c]) + 1 if j_index != 0: j[1][j_index - 1] = j[1][j_index - 1] + eng[1][c] if q == (len(predictions) - 3) and len(j[0]) != 226: eng = ['', []] for n in range(len(energy) - 2): if energy[n:n+2] == window[12:14]: y = 130 if n > 260 else 60 if n > 240 else 20 if n > 200 else 6 inflate = count2 / 2500000 y = y * inflate eng_index = eng[0].find(energy[n + 2]) + 1 if eng_index == 0: eng[0] = eng[0] + (energy[n + 2]) eng[-1].append(y) else: eng[-1][eng_index - 1] = eng[-1][eng_index - 1] + y for c in range(len(eng[0])): j_index = j[0].find(eng[0][c]) + 1 if j_index != 0: j[1][j_index - 1] = j[1][j_index - 1] + eng[1][c] sum2 = sum(j[1]) for g in range(len(j[0])): k = j[0][g] freq = j[1][g] char_index = predict[0].find(k) + 1 if char_index == 0: predict[0] = predict[0] + k predict[-1].append((freq / sum2) * _25ofRoof) else: predict[-1][char_index - 1] = (predict[-1][char_index - 1] + ((freq / sum2) * _25ofRoof)) summ = 1 - sum(predict[-1]) for n in range(len(predict[-1])): predict[-1][n] = predict[-1][n] + summ / len(predict[-1]) low = high decodepart = float('0.' + str(decode[0+de:16+de])) for m in range(len(predict[0])): x = predict[0][m] high = low low = low - predict[-1][m] * middle if len(window) == 15 and x == window[-1]: break elif len(window) != 15 and high > decodepart and decodepart > low: window = window + x input = input + x break char_location = 0 while str(f'{low:.18f}')[char_location] == str(f'{high:.18f}')[char_location] and char_location != len(str(low)): char_location = char_location + 1 char_location = char_location - 1 compressed = str(compressed) + str(f'{low:.18f}')[2: char_location] char_location = char_location - 2 for count in range(char_location): high = high * 10 low = low * 10 de = de + 1 middle = math.floor(low) high = high - middle low = low - middle middle = high - low node = 1 for i in window: char_index = tree[node - 1].find(i) + 1 if char_index == 0: tree[node - 1] = tree[node - 1] + i tree[node + 1 - 1].append(1) tree[node + 2 - 1].append(len(tree) + 1) tree.extend(('', [], [])) node = len(tree) - 2 else: tree[(node + 1 - 1)][char_index - 1] = tree[(node + 1 - 1)][char_index - 1] + 1 node = tree[(node + 2 - 1)][char_index - 1] open('compressed.txt', 'wb').write(bytes(str(bin(int(compressed))), 'ansi')) open('decompressed.txt', 'wb').write(bytes(str(input), 'ansi')) print((len(bin(int(compressed))) - 2) / 8)