#!/usr/bin/env python3 """Per-state duration histograms + line-clear sequences from ai2_rec.lua recordings.""" import sys from collections import Counter, defaultdict W = 0x800 hist = defaultdict(Counter) clears = Counter() # (lines, frames from 0A to next 00) cycle = Counter() # frames 00 -> 05 for path in sys.argv[1:]: rec = open(path, 'rb').read() n = len(rec) // W for base, sa in ((0x202, 0x20D), (0x462, 0x46D)): runs, cur, beg = [], None, 0 for f in range(n): s = rec[f * W + sa] if s != cur: if cur is not None: runs.append((beg, cur, f - beg)) cur, beg = s, f for i, (b, s, d) in enumerate(runs): if 0 < i < len(runs) - 1: hist[s][d] += 1 if s == 0x00 and i + 1 < len(runs): # frames until state 05 j, tot = i, 0 while j < len(runs) and runs[j][1] != 0x05: tot += runs[j][2]; j += 1 if j < len(runs) and j - i < 12: cycle[tuple(r[1] for r in runs[i:j])[:8], tot] += 1 if s == 0x0A and i + 1 < len(runs) and runs[i + 1][1] == 0x0B: fr = rec[b * W:(b + 1) * W] j, tot = i + 1, 0 while j < len(runs) and runs[j][1] != 0x00: tot += runs[j][2]; j += 1 lines = sum(1 for k in range(4) if fr[base + 0x33 + k] != 0xFF) fr2 = rec[(b + 1) * W:(b + 2) * W] lines = sum(1 for k in range(4) if fr2[base + 0x33 + k] != 0xFF) clears[lines, tot] += 1 for s in sorted(hist): print(f'state {s:02X}: ' + ', '.join(f'{d}f x{c}' for d, c in sorted(hist[s].items())[:14])) print('line clears (lines, frames from first 0B to next 00):') for k, v in sorted(clears.items()): print(' ', k, v) print('cycle 00->05 (state sequence, frames):') for k, v in cycle.most_common(12): print(' ', ' '.join(f'{s:02X}' for s in k[0]), k[1], 'x', v)