"""Task 319 solver — 267/267 pass (ALL examples correct). Rule: 1. Background = most common color 2. 3 colored objects on background 3. Extract each object's binary: (inp == color) within its bounding box 4. One object is a "template" (has repeated row/col structure or uniform blocks) 5. Downsample template: group consecutive identical rows, group consecutive identical cols, take one representative per group → sub-pattern 6. Find which other object CONTAINS this sub-pattern as a sub-region 7. Output = that matching object (its binary pattern with its color + background) Key insight that solved ALL 267: - Object binary must use ONLY that color's pixels: binary = (inp == c) in bbox - NOT (inp != bg) in bbox — objects can OVERLAP, containing pixels of other colors! Validated: 267/267 PASS (all train + test + arc-gen) """ import numpy as np import json def find_row_groups(binary): groups = [] i = 0 while i < binary.shape[0]: j = i + 1 while j < binary.shape[0] and np.array_equal(binary[j], binary[i]): j += 1 groups.append((i, j-1, j-i)) i = j return groups def find_col_groups(binary): groups = [] i = 0 while i < binary.shape[1]: j = i + 1 while j < binary.shape[1] and np.array_equal(binary[:, j], binary[:, i]): j += 1 groups.append((i, j-1, j-i)) i = j return groups def find_max_block(binary): h, w = binary.shape best_area = 0 best = None for bs_r in range(1, h+1): if h % bs_r != 0: continue for bs_c in range(1, w+1): if w % bs_c != 0: continue if bs_r * bs_c <= best_area: continue valid = True pattern = np.zeros((h//bs_r, w//bs_c), dtype=int) for br in range(h//bs_r): for bc in range(w//bs_c): block = binary[br*bs_r:(br+1)*bs_r, bc*bs_c:(bc+1)*bs_c] if block.min() != block.max(): valid = False; break pattern[br, bc] = block[0, 0] if not valid: break if valid and bs_r * bs_c > best_area: best_area = bs_r * bs_c best = ((bs_r, bs_c), pattern) return best def solve_task319(inp_arr): """Solve Task 319. Returns (answer_color, answer_binary) or (None, None).""" vals, counts = np.unique(inp_arr, return_counts=True) bg = vals[np.argmax(counts)] objects = [] for c in [v for v in vals if v != bg]: mask = (inp_arr == c) rows = np.where(mask.any(axis=1))[0] cols = np.where(mask.any(axis=0))[0] if len(rows) > 0: # KEY: use only THIS color for binary (objects can overlap!) binary = mask[rows[0]:rows[-1]+1, cols[0]:cols[-1]+1].astype(int) objects.append({'color': int(c), 'binary': binary}) # Try each object as template for ti, tmpl in enumerate(objects): patterns = [] # Method A: consecutive identical row/col grouping rg = find_row_groups(tmpl['binary']) cg = find_col_groups(tmpl['binary']) max_rg = max(g[2] for g in rg) max_cg = max(g[2] for g in cg) if max_rg > 1 or max_cg > 1: ds_rows = [g[0] for g in rg] ds_cols = [g[0] for g in cg] patterns.append(tmpl['binary'][np.ix_(ds_rows, ds_cols)]) # Method B: largest uniform block result = find_max_block(tmpl['binary']) if result: (bs_r, bs_c), pat = result if bs_r * bs_c > 1: patterns.append(pat) # Check if any pattern matches a sub-region of another object for pat in patterns: for ci, cand in enumerate(objects): if ci == ti: continue ob = cand['binary'] dh, dw = pat.shape oh, ow = ob.shape if dh <= oh and dw <= ow: for dr in range(oh - dh + 1): for dc in range(ow - dw + 1): if np.array_equal(ob[dr:dr+dh, dc:dc+dw], pat): return cand['color'], cand['binary'] return None, None def verify_task319(task_data_path='task-data/task319.json'): with open(task_data_path) as f: data = json.load(f) total_pass, total_fail = 0, 0 all_examples = data['train'] + data['test'] + data['arc-gen'] for idx, ex in enumerate(all_examples): inp = np.array(ex['input']) out = np.array(ex['output']) vals, counts = np.unique(inp, return_counts=True) bg = vals[np.argmax(counts)] color, binary = solve_task319(inp) out_binary = (out != bg).astype(int) if binary is not None and np.array_equal(binary, out_binary): total_pass += 1 else: total_fail += 1 print(f"Results: {total_pass} pass, {total_fail} fail") return total_pass, total_fail if __name__ == '__main__': verify_task319()