File size: 16,316 Bytes
343b6b9
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
{
  "b27ca6d3": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    twos = set()\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] == 2:\n                twos.add((r,c))\n    visited = set()\n    groups = []\n    for cell in twos:\n        if cell in visited:\n            continue\n        group = set()\n        queue = [cell]\n        while queue:\n            curr = queue.pop(0)\n            if curr in visited:\n                continue\n            visited.add(curr)\n            group.add(curr)\n            r, c = curr\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 twos and (nr, nc) not in visited:\n                    queue.append((nr, nc))\n        groups.append(group)\n    output = [row[:] for row in grid]\n    for group in groups:\n        if len(group) < 2:\n            continue\n        min_r = min(r for r,c in group)\n        max_r = max(r for r,c in group)\n        min_c = min(c for r,c in group)\n        max_c = max(c for r,c in group)\n        br1 = max(0, min_r - 1)\n        br2 = min(rows - 1, max_r + 1)\n        bc1 = max(0, min_c - 1)\n        bc2 = min(cols - 1, max_c + 1)\n        for r in range(br1, br2 + 1):\n            for c in range(bc1, bc2 + 1):\n                if r == br1 or r == br2 or c == bc1 or c == bc2:\n                    if output[r][c] == 0:\n                        output[r][c] = 3\n    return output",
  "b775ac94": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    nonzero = {}\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] != 0:\n                nonzero[(r,c)] = grid[r][c]\n    visited = set()\n    blobs = []\n    for cell in nonzero:\n        if cell in visited:\n            continue\n        blob = set()\n        queue = [cell]\n        while queue:\n            curr = queue.pop()\n            if curr in visited:\n                continue\n            visited.add(curr)\n            blob.add(curr)\n            r, c = curr\n            for dr in [-1,0,1]:\n                for dc in [-1,0,1]:\n                    if dr == 0 and dc == 0:\n                        continue\n                    nr, nc = r+dr, c+dc\n                    if (nr,nc) in nonzero and (nr,nc) not in visited:\n                        queue.append((nr,nc))\n        blobs.append(blob)\n    output = [row[:] for row in grid]\n    for blob in blobs:\n        colors = {}\n        for cell in blob:\n            c = nonzero[cell]\n            if c not in colors:\n                colors[c] = set()\n            colors[c].add(cell)\n        if len(colors) == 1:\n            continue\n        main_color = max(colors, key=lambda c: len(colors[c]))\n        main_cells = colors[main_color]\n        min_r = min(r for r,c in main_cells)\n        max_r = max(r for r,c in main_cells)\n        min_c = min(c for r,c in main_cells)\n        max_c = max(c for r,c in main_cells)\n        for conn_color, conn_cells in colors.items():\n            if conn_color == main_color:\n                continue\n            for cr, cc in conn_cells:\n                right_of = cc > max_c\n                left_of = cc < min_c\n                below = cr > max_r\n                above = cr < min_r\n                if right_of and not below and not above:\n                    axis_c = max_c + 0.5\n                    for mr, mc in main_cells:\n                        nc = int(2 * axis_c - mc)\n                        if 0 <= nc < cols and output[mr][nc] == 0:\n                            output[mr][nc] = conn_color\n                elif left_of and not below and not above:\n                    axis_c = min_c - 0.5\n                    for mr, mc in main_cells:\n                        nc = int(2 * axis_c - mc)\n                        if 0 <= nc < cols and output[mr][nc] == 0:\n                            output[mr][nc] = conn_color\n                elif below and not right_of and not left_of:\n                    axis_r = max_r + 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * axis_r - mr)\n                        if 0 <= nr < rows and output[nr][mc] == 0:\n                            output[nr][mc] = conn_color\n                elif above and not right_of and not left_of:\n                    axis_r = min_r - 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * axis_r - mr)\n                        if 0 <= nr < rows and output[nr][mc] == 0:\n                            output[nr][mc] = conn_color\n                elif right_of and below:\n                    pr = max_r + 0.5\n                    pc = max_c + 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * pr - mr)\n                        nc = int(2 * pc - mc)\n                        if 0 <= nr < rows and 0 <= nc < cols and output[nr][nc] == 0:\n                            output[nr][nc] = conn_color\n                elif left_of and below:\n                    pr = max_r + 0.5\n                    pc = min_c - 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * pr - mr)\n                        nc = int(2 * pc - mc)\n                        if 0 <= nr < rows and 0 <= nc < cols and output[nr][nc] == 0:\n                            output[nr][nc] = conn_color\n                elif right_of and above:\n                    pr = min_r - 0.5\n                    pc = max_c + 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * pr - mr)\n                        nc = int(2 * pc - mc)\n                        if 0 <= nr < rows and 0 <= nc < cols and output[nr][nc] == 0:\n                            output[nr][nc] = conn_color\n                elif left_of and above:\n                    pr = min_r - 0.5\n                    pc = min_c - 0.5\n                    for mr, mc in main_cells:\n                        nr = int(2 * pr - mr)\n                        nc = int(2 * pc - mc)\n                        if 0 <= nr < rows and 0 <= nc < cols and output[nr][nc] == 0:\n                            output[nr][nc] = conn_color\n    return output",
  "b782dc8a": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    markers = {}\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] not in (0, 8):\n                markers[(r,c)] = grid[r][c]\n    center = None\n    max_neighbors = 0\n    for (r,c), v in markers.items():\n        n = 0\n        for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n            if (r+dr, c+dc) in markers:\n                n += 1\n        if n > max_neighbors:\n            max_neighbors = n\n            center = (r, c)\n    center_color = markers[center]\n    arm_colors = set(v for v in markers.values() if v != center_color)\n    arm_color = arm_colors.pop() if arm_colors else center_color\n    output = [row[:] for row in grid]\n    from collections import deque\n    visited = {}\n    queue = deque()\n    visited[center] = 0\n    queue.append((center, 0))\n    for (r,c), v in markers.items():\n        if (r,c) != center:\n            visited[(r,c)] = 1\n            queue.append(((r,c), 1))\n    while queue:\n        (r,c), dist = queue.popleft()\n        color = center_color if dist % 2 == 0 else arm_color\n        output[r][c] = color\n        for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:\n            nr, nc = r+dr, c+dc\n            if 0 <= nr < rows and 0 <= nc < cols and (nr,nc) not in visited and grid[nr][nc] == 0:\n                visited[(nr,nc)] = dist + 1\n                queue.append(((nr,nc), dist + 1))\n    return output",
  "b8825c91": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    four_cells = []\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] == 4:\n                four_cells.append((r,c))\n    cr = (rows - 1) / 2\n    cc = (cols - 1) / 2\n    output = [row[:] for row in grid]\n    for r, c in four_cells:\n        sr = int(2 * cr - r)\n        sc = int(2 * cc - c)\n        if 0 <= sr < rows and 0 <= sc < cols and grid[sr][sc] != 4:\n            output[r][c] = grid[sr][sc]\n    return output",
  "c1d99e64": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    output = [row[:] for row in grid]\n    zero_rows = []\n    for r in range(rows):\n        if all(grid[r][c] == 0 for c in range(cols)):\n            zero_rows.append(r)\n    zero_cols = []\n    for c in range(cols):\n        if all(grid[r][c] == 0 for r in range(rows)):\n            zero_cols.append(c)\n    for r in zero_rows:\n        for c in range(cols):\n            output[r][c] = 2\n    for c in zero_cols:\n        for r in range(rows):\n            output[r][c] = 2\n    return output",
  "c3f564a4": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    non_zero_vals = set()\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] != 0:\n                non_zero_vals.add(grid[r][c])\n    N = len(non_zero_vals)\n    off = None\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] != 0:\n                off = (grid[r][c] - 1 - (r + c)) % N\n                break\n        if off is not None:\n            break\n    output = []\n    for r in range(rows):\n        row = []\n        for c in range(cols):\n            row.append(((r + c + off) % N) + 1)\n        output.append(row)\n    return output",
  "c444b776": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    four_rows = []\n    for r in range(rows):\n        if all(grid[r][c] == 4 for c in range(cols)):\n            four_rows.append(r)\n    four_cols = []\n    for c in range(cols):\n        if all(grid[r][c] == 4 for r in range(rows)):\n            four_cols.append(c)\n    row_ranges = []\n    prev = 0\n    for r in sorted(four_rows):\n        if r > prev:\n            row_ranges.append((prev, r))\n        prev = r + 1\n    if prev < rows:\n        row_ranges.append((prev, rows))\n    col_ranges = []\n    prev = 0\n    for c in sorted(four_cols):\n        if c > prev:\n            col_ranges.append((prev, c))\n        prev = c + 1\n    if prev < cols:\n        col_ranges.append((prev, cols))\n    def get_pattern(r_start, r_end, c_start, c_end):\n        pat = []\n        for r in range(r_start, r_end):\n            row = []\n            for c in range(c_start, c_end):\n                row.append(grid[r][c])\n            pat.append(row)\n        return pat\n    def has_content(pat):\n        for row in pat:\n            for v in row:\n                if v != 0:\n                    return True\n        return False\n    source_pat = None\n    for rs, re in row_ranges:\n        for cs, ce in col_ranges:\n            pat = get_pattern(rs, re, cs, ce)\n            if has_content(pat):\n                source_pat = pat\n                break\n        if source_pat:\n            break\n    output = [row[:] for row in grid]\n    for rs, re in row_ranges:\n        for cs, ce in col_ranges:\n            h = re - rs\n            w = ce - cs\n            if h == len(source_pat) and w == len(source_pat[0]):\n                for r in range(h):\n                    for c in range(w):\n                        output[rs + r][cs + c] = source_pat[r][c]\n    return output",
  "c909285e": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    from collections import Counter\n    all_vals = Counter()\n    for r in range(rows):\n        for c in range(cols):\n            all_vals[grid[r][c]] += 1\n    for color in all_vals:\n        cells = [(r,c) for r in range(rows) for c in range(cols) if grid[r][c] == color]\n        if len(cells) < 8 or len(cells) > 200:\n            continue\n        min_r = min(r for r,c in cells)\n        max_r = max(r for r,c in cells)\n        min_c = min(c for r,c in cells)\n        max_c = max(c for r,c in cells)\n        border_cells = set()\n        for r in range(min_r, max_r+1):\n            for c in range(min_c, max_c+1):\n                if r == min_r or r == max_r or c == min_c or c == max_c:\n                    border_cells.add((r,c))\n        if set(cells) == border_cells:\n            output = []\n            for r in range(min_r, max_r+1):\n                row = []\n                for c in range(min_c, max_c+1):\n                    row.append(grid[r][c])\n                output.append(row)\n            return output\n    return grid",
  "ce602527": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    bg = grid[0][0]\n    shapes = {}\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] != bg:\n                v = grid[r][c]\n                if v not in shapes:\n                    shapes[v] = set()\n                shapes[v].add((r,c))\n    shape_info = {}\n    for color, cells in shapes.items():\n        min_r = min(r for r,c in cells)\n        max_r = max(r for r,c in cells)\n        min_c = min(c for r,c in cells)\n        max_c = max(c for r,c in cells)\n        h = max_r - min_r + 1\n        w = max_c - min_c + 1\n        binary = []\n        for r in range(min_r, max_r+1):\n            row = []\n            for c in range(min_c, max_c+1):\n                row.append(1 if (r,c) in cells else 0)\n            binary.append(row)\n        row_types = [1 if all(v == 1 for v in row) else 0 for row in binary]\n        shape_info[color] = {'cells': cells, 'h': h, 'w': w, 'binary': binary, 'row_types': row_types, 'size': len(cells)}\n    large_c = max(shape_info, key=lambda c: shape_info[c]['size'])\n    large = shape_info[large_c]\n    binary = large['binary']\n    h = large['h']\n    row_groups = []\n    i_r = 0\n    while i_r < h:\n        j_r = i_r + 1\n        while j_r < h and binary[j_r] == binary[i_r]:\n            j_r += 1\n        is_full = 1 if all(v == 1 for v in binary[i_r]) else 0\n        row_groups.append(is_full)\n        i_r = j_r\n    smalls = {c: v for c, v in shape_info.items() if c != large_c}\n    for sc, sinfo in smalls.items():\n        rt = sinfo['row_types']\n        if len(rt) >= len(row_groups) and rt[:len(row_groups)] == row_groups:\n            binary_s = sinfo['binary']\n            output = []\n            for row in binary_s:\n                out_row = [sc if v == 1 else bg for v in row]\n                output.append(out_row)\n            return output\n    for sc, sinfo in smalls.items():\n        rt = sinfo['row_types']\n        if len(rt) >= len(row_groups) and rt[-len(row_groups):] == row_groups:\n            binary_s = sinfo['binary']\n            output = []\n            for row in binary_s:\n                out_row = [sc if v == 1 else bg for v in row]\n                output.append(out_row)\n            return output\n    return grid",
  "d07ae81c": "def solve(grid):\n    rows = len(grid)\n    cols = len(grid[0])\n    from collections import Counter\n    vals = Counter()\n    for r in range(rows):\n        for c in range(cols):\n            vals[grid[r][c]] += 1\n    region_colors = [v for v, _ in vals.most_common(2)]\n    markers = []\n    for r in range(rows):\n        for c in range(cols):\n            if grid[r][c] not in region_colors:\n                markers.append((r, c, grid[r][c]))\n    color_map = {}\n    for mr, mc, mcolor in markers:\n        neighbor_counts = Counter()\n        for dr in range(-2, 3):\n            for dc in range(-2, 3):\n                nr, nc = mr+dr, mc+dc\n                if 0 <= nr < rows and 0 <= nc < cols and (nr,nc) != (mr,mc):\n                    v = grid[nr][nc]\n                    if v in region_colors:\n                        neighbor_counts[v] += 1\n        if neighbor_counts:\n            home_region = neighbor_counts.most_common(1)[0][0]\n            color_map[home_region] = mcolor\n    output = [row[:] for row in grid]\n    for mr, mc, mcolor in markers:\n        for dr, dc in [(-1,-1),(-1,1),(1,-1),(1,1)]:\n            r, c = mr + dr, mc + dc\n            while 0 <= r < rows and 0 <= c < cols:\n                orig = grid[r][c]\n                if orig in color_map:\n                    output[r][c] = color_map[orig]\n                r += dr\n                c += dc\n    return output"
}