Skip to content

Latest commit

 

History

History
83 lines (67 loc) · 3.23 KB

File metadata and controls

83 lines (67 loc) · 3.23 KB

ARC Task 1c786137 - Corner Rectangle Frames

Pattern Description

Find a rectangle frame drawn with a single color in the grid and extract the content inside the frame.

Evolution Log

Generation 0 (768 bytes)

Initial readable solution with explicit loops and variable names.

def solve(grid):
    rows = len(grid)
    cols = len(grid[0])
    best = None
    for r in range(rows):
        for c in range(cols):
            color = grid[r][c]
            for h in range(2, rows - r + 1):
                for w in range(2, cols - c + 1):
                    if all(grid[r][c+i] == color for i in range(w)) and \
                       all(grid[r+h-1][c+i] == color for i in range(w)) and \
                       all(grid[r+j][c] == color for j in range(h)) and \
                       all(grid[r+j][c+w-1] == color for j in range(h)):
                        area = (h-2) * (w-2)
                        if best is None or area > best[0]:
                            best = (area, [row[c+1:c+w-1] for row in grid[r+1:r+h-1]])
    return best[1]

Generation 1 (413 bytes)

Shortened variable names, removed whitespace, used single-letter vars.

Generation 2 (389 bytes)

Used walrus operator := and replaced and with * for boolean multiplication.

Generation 3 (367 bytes)

Replaced multiple all() checks with set union: {v}=={...}|{...}|{...}|{...}

Generation 4 (363 bytes)

Changed loop variables to use end positions directly (H, W instead of h, w).

Generation 5 (330 bytes)

Used list slicing g[r][c:W] and concatenation instead of set comprehensions for rows.

Generation 6 (301 bytes)

Converted to single expression with max() and key=lambda x:x[1].

Generation 7 (283 bytes)

Put area first in tuple to use natural tuple ordering: ((H-r-2)*(W-c-2), result).

Generation 8 (281 bytes)

Removed outer brackets - used generator expression instead of list comprehension.

Generation 9 (277 bytes)

Simplified area calculation since only relative ordering matters: (H-r)*(W-c).

Generation 10 (249 bytes) - CHAMPION

Key optimizations:

  • 2>len({...}) instead of len(...)<2 or {v}==...
  • g[r][c:W]+g[H-1][c:W] with unpack for top/bottom rows
  • [z[k]for z in g[r:H]for k in(c,W-1)] for left/right columns using tuple iteration

Champion Solution (249 bytes)

def solve(g):
 R=len(g);C=len(g[0])
 return max(((H-r)*(W-c),[x[c+1:W-1]for x in g[r+1:H-1]])for r in range(R)for c in range(C)for H in range(r+2,R+1)for W in range(c+2,C+1)if 2>len({*g[r][c:W]+g[H-1][c:W],*[z[k]for z in g[r:H]for k in(c,W-1)]}))[1]

Key Golf Techniques Used

  1. Single-letter variables: g, R, C, H, W, r, c, x, z, k
  2. Semicolon chaining: R=len(g);C=len(g[0])
  3. Generator expression in max(): Avoids storing list in memory
  4. Tuple comparison: Put area first for natural ordering
  5. Set unpacking: {*list1,*list2} creates union efficiently
  6. Tuple iteration: for k in(c,W-1) is shorter than for k in[c,W-1]
  7. Comparison flip: 2>len(...) saves space vs len(...)<2
  8. Slice arithmetic: Direct slice extraction avoids temporary variables

Final Results

  • Byte Count: 249
  • Score: 2251 (= 2500 - 249)
  • All tests passing: Yes (3 train + 1 test)