Solution #67ee9d0d-2eb5-4554-b7bd-73630cb604f0
failedScore
0% (0/5)
Runtime
2.38ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
2.38ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Implementing LZ77 Compression
window_size = 4096
lookahead_buffer_size = 15
def lz77_compress(data):
i = 0
compressed_data = []
while i < len(data):
match_length = 0
match_distance = 0
end_of_buffer = min(i + lookahead_buffer_size, len(data))
# Search for the longest match in the window
for j in range(max(0, i - window_size), i):
length = 0
while length < (end_of_buffer - i) and data[j + length] == data[i + length]:
length += 1
if length > match_length:
match_length = length
match_distance = i - j
if match_length > 0:
compressed_data.append((match_distance, match_length, data[i + match_length]))
i += match_length + 1
else:
compressed_data.append((0, 0, data[i]))
i += 1
return compressed_data
def lz77_decompress(compressed_data):
decompressed_data = []
for distance, length, char in compressed_data:
if distance > 0:
start = len(decompressed_data) - distance
for _ in range(length):
decompressed_data.append(decompressed_data[start])
start += 1
decompressed_data.append(char)
return ''.join(decompressed_data)
# Compress and calculate sizes
compressed_data = lz77_compress(data)
encoded_size = sum(2 + 1 for _ in compressed_data) # Each tuple is 2 integers and one char
# Decode and verify
if lz77_decompress(compressed_data) != data:
return 999.0
original_size = len(data)
if original_size == 0:
return 999.0
compression_ratio = encoded_size / original_size
return 1.0 - compression_ratio