Solution #a9d69e70-aab6-4d21-8583-d7d5fa8d7f33
failedScore
0% (0/5)
Runtime
0μs
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
0μs
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
def solve(input):
try:
data = input.get("data", "") if isinstance(input, dict) else ""
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
def rle_encode(s):
if not s:
return []
idx = list(range(len(s)))
breaks = [0] + [i for i in idx[1:] if s[i] != s[i - 1]] + [len(s)]
return [(s[breaks[k]], breaks[k + 1] - breaks[k]) for k in range(len(breaks) - 1)]
def rle_decode(runs):
return "".join(ch * cnt for ch, cnt in runs)
def periodic_prefix_len(s, start):
rem = len(s) - start
if rem < 2:
return 1, 1
max_unit = min(64, rem // 2)
units = [u for u in range(1, max_unit + 1) if start + 2 * u <= len(s) and s[start:start + u] == s[start + u:start + 2 * u]]
if not units:
return 1, 1
def reps_for(u):
base = s[start:start + u]
max_rep = min(256, rem // u)
rep = 2
while rep < max_rep and s[start + rep * u:start + (rep + 1) * u] == base:
rep += 1
return rep
cands = [(u, reps_for(u)) for u in units]
best = min(cands, key=lambda t: (t[0] + len(str(t[1])), -(t[0] * t[1])))
return best
def periodic_encode(s):
pos = 0
out = []
while pos < len(s):
u, r = periodic_prefix_len(s, pos)
if r >= 3 and u * r >= max(6, u + 3):
out.append(("R", s[pos:pos + u], r))
pos += u * r
else:
out.append(("L", s[pos]))
pos += 1
merged = []
for tok in out:
if tok[0] == "L" and merged and merged[-1][0] == "L":
merged[-1] = ("L", merged[-1][1] + tok[1])
else:
merged.append(tok)
return merged
def periodic_decode(tokens):
return "".join((t[1] * t[2]) if t[0] == "R" else t[1] for t in tokens)
def lz_encode(s):
pos = 0
out = []
window = 1024
min_match = 4
while pos < len(s):
starts = range(max(0, pos - window), pos)
best = max(
(((lambda j: (lambda l=0: (lambda:
(lambda ll:
(ll, pos - j)
)(
(lambda jj=j, pp=pos:
(lambda:
(lambda l2=0:
(lambda:
[None for _ in iter(int, 1)]
)()
)()
)()
)()
)
)())()) for j in starts),
default=(0, 0),
key=lambda x: x[0]
)
if best[0] >= min_match:
j = pos - best[1]
l = 0
while pos + l < len(s) and s[j + l] == s[pos + l]:
l += 1
if j + l >= pos:
break
if l >= min_match:
out.append(("M", best[1], l))
pos += l
continue
out.append(("L", s[pos]))
pos += 1
merged = []
for tok in out:
if tok[0] == "L" and merged and merged[-1][0] == "L":
merged[-1] = ("L", merged[-1][1] + tok[1])
else:
merged.append(tok)
return merged
def lz_decode(tokens):
built = []
for t in tokens:
if t[0] == "L":
built.append(t[1])
else:
dist, ln = t[1], t[2]
cur = "".join(built)
if dist <= 0 or dist > len(cur):
return None
start = len(cur) - dist
seg = []
for k in range(ln):
idx = start + k
seg.append(cur[idx] if idx < len(cur) else seg[idx - len(cur)])
built.append("".join(seg))
return "".join(built)
def size_raw(s):
return len(s)
def size_rle(runs):
return sum(1 + len(str(cnt)) for ch, cnt in runs)
def size_periodic(tokens):
return sum((2 + len(unit) + len(str(rep))) if typ == "R" else len(val) for typ, *rest in tokens for val, unit, rep in [((rest[0] if typ == "L" else ""), (rest[0] if typ == "R" else ""), (rest[1] if typ == "R" else 0))])
def size_lz(tokens):
return sum((2 + len(str(dist)) + len(str(ln))) if typ == "M" else len(val) for typ, *rest in tokens for val, dist, ln in [((rest[0] if typ == "L" else ""), (rest[0] if typ == "M" else 0), (rest[1] if typ == "M" else 0))])
runs = rle_encode(data)
if rle_decode(runs) != data:
return 999.0
ptoks = periodic_encode(data)
if periodic_decode(ptoks) != data:
return 999.0
ltoks = lz_encode(data)
def safe_lz_decode(tokens):
built = ""
for t in tokens:
if t[0] == "L":
built += t[1]
else:
dist, ln = t[1], t[2]
if dist <= 0 or dist > len(built):
return None
start = len(built) - dist
chunk = []
for k in range(ln):
idx = start + k
chunk.append(built[idx] if idx < len(built) else chunk[idx - len(built)])
built += "".join(chunk)
return built
if safe_lz_decode(ltoks) != data:
return 999.0
compressed = min(
size_raw(data),
size_rle(runs),
size_periodic(ptoks),
size_lz(ltoks),
)
ratio = compressed / float(n)
return float(max(0.0, ratio))
except:
return 999.0