{
  "analysis": "Partial updates (48 bytes) are actually cheaper than EL+rewrite (91 bytes) for row 13 because the changes are in large runs. I need to calculate the actual cost per row and choose the cheaper option. Let me implement this correctly.",
  "plan": "Rewrite the encoder to calculate the actual cost per row and choose the cheaper option (EL+rewrite vs partial).",
  "commands": [
    {
      "keystrokes": "cat > /app/termzip_ansi.py << 'EOF'\nimport struct\nimport sys\n\ndef find_runs(changes):\n    \"\"\"Find contiguous runs of changed columns.\"\"\"\n    runs = []\n    if not changes:\n        return runs\n    run_start = changes[0]\n    run_end = changes[0]\n    for c in changes[1:]:\n        if c == run_end + 1:\n            run_end = c\n        else:\n            runs.append((run_start, run_end))\n            run_start = run_end = c\n    runs.append((run_start, run_end))\n    return runs\n\ndef main():\n    if len(sys.argv) != 2:\n        sys.exit(1)\n    \n    with open(sys.argv[1], 'rb') as f:\n        magic = f.read(4)\n        if magic != b'TZA1':\n            sys.exit(1)\n        w, h = struct.unpack('<HH', f.read(4))\n        n_frames, max_bytes = struct.unpack('<II', f.read(8))\n        \n        # Read all frames\n        frames = [f.read(w * h) for _ in range(n_frames)]\n    \n    out = []\n    \n    # Frame 0: write non-space characters using absolute positioning\n    for r in range(h):\n        row = frames[0][r*w:(r+1)*w]\n        non_space = [c for c in range(w) if row[c] != 32]\n        runs = find_runs(non_space)\n        for start, end in runs:\n            out.append(f'\\x1b[{r+1};{start+1}H')\n            out.append(row[start:end+1].decode('latin1'))\n    out.append('\\x1e')\n    \n    # Subsequent frames\n    for i in range(1, n_frames):\n        # Find changed rows and their changes\n        row_changes = {}\n        for r in range(h):\n            prev_row = frames[i-1][r*w:(r+1)*w]\n            curr_row = frames[i][r*w:(r+1)*w]\n            if prev_row != curr_row:\n                changes = [c for c in range(w) if prev_row[c] != curr_row[c]]\n                row_changes[r] = (changes, curr_row)\n        \n        if not row_changes:\n            out.append('\\x1e')\n            continue\n        \n        for r, (changes, curr_row) in sorted(row_changes.items()):\n            runs = find_runs(changes)\n            \n            # Calculate costs\n            # EL+rewrite: ESC[r;1H + ESC[2K + row\n            cost_erase = 3 + len(str(r+1)) + 1 + 1 + 4 + w\n            \n            # Partial: sum of positioning + content for each run\n            cost_partial = sum(3 + len(str(r+1)) + 1 + len(str(start+1)) + 1 + (end-start+1) for start, end in runs)\n            \n            if cost_erase <= cost_partial:\n                out.append(f'\\x1b[{r+1};1H')\n                out.append('\\x1b[2K')\n                out.append(curr_row.decode('latin1'))\n            else:\n                for start, end in runs:\n                    out.append(f'\\x1b[{r+1};{start+1}H')\n                    out.append(curr_row[start:end+1].decode('latin1'))\n        \n        out.append('\\x1e')\n    \n    sys.stdout.write(''.join(out))\n\nif __name__ == '__main__':\n    main()\nEOF\n",
      "duration": 0.5
    }
  ]
}