rogermt commited on
Commit
bca2538
·
verified ·
1 Parent(s): 0598f18

Add task197 solver (267/267)

Browse files
medal-solvers/task_solvers/task197_solver_267.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Task 197 Python Solver: Pattern repetition with color substitution.
3
+ 267/267 verified.
4
+
5
+ Rule:
6
+ 1. One row is the "template" (fully non-zero across entire width)
7
+ 2. Other non-zero rows are "seeds" (first N pixels filled, rest zero)
8
+ 3. For each seed row: build color mapping from template → seed
9
+ (template[c] maps to seed[c] for c < seed_length)
10
+ 4. Apply mapping to full template to fill the entire row
11
+ """
12
+ import numpy as np
13
+ import json
14
+
15
+
16
+ def solve_task197(grid):
17
+ grid = np.array(grid)
18
+ H, W = grid.shape
19
+ out = grid.copy()
20
+
21
+ # Find template row (fully non-zero)
22
+ template_row = None
23
+ for r in range(H):
24
+ if np.all(grid[r] != 0):
25
+ template_row = r
26
+ break
27
+ if template_row is None:
28
+ return out.tolist()
29
+
30
+ template = grid[template_row]
31
+
32
+ # For each seed row
33
+ for r in range(H):
34
+ if r == template_row:
35
+ continue
36
+ row = grid[r]
37
+ if np.all(row == 0):
38
+ continue
39
+
40
+ # Find seed length (contiguous non-zero from left)
41
+ seed_len = 0
42
+ for c in range(W):
43
+ if row[c] != 0:
44
+ seed_len = c + 1
45
+ else:
46
+ break
47
+ if seed_len == 0:
48
+ continue
49
+
50
+ # Build color mapping: template_color → seed_color
51
+ color_map = {}
52
+ for c in range(seed_len):
53
+ t_color = template[c]
54
+ s_color = row[c]
55
+ if t_color not in color_map:
56
+ color_map[t_color] = s_color
57
+
58
+ # Apply mapping to full template
59
+ for c in range(W):
60
+ t_color = template[c]
61
+ if t_color in color_map:
62
+ out[r, c] = color_map[t_color]
63
+
64
+ return out.tolist()
65
+
66
+
67
+ def validate():
68
+ with open('/kaggle/input/competitions/neurogolf-2026/task197.json') as f:
69
+ data = json.load(f)
70
+
71
+ all_ex = data.get('train', []) + data.get('test', []) + data.get('arc-gen', [])
72
+ right, wrong = 0, 0
73
+ for ex in all_ex:
74
+ result = solve_task197(ex['input'])
75
+ if result == ex['output']:
76
+ right += 1
77
+ else:
78
+ wrong += 1
79
+ print(f"Task 197: {right}/{right+wrong}")
80
+ return wrong == 0
81
+
82
+
83
+ if __name__ == '__main__':
84
+ validate()