New Terminal Output:

root@f3f8d6319494:/app# python3 -c "
> import struct
>
> 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 per-row change density across all frames
> row_change_counts = [0] * h
> for i in range(1, n_frames):
>     for r in range(h):
>         if frames[i-1][r*w:(r+1)*w] != frames[i][r*w:(r+1)*w]:
>             row_change_counts[r] += 1
>
> print(f'Rows most often changed:')
> for r in sorted(range(h), key=lambda x: -row_change_counts[x])[:10]:
>     print(f'  Row {r}: {row_change_counts[r]} times')
>
> # Calculate optimal strategy cost
> # Strategy: For each changed row, use ESC[r;1H then either:
> #   - ESC[2K + rewrite (4 + w bytes)
> #   - or partial updates with runs
>
> def pos_cost(r, c):
>     return 3 + len(str(r)) + 1 + len(str(c))  # ESC[r;cH
>
> total_cost = 0
>
> # First frame: write all non-space using runs
> frame0 = frames[0]
> for r in range(h):
>     row = frame0[r*w:(r+1)*w]
>     # Find runs of non-space
>     in_run = False
>     run_start = 0
>     for c in range(w):
>         if row[c] != 32:
>             if not in_run:
>                 in_run = True
>                 run_start = c
>         else:
>             if in_run:
>                 in_run = False
>                 run_len = c - run_start
>                 total_cost += pos_cost(r+1, run_start+1) + run_len
>     if in_run:
>         run_len = w - run_start
>         total_cost += pos_cost(r+1, run_start+1) + run_len
> total_cost += 1  # delimiter
>
> print(f'Frame 0 cost: {total_cost}')
>
> # Subsequent frames
> for i in range(1, n_frames):
>     frame_cost = 0
>     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
>
>         # Count changes and find runs
>         changes = [c for c in range(w) if prev_row[c] != curr_row[c]]
>         if not changes:
>             continue
>
>         # Find runs
>         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))
>
>         # Cost: position + run content for each run
>         for start, end in runs:
>             frame_cost += pos_cost(r+1, start+1) + (end - start + 1)
>
>     total_cost += frame_cost + 1
>
> print(f'Total cost: {total_cost}')
> print(f'Budget: {max_bytes}')
> print(f'Over/under: {total_cost - max_bytes}')
> "
Rows most often changed:
  Row 1: 1199 times
  Row 2: 299 times
  Row 3: 299 times
  Row 4: 299 times
  Row 5: 299 times
  Row 6: 299 times
  Row 7: 299 times
  Row 8: 299 times
  Row 9: 299 times
  Row 10: 299 times
Frame 0 cost: 1922
Total cost: 302456
Budget: 69000
Over/under: 233456
root@f3f8d6319494:/app#
