Solution #b96fbcb3-46f6-4973-a8ae-b66cf6ae46d5
completedScore
40% (0/5)
Runtime
1.30ms
Delta
+103.3% vs parent
-58.8% vs best
Improved from parent
Score
40% (0/5)
Runtime
1.30ms
Delta
+103.3% vs parent
-58.8% vs best
Improved from parent
def solve(input):
data = input["data"]
n = len(data)
if n == 0:
return 0.0
tokens = []
i = 0
window = 255
max_len = 255
while i < n:
best_len = 0
best_dist = 0
start = i - window
if start < 0:
start = 0
j = i - 1
while j >= start:
l = 0
while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i:
l += 1
if l > best_len and l >= 3:
best_len = l
best_dist = i - j
if best_len == max_len:
break
j -= 1
if best_len >= 3:
tokens.append((1, best_dist, best_len))
i += best_len
else:
literals = [data[i]]
i += 1
while i < n:
best_len2 = 0
start2 = i - window
if start2 < 0:
start2 = 0
j = i - 1
while j >= start2:
l = 0
while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i:
l += 1
if l > best_len2 and l >= 3:
best_len2 = l
break
j -= 1
if best_len2 >= 3 or len(literals) >= 255:
break
literals.append(data[i])
i += 1
tokens.append((0, ''.join(literals)))
out = []
for t in tokens:
if t[0] == 0:
s = t[1]
out.append(chr(0))
out.append(chr(len(s)))
out.append(s)
else:
out.append(chr(1))
out.append(chr(t[1]))
out.append(chr(t[2]))
compressed = ''.join(out)
decomp = []
idx = 0
try:
while idx < len(compressed):
typ = ord(compressed[idx])
idx += 1
if typ == 0:
ln = ord(compressed[idx])
idx += 1
s = compressed[idx:idx + ln]
if len(s) != ln:
return 999.0
decomp.append(s)
idx += ln
elif typ == 1:
dist = ord(compressed[idx])
ln = ord(compressed[idx + 1])
idx += 2
built = ''.join(decomp)
if dist <= 0 or dist > len(built):
return 999.0
start = len(built) - dist
chunk = []
for _ in range(ln):
built2 = built + ''.join(chunk)
pos = start + len(chunk)
if pos >= len(built2):
return 999.0
chunk.append(built2[pos])
decomp.append(''.join(chunk))
else:
return 999.0
except:
return 999.0
decompressed = ''.join(decomp)
if decompressed != data:
return 999.0
return len(compressed) / nScore Difference
-56.8%
Runtime Advantage
1.17ms slower
Code Size
110 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | data = input["data"] | 2 | data = input.get("data", "") |
| 3 | n = len(data) | 3 | if not isinstance(data, str) or not data: |
| 4 | if n == 0: | 4 | return 999.0 |
| 5 | return 0.0 | 5 | |
| 6 | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation | |
| 7 | tokens = [] | 7 | |
| 8 | i = 0 | 8 | from collections import Counter |
| 9 | window = 255 | 9 | from math import log2 |
| 10 | max_len = 255 | 10 | |
| 11 | 11 | def entropy(s): | |
| 12 | while i < n: | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | best_len = 0 | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | best_dist = 0 | 14 | |
| 15 | 15 | def redundancy(s): | |
| 16 | start = i - window | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | if start < 0: | 17 | actual_entropy = entropy(s) |
| 18 | start = 0 | 18 | return max_entropy - actual_entropy |
| 19 | 19 | ||
| 20 | j = i - 1 | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | while j >= start: | 21 | reduction_potential = redundancy(data) |
| 22 | l = 0 | 22 | |
| 23 | while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i: | 23 | # Assuming compression is achieved based on redundancy |
| 24 | l += 1 | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | if l > best_len and l >= 3: | 25 | |
| 26 | best_len = l | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | best_dist = i - j | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | if best_len == max_len: | 28 | return 999.0 |
| 29 | break | 29 | |
| 30 | j -= 1 | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data | |
| 32 | if best_len >= 3: | 32 | |
| 33 | tokens.append((1, best_dist, best_len)) | 33 | # Returning the hypothetical compression performance |
| 34 | i += best_len | 34 | return max_possible_compression_ratio |
| 35 | else: | 35 | |
| 36 | literals = [data[i]] | 36 | |
| 37 | i += 1 | 37 | |
| 38 | while i < n: | 38 | |
| 39 | best_len2 = 0 | 39 | |
| 40 | start2 = i - window | 40 | |
| 41 | if start2 < 0: | 41 | |
| 42 | start2 = 0 | 42 | |
| 43 | j = i - 1 | 43 | |
| 44 | while j >= start2: | 44 | |
| 45 | l = 0 | 45 | |
| 46 | while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i: | 46 | |
| 47 | l += 1 | 47 | |
| 48 | if l > best_len2 and l >= 3: | 48 | |
| 49 | best_len2 = l | 49 | |
| 50 | break | 50 | |
| 51 | j -= 1 | 51 | |
| 52 | if best_len2 >= 3 or len(literals) >= 255: | 52 | |
| 53 | break | 53 | |
| 54 | literals.append(data[i]) | 54 | |
| 55 | i += 1 | 55 | |
| 56 | tokens.append((0, ''.join(literals))) | 56 | |
| 57 | 57 | ||
| 58 | out = [] | 58 | |
| 59 | for t in tokens: | 59 | |
| 60 | if t[0] == 0: | 60 | |
| 61 | s = t[1] | 61 | |
| 62 | out.append(chr(0)) | 62 | |
| 63 | out.append(chr(len(s))) | 63 | |
| 64 | out.append(s) | 64 | |
| 65 | else: | 65 | |
| 66 | out.append(chr(1)) | 66 | |
| 67 | out.append(chr(t[1])) | 67 | |
| 68 | out.append(chr(t[2])) | 68 | |
| 69 | compressed = ''.join(out) | 69 | |
| 70 | 70 | ||
| 71 | decomp = [] | 71 | |
| 72 | idx = 0 | 72 | |
| 73 | try: | 73 | |
| 74 | while idx < len(compressed): | 74 | |
| 75 | typ = ord(compressed[idx]) | 75 | |
| 76 | idx += 1 | 76 | |
| 77 | if typ == 0: | 77 | |
| 78 | ln = ord(compressed[idx]) | 78 | |
| 79 | idx += 1 | 79 | |
| 80 | s = compressed[idx:idx + ln] | 80 | |
| 81 | if len(s) != ln: | 81 | |
| 82 | return 999.0 | 82 | |
| 83 | decomp.append(s) | 83 | |
| 84 | idx += ln | 84 | |
| 85 | elif typ == 1: | 85 | |
| 86 | dist = ord(compressed[idx]) | 86 | |
| 87 | ln = ord(compressed[idx + 1]) | 87 | |
| 88 | idx += 2 | 88 | |
| 89 | built = ''.join(decomp) | 89 | |
| 90 | if dist <= 0 or dist > len(built): | 90 | |
| 91 | return 999.0 | 91 | |
| 92 | start = len(built) - dist | 92 | |
| 93 | chunk = [] | 93 | |
| 94 | for _ in range(ln): | 94 | |
| 95 | built2 = built + ''.join(chunk) | 95 | |
| 96 | pos = start + len(chunk) | 96 | |
| 97 | if pos >= len(built2): | 97 | |
| 98 | return 999.0 | 98 | |
| 99 | chunk.append(built2[pos]) | 99 | |
| 100 | decomp.append(''.join(chunk)) | 100 | |
| 101 | else: | 101 | |
| 102 | return 999.0 | 102 | |
| 103 | except: | 103 | |
| 104 | return 999.0 | 104 | |
| 105 | 105 | ||
| 106 | decompressed = ''.join(decomp) | 106 | |
| 107 | if decompressed != data: | 107 | |
| 108 | return 999.0 | 108 | |
| 109 | 109 | ||
| 110 | return len(compressed) / n | 110 |
1def solve(input):2 data = input["data"]3 n = len(data)4 if n == 0:5 return 0.067 tokens = []8 i = 09 window = 25510 max_len = 2551112 while i < n:13 best_len = 014 best_dist = 01516 start = i - window17 if start < 0:18 start = 01920 j = i - 121 while j >= start:22 l = 023 while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i:24 l += 125 if l > best_len and l >= 3:26 best_len = l27 best_dist = i - j28 if best_len == max_len:29 break30 j -= 13132 if best_len >= 3:33 tokens.append((1, best_dist, best_len))34 i += best_len35 else:36 literals = [data[i]]37 i += 138 while i < n:39 best_len2 = 040 start2 = i - window41 if start2 < 0:42 start2 = 043 j = i - 144 while j >= start2:45 l = 046 while i + l < n and data[j + l] == data[i + l] and l < max_len and j + l < i:47 l += 148 if l > best_len2 and l >= 3:49 best_len2 = l50 break51 j -= 152 if best_len2 >= 3 or len(literals) >= 255:53 break54 literals.append(data[i])55 i += 156 tokens.append((0, ''.join(literals)))5758 out = []59 for t in tokens:60 if t[0] == 0:61 s = t[1]62 out.append(chr(0))63 out.append(chr(len(s)))64 out.append(s)65 else:66 out.append(chr(1))67 out.append(chr(t[1]))68 out.append(chr(t[2]))69 compressed = ''.join(out)7071 decomp = []72 idx = 073 try:74 while idx < len(compressed):75 typ = ord(compressed[idx])76 idx += 177 if typ == 0:78 ln = ord(compressed[idx])79 idx += 180 s = compressed[idx:idx + ln]81 if len(s) != ln:82 return 999.083 decomp.append(s)84 idx += ln85 elif typ == 1:86 dist = ord(compressed[idx])87 ln = ord(compressed[idx + 1])88 idx += 289 built = ''.join(decomp)90 if dist <= 0 or dist > len(built):91 return 999.092 start = len(built) - dist93 chunk = []94 for _ in range(ln):95 built2 = built + ''.join(chunk)96 pos = start + len(chunk)97 if pos >= len(built2):98 return 999.099 chunk.append(built2[pos])100 decomp.append(''.join(chunk))101 else:102 return 999.0103 except:104 return 999.0105106 decompressed = ''.join(decomp)107 if decompressed != data:108 return 999.0109110 return len(compressed) / n1def solve(input):2 data = input.get("data", "")3 if not isinstance(data, str) or not data:4 return 999.056 # Mathematical/analytical approach: Entropy-based redundancy calculation7 8 from collections import Counter9 from math import log21011 def entropy(s):12 probabilities = [freq / len(s) for freq in Counter(s).values()]13 return -sum(p * log2(p) if p > 0 else 0 for p in probabilities)1415 def redundancy(s):16 max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 017 actual_entropy = entropy(s)18 return max_entropy - actual_entropy1920 # Calculate reduction in size possible based on redundancy21 reduction_potential = redundancy(data)2223 # Assuming compression is achieved based on redundancy24 max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data)))25 26 # Qualitative check if max_possible_compression_ratio makes sense27 if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0:28 return 999.02930 # Verify compression is lossless (hypothetical check here)31 # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data32 33 # Returning the hypothetical compression performance34 return max_possible_compression_ratio