New Terminal Output:

root@f3f8d6319494:/app# python3 << 'EOF'
> import struct
  curr_row = frames[i][r*w:(r+1)*w]
        changes = [(c, prev_row[c], curr_row[c]) for c in range(w) if prev_row[c] != curr_row[c]]

        # Find runs
        runs = []
        if changes:
            run_start = changes[0][0]
            run_end = changes[0][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 of EL + rewrite
        cost_erase = cost_move(cursor_r, cursor_c, r+1, 1) + 4 + w

        # Cost of partial
        cost_partial = 0
        for start, end in runs:
            cost_partial += cost_move(cursor_r, cursor_c, r+1, start+1)
            cursor_r, cursor_c = r+1, end+2
        cost_partial += len(changes)

        if cost_erase <= cost_partial:
            total_cost += cost_move(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

print(f'Total: {total_cost}, Budget: {max_bytes}, Diff: {total_cost - max_bytes}')
EOF
>
> 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 cost_move(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 move + 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
>
> def abs_pos_cost(r, c):
>     return 3 + len(str(r)) + 1 + len(str(c))
>
> total_cost = 0
>
> # Frame 0: write non-space using runs
> frame0 = frames[0]
> cursor_r, cursor_c = 1, 1
> for r in range(h):
>     row = frame0[r*w:(r+1)*w]
>     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 += cost_move(cursor_r, cursor_c, r+1, run_start+1) + run_len
>                 cursor_r, cursor_c = r+1, run_start+run_len+1
>     if in_run:
>         run_len = w - run_start
>         total_cost += cost_move(cursor_r, cursor_c, r+1, run_start+1) + run_len
>         cursor_r, cursor_c = r+1, run_start+run_len+1
> total_cost += 1
> print(f'Frame 0 cost: {total_cost}')
>
> # Subsequent frames
> cursor_r, cursor_c = 24, 1  # End of frame 0
> for i in range(1, n_frames):
>     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
>         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, prev_row[c], curr_row[c]) for c in range(w) if prev_row[c] != curr_row[c]]
>
>         # Find runs
>         runs = []
>         if changes:
>             run_start = changes[0][0]
>             run_end = changes[0][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 of EL + rewrite
>         cost_erase = cost_move(cursor_r, cursor_c, r+1, 1) + 4 + w
>
>         # Cost of partial
>         cost_partial = 0
>         for start, end in runs:
>             cost_partial += cost_move(cursor_r, cursor_c, r+1, start+1)
>             cursor_r, cursor_c = r+1, end+2
>         cost_partial += len(changes)
>
>         if cost_erase <= cost_partial:
>             total_cost += cost_move(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
>
> print(f'Total: {total_cost}, Budget: {max_bytes}, Diff: {total_cost - max_bytes}')
> EOF
Frame 0 cost: 1620
Total: 294220, Budget: 69000, Diff: 225220
root@f3f8d6319494:/app#
