New Terminal Output:

root@f3f8d6319494:/app# python3 << 'PYEOF'
> import struct
> from collections import Counter, 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)]
>
> # Analyze changes per row across all frames
> row_change_stats = defaultdict(list)  # row -> list of change counts
>
> for i in range(1, n_frames):
>     for r in range(h):
>         prev_row = frames[i-1][r*w:(r+1)*w]
>         curr_row = frames[i][r*w:(r+1)*w]
>         if prev_row != curr_row:
>             changes = sum(1 for c in range(w) if prev_row[c] != curr_row[c])
>             row_change_stats[r].append(changes)
>
> print('Row change statistics:')
> for r in sorted(row_change_stats.keys()):
>     counts = row_change_stats[r]
>     print(f'  Row {r}: {len(counts)} changes, min={min(counts)}, max={max(counts)}, avg={sum(counts)/len(counts):.1f}')
>
> # Calculate optimal cost with threshold
> def pos_bytes(r, c):
>     return 3 + len(str(r)) + 1 + len(str(c))
>
> 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 for frame 0 (non-space only)
> frame0_cost = 0
> 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:
>         frame0_cost += pos_bytes(r+1, start+1) + (end - start + 1)
> frame0_cost += 1
> print(f'\nFrame 0 cost: {frame0_cost}')
>
> # Calculate cost for subsequent frames with optimal strategy per row
> rest_cost = 0
> for i in range(1, n_frames):
>     for r in range(h):
>         prev_row = frames[i-1][r*w:(r+1)*w]
>         curr_row = frames[i][r*w:(r+1)*w]
>         if prev_row == curr_row:
>             continue
>         changes = [c for c in range(w) if prev_row[c] != curr_row[c]]
>         runs = find_runs(changes)
>
>         # Cost of partial updates
>         partial_cost = sum(pos_bytes(r+1, start+1) + (end-start+1) for start, end in runs)
>
>         # Cost of EL + rewrite
>         erase_cost = pos_bytes(r+1, 1) + 4 + w  # ESC[r;1H + ESC[2K + row
>
>         # Choose cheaper
>         rest_cost += min(partial_cost, erase_cost)
>     rest_cost += 1  # delimiter
>
> print(f'Frames 1+ cost (optimal per row): {rest_cost}')
> print(f'Total: {frame0_cost + rest_cost}')
> print(f'Budget: {max_bytes}')
> print(f'Diff: {frame0_cost + rest_cost - max_bytes}')
> PYEOF
Row change statistics:
  Row 1: 1199 changes, min=4, max=36, avg=6.3
  Row 2: 299 changes, min=10, max=38, avg=30.6
  Row 3: 299 changes, min=14, max=38, avg=30.5
  Row 4: 299 changes, min=14, max=38, avg=30.4
  Row 5: 299 changes, min=14, max=38, avg=30.4
  Row 6: 299 changes, min=14, max=38, avg=30.4
  Row 7: 299 changes, min=14, max=38, avg=30.5
  Row 8: 299 changes, min=14, max=38, avg=30.5
  Row 9: 299 changes, min=14, max=38, avg=30.3
  Row 10: 299 changes, min=14, max=38, avg=30.6
  Row 11: 299 changes, min=14, max=38, avg=30.5
  Row 13: 171 changes, min=14, max=45, avg=33.1
  Row 14: 171 changes, min=14, max=45, avg=33.1
  Row 15: 171 changes, min=18, max=45, avg=33.3
  Row 16: 171 changes, min=18, max=45, avg=33.3
  Row 17: 171 changes, min=18, max=45, avg=33.4
  Row 18: 171 changes, min=18, max=45, avg=33.4
  Row 19: 171 changes, min=18, max=45, avg=33.4
  Row 20: 171 changes, min=18, max=45, avg=33.4
  Row 21: 171 changes, min=18, max=45, avg=33.5
  Row 22: 196 changes, min=18, max=45, avg=33.5

Frame 0 cost: 1922
Frames 1+ cost (optimal per row): 300526
Total: 302448
Budget: 69000
Diff: 233448
root@f3f8d6319494:/app#
