"""Task 277 Solver — Smallest connected component recoloring (266/266 PASS). Rule: 1. Input is 10x10 with separate shapes made of color 8 on background 0 2. Find 8-connected components of color 8 3. The component with the SMALLEST area (pixel count) gets recolored to 2 4. All other components get recolored to 1 ONNX viability: - Requires connected-component detection (8-connected) - On 10x10 grid, MaxPool-based CC detection needs ~20-30 iterations × ~5 nodes = 100-150 nodes - Base: 56 nodes, profiled score 13.30 - Even with CC: ~150 nodes * 400 bytes = 60K memory + params → score ~14.1 - **POTENTIALLY VIABLE** — score 14.1 vs base 13.30 → gain +0.8 - But CC detection in ONNX is fragile and complex to build correctly CC approach for ONNX: 1. Initialize label grid = nonzero_mask * (row*10 + col + 1) [unique labels] 2. Repeat 20 times: MaxPool(3x3) the label grid, masked by nonzero_mask 3. After convergence, each CC has the same max label 4. Count pixels per label → find minimum count label → mask it as color 2 5. Remaining → color 1 Challenge: Step 4 requires comparing per-label counts, which is hard without dynamic shapes. Alternative: could potentially use the fact that shapes are always rectangular frames or solid blocks. Status: Rule cracked (266/266), ONNX build pending — needs careful CC implementation. """ import numpy as np import json from scipy import ndimage def solve_277(inp_grid): """Smallest CC gets color 2, all others get color 1. 266/266 PASS.""" inp = np.array(inp_grid) out = np.zeros_like(inp) # Find 8-connected components of color 8 mask = (inp == 8).astype(int) labeled, n = ndimage.label(mask, structure=np.ones((3, 3))) if n == 0: return out # Find smallest component by area areas = [] for i in range(1, n + 1): areas.append(((labeled == i).sum(), i)) min_area = min(a for a, _ in areas) for area, idx in areas: component = (labeled == idx) if area == min_area: out[component] = 2 else: out[component] = 1 return out if __name__ == '__main__': from pathlib import Path task_data = Path('/app/task-data') if not task_data.exists(): task_data = Path(__file__).parent.parent / 'task-data' with open(task_data / 'task277.json') as f: data = json.load(f) all_ex = data['train'] + data['test'] + data.get('arc-gen', []) right = sum(1 for ex in all_ex if np.array_equal(solve_277(ex['input']), np.array(ex['output']))) print(f'task277: {right}/{len(all_ex)} PASS')