New Terminal Output:

root@f3f8d6319494:/app# python3 << 'PYEOF'
> import struct
> from collections import defaultdict
>
> with open('/app/frames.tza', 'rb') as f:
>     f.read(4)
>     w, h = struct.unpack('<HH', f.read(4))
>     n_frames, max_bytes = struct.unpack('<II', f.read(8))
>     frames = [f.read(w*h) for _ in range(n_frames)]
>
> def find_runs(changes):
>     runs = []
>     if not changes:
>         return runs
>     run_start = changes[0]
>     run_end = changes[0]
>     for c in changes[1:]:
>         if c == run_end + 1:
>             run_end = c
>         else:
>             runs.append((run_start, run_end))
>             run_start = run_end = c
>     runs.append((run_start, run_end))
>     return runs
>
> # Calculate cost using relative movements
> def rel_move_cost(from_r, from_c, to_r, to_c):
>     if from_r == to_r and from_c == to_c:
>         return 0
>     if from_r == to_r:
>         dc = to_c - from_c
>         if dc > 0:
>             return 4 + len(str(dc))  # ESC[nC
>         else:
>             return 4 + len(str(-dc))  # ESC[nD
>     # Different rows: CR + vertical + horizontal
>     cost = 1  # CR
>     dr = to_r - from_r
>     if dr > 0:
>         cost += 4 + len(str(dr))  # ESC[nB
>     elif dr < 0:
>         cost += 4 + len(str(-dr))  # ESC[nA
>     if to_c > 1:
>         cost += 4 + len(str(to_c - 1))  # ESC[nC from col 1
>     return cost
>
> # Frame 0: write non-space runs
> total_cost = 0
> cursor_r, cursor_c = 1, 1
> for r in range(h):
>     row = frames[0][r*w:(r+1)*w]
>     non_space = [c for c in range(w) if row[c] != 32]
>     runs = find_runs(non_space)
>     for start, end in runs:
>         total_cost += rel_move_cost(cursor_r, cursor_c, r+1, start+1) + (end - start + 1)
>         cursor_r, cursor_c = r+1, end+2  # after writing
> total_cost += 1  # delimiter
> print(f'Frame 0 cost: {total_cost}')
>
> # Subsequent frames
> for i in range(1, n_frames):
>     # Find changed rows
>     changed_rows = [r for r in range(h) if frames[i-1][r*w:(r+1)*w] != frames[i][r*w:(r+1)*w]]
>     if not changed_rows:
>         total_cost += 1  # just delimiter
>         continue
>
>     for r in changed_rows:
>         prev_row = frames[i-1][r*w:(r+1)*w]
>         curr_row = frames[i][r*w:(r+1)*w]
>         changes = [c for c in range(w) if prev_row[c] != curr_row[c]]
>         runs = find_runs(changes)
>
>         # Cost of EL + rewrite
>         cost_erase = rel_move_cost(cursor_r, cursor_c, r+1, 1) + 4 + w
>
>         # Cost of partial updates
>         cost_partial = 0
>         for start, end in runs:
>             cost_partial += rel_move_cost(cursor_r, cursor_c, r+1, start+1)
>             cursor_r, cursor_c = r+1, end+2
>         cost_partial += len(changes)
>
>         # Choose cheaper
>         if cost_erase <= cost_partial:
>             total_cost += rel_move_cost(cursor_r, cursor_c, r+1, 1) + 4 + w
>             cursor_r, cursor_c = r+1, w+1
>         else:
>             total_cost += cost_partial
>     total_cost += 1  # delimiter
>
> print(f'Total cost: {total_cost}')
> print(f'Budget: {max_bytes}')
> print(f'Diff: {total_cost - max_bytes}')
> PYEOF
Frame 0 cost: 1620
Total cost: 294220
Budget: 69000
Diff: 225220
root@f3f8d6319494:/app#
