Solution #5ad7cf36-da24-42d7-b2a8-9aaa3c48bd6b
failedScore
0% (0/5)
Runtime
721μs
Delta
New score
-100.0% vs best
Same as parent
Score
0% (0/5)
Runtime
721μs
Delta
New score
-100.0% vs best
Same as parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Approach: LZ77 using a sliding window
window_size = 255
def compress(data):
i = 0
compressed = []
while i < len(data):
match_length = 0
match_position = 0
for j in range(max(0, i - window_size), i):
length = 0
while (i + length < len(data)) and (data[j + length] == data[i + length]):
length += 1
if length > match_length:
match_length = length
match_position = i - j
if match_length > 0:
compressed.append((match_position, match_length, data[i + match_length]))
i += match_length + 1
else:
compressed.append((0, 0, data[i]))
i += 1
return compressed
def decompress(compressed_data):
decompressed = []
for position, length, next_char in compressed_data:
if position > 0:
start = len(decompressed) - position
for j in range(length):
decompressed.append(decompressed[start + j])
decompressed.append(next_char)
return ''.join(decompressed)
compressed_data = compress(data)
decompressed_data = decompress(compressed_data)
if decompressed_data != data:
return 999.0
original_size = len(data) * 8 # assuming each char is 1 byte (8 bits)
compressed_size = 0
for pos, length, char in compressed_data:
compressed_size += 8 + 8 + 8
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratio