New Terminal Output:

root@f3f8d6319494:/app# python3 -c "
> import struct
        changed_rows.append(r)

    if not changed_rows:
        total_cost += 1  # just delimiter
        continue

    frame_cost = 0

    for r in changed_rows:
        # Analyze this row
        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 = 4 + w  # ESC[2K + rewrite

        # Cost of partial updates (from current cursor)
        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  # after writing run
        cost_partial += len(changes)

        # Choose cheaper option
        if cost_erase <= cost_partial:
            frame_cost += cost_move(cursor_r, cursor_c, r + 1, 1)  # move to start of row
            frame_cost += 4 + w  # ESC[2K + rewrite
            cursor_r, cursor_c = r + 1, w + 1
        else:
            frame_cost += cost_partial

    total_cost += frame_cost + 1  # + delimiter

print(f'Total cost: {total_cost}')
print(f'Budget: {max_bytes}')
print(f'Over/under: {total_cost - max_bytes}')
"
>
> 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: use CR + CUD/CUU + CUF
>     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
>     return cost
>
> 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 = []
>     for i in range(n_frames):
>         frames.append(f.read(w*h))
>
> # First frame: write non-space chars in row-major order
> total_cost = 0
> non_space_positions = [(j//w, j%w) for j in range(w*h) if frames[0][j] != 32]
> print(f'Frame 0 non-space: {len(non_space_positions)}')
>
> # Cost to write non-space chars
> if non_space_positions:
>     # Move to first non-space
>     total_cost += 0  # already at (1,1)
>     cursor_r, cursor_c = non_space_positions[0][0] + 1, non_space_positions[0][1] + 1
>
>     # Write all non-space chars
>     for r, c in non_space_positions:
>         if r == cursor_r - 1 and c == cursor_c:
>             # Already in position (contiguous)
>             cursor_c += 1
>         else:
>             total_cost += cost_move(cursor_r, cursor_c, r + 1, c + 1)
>             cursor_r, cursor_c = r + 1, c + 1
>             cursor_c += 1  # after writing
>     total_cost += len(non_space_positions) + 1  # chars + delimiter
> else:
>     total_cost += 1  # just delimiter
>
> print(f'Frame 0 cost: {total_cost}')
>
> # Calculate cost for all frames using line-based strategy
> cursor_r, cursor_c = h, 1  # After writing frame 0, assume cursor at end
>
> for i in range(1, n_frames):
>     changed_rows = []
>     for r in range(h):
>         if frames[i-1][r*w:(r+1)*w] != frames[i][r*w:(r+1)*w]:
>             changed_rows.append(r)
>
>     if not changed_rows:
>         total_cost += 1  # just delimiter
>         continue
>
>     frame_cost = 0
>
>     for r in changed_rows:
>         # Analyze this row
>         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 = 4 + w  # ESC[2K + rewrite
>
>         # Cost of partial updates (from current cursor)
>         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  # after writing run
>         cost_partial += len(changes)
>
>         # Choose cheaper option
>         if cost_erase <= cost_partial:
>             frame_cost += cost_move(cursor_r, cursor_c, r + 1, 1)  # move to start of row
>             frame_cost += 4 + w  # ESC[2K + rewrite
>             cursor_r, cursor_c = r + 1, w + 1
>         else:
>             frame_cost += cost_partial
>
>     total_cost += frame_cost + 1  # + delimiter
>
> print(f'Total cost: {total_cost}')
> print(f'Budget: {max_bytes}')
> print(f'Over/under: {total_cost - max_bytes}')
> "
Frame 0 non-space: 901
Frame 0 cost: 1275
Total cost: 293875
Budget: 69000
Over/under: 224875
root@f3f8d6319494:/app#
