| { | |
| "4c5c2cf0": "def solve_4c5c2cf0(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # Find all non-zero cells, separate into two color groups\n colors = {}\n for r in range(h):\n for c in range(w):\n if grid[r][c] != 0:\n col = grid[r][c]\n if col not in colors:\n colors[col] = []\n colors[col].append((r, c))\n\n color_list = list(colors.keys())\n\n # The connector has 4-fold symmetry (X/diamond pattern with center).\n # Check each color for this property.\n connector_color = None\n shape_color = None\n\n for col in color_list:\n cells = colors[col]\n rs = [r for r,c in cells]\n cs = [c for r,c in cells]\n cr = (min(rs) + max(rs)) / 2\n cc = (min(cs) + max(cs)) / 2\n # Check 4-fold symmetry: for each cell, all 4 reflections should exist\n cell_set = set(cells)\n symmetric = True\n for r, c in cells:\n # Check horizontal, vertical, and both reflections\n hr, hc = r, int(2*cc - c)\n vr, vc = int(2*cr - r), c\n br, bc = int(2*cr - r), int(2*cc - c)\n if (hr, hc) not in cell_set or (vr, vc) not in cell_set or (br, bc) not in cell_set:\n symmetric = False\n break\n if symmetric and len(cells) >= 4:\n connector_color = col\n else:\n shape_color = col\n\n if connector_color is None or shape_color is None:\n if len(colors[color_list[0]]) < len(colors[color_list[1]]):\n connector_color = color_list[0]\n shape_color = color_list[1]\n else:\n connector_color = color_list[1]\n shape_color = color_list[0]\n\n conn_cells = colors[connector_color]\n shape_cells = colors[shape_color]\n\n # Find connector center\n conn_rs = [r for r,c in conn_cells]\n conn_cs = [c for r,c in conn_cells]\n conn_cr = (min(conn_rs) + max(conn_rs)) / 2\n conn_cc = (min(conn_cs) + max(conn_cs)) / 2\n\n ccr = round(conn_cr)\n ccc = round(conn_cc)\n\n # For each shape cell, compute its reflection through the connector center\n for r, c in shape_cells:\n # Horizontal reflection\n nr, nc = r, 2*ccc - c\n if 0 <= nr < h and 0 <= nc < w and out[nr][nc] == 0:\n out[nr][nc] = shape_color\n # Vertical reflection\n nr, nc = 2*ccr - r, c\n if 0 <= nr < h and 0 <= nc < w and out[nr][nc] == 0:\n out[nr][nc] = shape_color\n # Both reflections\n nr, nc = 2*ccr - r, 2*ccc - c\n if 0 <= nr < h and 0 <= nc < w and out[nr][nc] == 0:\n out[nr][nc] = shape_color\n\n return out\n", | |
| "508bd3b6": "def solve_508bd3b6(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # Find 8 cells (the arrow/diagonal line)\n eights = []\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 8:\n eights.append((r, c))\n eights.sort()\n\n # Direction of the 8-arrow\n dr = eights[1][0] - eights[0][0]\n dc = eights[1][1] - eights[0][1]\n # Normalize to unit steps\n dr = 1 if dr > 0 else (-1 if dr < 0 else 0)\n dc = 1 if dc > 0 else (-1 if dc < 0 else 0)\n\n # Find wall of 2s\n two_set = set()\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 2:\n two_set.add((r, c))\n\n # Determine wall orientation\n two_rows = set(r for r, c in two_set)\n two_cols = set(c for r, c in two_set)\n wall_is_horizontal = any(sum(1 for c2 in range(w) if (row, c2) in two_set) == w for row in two_rows)\n wall_is_vertical = any(sum(1 for r2 in range(h) if (r2, col) in two_set) == h for col in two_cols)\n\n # The ball continues from the tip of the 8-arrow in the arrow direction,\n # bouncing off the wall when it would enter a wall cell.\n # Find which end of the 8-line to continue from:\n # Continue in the direction that goes TOWARD the wall.\n # Test: does continuing from last 8 in arrow direction lead toward wall?\n last_8 = eights[-1] # last in sorted order = farthest in +row direction\n first_8 = eights[0]\n\n # Try continuing from last_8 in direction (dr, dc)\n test_r, test_c = last_8[0] + dr, last_8[1] + dc\n goes_toward_wall_from_last = False\n for step in range(1, max(h, w)):\n tr, tc = last_8[0] + step * dr, last_8[1] + step * dc\n if (tr, tc) in two_set or tr < 0 or tr >= h or tc < 0 or tc >= w:\n if (tr, tc) in two_set:\n goes_toward_wall_from_last = True\n break\n\n if goes_toward_wall_from_last:\n start = last_8\n move_dr, move_dc = dr, dc\n else:\n start = first_8\n move_dr, move_dc = -dr, -dc\n\n # Draw path from start in direction (move_dr, move_dc) until hitting wall, then bounce\n r, c = start\n cur_dr, cur_dc = move_dr, move_dc\n while True:\n nr, nc = r + cur_dr, c + cur_dc\n if nr < 0 or nr >= h or nc < 0 or nc >= w:\n break\n if (nr, nc) in two_set:\n # Bounce: reverse the component perpendicular to wall\n if wall_is_horizontal:\n cur_dr = -cur_dr # reverse vertical\n elif wall_is_vertical:\n cur_dc = -cur_dc # reverse horizontal\n nr, nc = r + cur_dr, c + cur_dc\n if nr < 0 or nr >= h or nc < 0 or nc >= w:\n break\n if (nr, nc) in two_set:\n break\n if out[nr][nc] != 0:\n break\n out[nr][nc] = 3\n r, c = nr, nc\n\n return out\n", | |
| "50cb2852": "def solve_50cb2852(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # Find connected components of non-zero cells\n visited = [[False]*w for _ in range(h)]\n\n def flood_fill(sr, sc, color):\n cells = []\n stack = [(sr, sc)]\n while stack:\n r, c = stack.pop()\n if r < 0 or r >= h or c < 0 or c >= w:\n continue\n if visited[r][c] or grid[r][c] != color:\n continue\n visited[r][c] = True\n cells.append((r, c))\n for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n stack.append((r+dr, c+dc))\n return cells\n\n for r in range(h):\n for c in range(w):\n if grid[r][c] != 0 and not visited[r][c]:\n color = grid[r][c]\n cells = flood_fill(r, c, color)\n if len(cells) < 4:\n continue\n # Find bounding box\n rs = [r for r,c in cells]\n cs = [c for r,c in cells]\n min_r, max_r = min(rs), max(rs)\n min_c, max_c = min(cs), max(cs)\n # Fill interior with 8\n for ir in range(min_r+1, max_r):\n for ic in range(min_c+1, max_c):\n out[ir][ic] = 8\n\n return out\n", | |
| "5117e062": "def solve_5117e062(grid):\n h, w = len(grid), len(grid[0])\n\n # Find the 8 cell\n eight_r, eight_c = -1, -1\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 8:\n eight_r, eight_c = r, c\n\n # Find the color of the shape containing the 8\n # Look at neighbors of the 8 cell\n shape_color = 0\n for dr, dc in [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]:\n nr, nc = eight_r + dr, eight_c + dc\n if 0 <= nr < h and 0 <= nc < w and grid[nr][nc] != 0 and grid[nr][nc] != 8:\n shape_color = grid[nr][nc]\n break\n\n # Find all cells of this shape color + the 8 cell\n cells = []\n for r in range(h):\n for c in range(w):\n if grid[r][c] == shape_color or (r == eight_r and c == eight_c):\n cells.append((r, c))\n\n # Actually we need to find just the connected component containing the 8\n # Let's get the bounding box of cells near the 8\n # Find connected component of shape_color + 8\n visited = set()\n stack = [(eight_r, eight_c)]\n component = []\n while stack:\n r, c = stack.pop()\n if (r, c) in visited:\n continue\n if r < 0 or r >= h or c < 0 or c >= w:\n continue\n if grid[r][c] != shape_color and grid[r][c] != 8:\n continue\n visited.add((r, c))\n component.append((r, c))\n for dr in [-1, 0, 1]:\n for dc in [-1, 0, 1]:\n if dr == 0 and dc == 0:\n continue\n stack.append((r+dr, c+dc))\n\n rs = [r for r, c in component]\n cs = [c for r, c in component]\n min_r, max_r = min(rs), max(rs)\n min_c, max_c = min(cs), max(cs)\n\n # Build output: the shape pattern with 8 replaced by shape_color\n result = []\n for r in range(min_r, max_r + 1):\n row = []\n for c in range(min_c, max_c + 1):\n if grid[r][c] == shape_color or grid[r][c] == 8:\n row.append(shape_color)\n else:\n row.append(0)\n result.append(row)\n\n return result\n", | |
| "5168d44c": "def solve_5168d44c(grid):\n h, w = len(grid), len(grid[0])\n out = [[0]*w for _ in range(h)]\n\n # Find the box (3x3 of 2s with 3 center)\n box_cells = []\n center_r, center_c = -1, -1\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 2:\n box_cells.append((r, c))\n\n # Find box center (the 3 inside the box)\n br = [r for r,c in box_cells]\n bc = [c for r,c in box_cells]\n center_r = (min(br) + max(br)) // 2\n center_c = (min(bc) + max(bc)) // 2\n\n # Find trail 3s (not the center)\n trail_3s = []\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 3 and (r, c) != (center_r, center_c):\n trail_3s.append((r, c))\n\n # Determine trail direction\n # All trail 3s should be on same row or same col as center\n same_row = all(r == center_r for r, c in trail_3s)\n same_col = all(c == center_c for r, c in trail_3s)\n\n if same_row:\n # Horizontal trail\n left = [c for r, c in trail_3s if c < center_c]\n right = [c for r, c in trail_3s if c > center_c]\n if len(right) > len(left):\n move_dr, move_dc = 0, 2\n else:\n move_dr, move_dc = 0, -2\n else:\n # Vertical trail\n above = [r for r, c in trail_3s if r < center_r]\n below = [r for r, c in trail_3s if r > center_r]\n if len(below) > len(above):\n move_dr, move_dc = 2, 0\n else:\n move_dr, move_dc = -2, 0\n\n # Place ALL 3s from input (trail + original center) - they all stay\n all_3s = trail_3s + [(center_r, center_c)]\n for r, c in all_3s:\n out[r][c] = 3\n\n # Place box (2-cells) at new position, overwriting 0s but not 3s at center\n for r, c in box_cells:\n nr = r + move_dr\n nc = c + move_dc\n if 0 <= nr < h and 0 <= nc < w:\n out[nr][nc] = 2\n\n # The new box center remains 3\n new_cr = center_r + move_dr\n new_cc = center_c + move_dc\n out[new_cr][new_cc] = 3\n\n return out\n", | |
| "539a4f51": "def solve_539a4f51(grid):\n n = len(grid)\n # Get diagonal\n diag = [grid[i][i] for i in range(n)]\n # Strip trailing 0s\n while diag and diag[-1] == 0:\n diag.pop()\n period = len(diag)\n out_size = 2 * n\n out = [[0]*out_size for _ in range(out_size)]\n for r in range(out_size):\n for c in range(out_size):\n out[r][c] = diag[max(r, c) % period]\n return out\n", | |
| "53b68214": "def solve_53b68214(grid):\n h = len(grid)\n w = len(grid[0])\n target_h = w # output is always 10x10 (width x width)\n\n if h >= target_h:\n return grid[:target_h]\n\n # Find period and shift\n best_p = None\n best_s = None\n\n for p in range(1, h):\n # Check if all rows i and i+p have consistent shift\n shifts = []\n valid = True\n for i in range(h - p):\n row_a = grid[i]\n row_b = grid[i + p]\n # Find shift between row_a and row_b\n # Find non-zero positions\n nz_a = [c for c in range(w) if row_a[c] != 0]\n nz_b = [c for c in range(w) if row_b[c] != 0]\n if len(nz_a) == 0 and len(nz_b) == 0:\n shifts.append(0)\n continue\n if len(nz_a) != len(nz_b):\n valid = False\n break\n if len(nz_a) == 0:\n shifts.append(0)\n continue\n s = nz_b[0] - nz_a[0]\n # Verify all positions shift by s\n if all(nz_b[j] - nz_a[j] == s for j in range(len(nz_a))):\n # Also verify colors match\n if all(row_a[nz_a[j]] == row_b[nz_b[j]] for j in range(len(nz_a))):\n shifts.append(s)\n else:\n valid = False\n break\n else:\n valid = False\n break\n\n if valid and len(set(shifts)) <= 1:\n best_p = p\n best_s = shifts[0] if shifts else 0\n break\n\n if best_p is None:\n best_p = h\n best_s = 0\n\n # Extend to target_h rows\n out = [row[:] for row in grid]\n for i in range(h, target_h):\n # Row i = row (i - best_p) shifted by best_s\n src = i - best_p\n new_row = [0] * w\n for c in range(w):\n src_c = c - best_s\n if 0 <= src_c < w:\n new_row[c] = out[src][src_c]\n out.append(new_row)\n\n return out\n", | |
| "543a7ed5": "def solve_543a7ed5(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # Find connected components of 6 cells\n visited = [[False]*w for _ in range(h)]\n\n def flood_fill(sr, sc):\n cells = []\n stack = [(sr, sc)]\n while stack:\n r, c = stack.pop()\n if r < 0 or r >= h or c < 0 or c >= w:\n continue\n if visited[r][c] or grid[r][c] != 6:\n continue\n visited[r][c] = True\n cells.append((r, c))\n for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n stack.append((r+dr, c+dc))\n return cells\n\n rectangles = []\n for r in range(h):\n for c in range(w):\n if grid[r][c] == 6 and not visited[r][c]:\n cells = flood_fill(r, c)\n rectangles.append(cells)\n\n for cells in rectangles:\n rs = [r for r, c in cells]\n cs = [c for r, c in cells]\n min_r, max_r = min(rs), max(rs)\n min_c, max_c = min(cs), max(cs)\n\n # Check if hollow (interior has 8 cells)\n has_interior = False\n for r in range(min_r + 1, max_r):\n for c in range(min_c + 1, max_c):\n if grid[r][c] == 8:\n has_interior = True\n break\n if has_interior:\n break\n\n # Fill interior with 4 if hollow\n if has_interior:\n for r in range(min_r + 1, max_r):\n for c in range(min_c + 1, max_c):\n if grid[r][c] == 8:\n out[r][c] = 4\n\n # Add 3-border around the rectangle\n for r in range(min_r - 1, max_r + 2):\n for c in range(min_c - 1, max_c + 2):\n if 0 <= r < h and 0 <= c < w:\n if out[r][c] == 8:\n # Check if adjacent to the rectangle bbox\n if r == min_r - 1 or r == max_r + 1 or c == min_c - 1 or c == max_c + 1:\n out[r][c] = 3\n\n return out\n", | |
| "54d82841": "def solve_54d82841(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # Find U-shapes: look for patterns like [X,X,X] on top and [X,0,X] below\n # (or rotated versions)\n # The shapes are \u2293-shaped (3 cells on top, 2 cells below with gap)\n\n # Find all non-zero colored shapes\n visited = [[False]*w for _ in range(h)]\n\n def flood_fill(sr, sc, color):\n cells = []\n stack = [(sr, sc)]\n while stack:\n r, c = stack.pop()\n if r < 0 or r >= h or c < 0 or c >= w:\n continue\n if visited[r][c] or grid[r][c] != color:\n continue\n visited[r][c] = True\n cells.append((r, c))\n for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n stack.append((r+dr, c+dc))\n return cells\n\n shapes = []\n for r in range(h):\n for c in range(w):\n if grid[r][c] != 0 and not visited[r][c]:\n color = grid[r][c]\n cells = flood_fill(r, c, color)\n shapes.append((color, cells))\n\n # For each shape, find the \"opening\" - the 0-cell that's enclosed on 3 sides\n for color, cells in shapes:\n cell_set = set(cells)\n rs = [r for r, c in cells]\n cs = [c for r, c in cells]\n min_r, max_r = min(rs), max(rs)\n min_c, max_c = min(cs), max(cs)\n\n # Check for opening: find 0-cells within/adjacent to bbox that have 3 colored neighbors\n # Actually the U-shape has a specific gap. Let me find it.\n # The opening is a cell within the bbox that is 0 and surrounded by shape cells on 3 sides.\n for r in range(min_r, max_r + 1):\n for c in range(min_c, max_c + 1):\n if (r, c) not in cell_set and grid[r][c] == 0:\n # Count adjacent shape cells\n adj = 0\n for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n nr, nc = r + dr, c + dc\n if (nr, nc) in cell_set:\n adj += 1\n if adj >= 3:\n # This is the opening - place 4 at bottom of grid in this column\n out[h-1][c] = 4\n\n return out\n", | |
| "54d9e175": "def solve_54d9e175(grid):\n h, w = len(grid), len(grid[0])\n out = [row[:] for row in grid]\n\n # The grid is divided by 5-separators into sections\n # Find separator rows and columns\n sep_rows = []\n sep_cols = []\n\n for r in range(h):\n if all(grid[r][c] == 5 for c in range(w)):\n sep_rows.append(r)\n\n for c in range(w):\n if all(grid[r][c] == 5 for r in range(h)):\n sep_cols.append(c)\n\n # Define sections as regions between separators\n row_ranges = []\n prev = 0\n for sr in sep_rows:\n if sr > prev:\n row_ranges.append((prev, sr))\n prev = sr + 1\n if prev < h:\n row_ranges.append((prev, h))\n\n col_ranges = []\n prev = 0\n for sc in sep_cols:\n if sc > prev:\n col_ranges.append((prev, sc))\n prev = sc + 1\n if prev < w:\n col_ranges.append((prev, w))\n\n # For each section, find the center color and fill with color + 5\n for r_start, r_end in row_ranges:\n for c_start, c_end in col_ranges:\n # Find the non-zero, non-5 color in this section\n center_color = 0\n for r in range(r_start, r_end):\n for c in range(c_start, c_end):\n if grid[r][c] != 0 and grid[r][c] != 5:\n center_color = grid[r][c]\n break\n if center_color != 0:\n break\n\n if center_color != 0:\n fill_color = center_color + 5\n for r in range(r_start, r_end):\n for c in range(c_start, c_end):\n if grid[r][c] != 5:\n out[r][c] = fill_color\n\n return out\n" | |
| } |