Pattern: Mark 1s adjacent to 8s within the 8-bounded region
Given a grid with 0s, 1s, and 8s:
- Find the bounding box of all cells containing 8
- Within that bounding box, find cells with value 1 that are adjacent (8-directionally) to an 8
- Change those cells from 1 to 3
Final byte count: 274 bytes Score: 2226 points
def solve(g):
o=[*map(list,g)];e={(i,j)for i,r in enumerate(g)for j,v in enumerate(r)if v>7}
if e:I,J=zip(*e);[o[r].__setitem__(c,3)for r in range(min(I),max(I)+1)for c in range(min(J),max(J)+1)if g[r][c]==1if e&{(r+a,c+b)for a in(-1,0,1)for b in(-1,0,1)if a|b}]
return oBytes: 967
Readable implementation with full variable names, explicit loops, and clear logic.
Bytes: 409 (-558)
Applied basic golf tricks:
- Single-letter variable names (g, o, e, r, c)
- Removed unnecessary whitespace
- Combined statements with semicolons
- Used list comprehension for adjacency check
Bytes: 363 (-46)
Combined more statements on single lines with semicolons.
Bytes: 326 (-37)
Experimented with encoding row/col as single index (p = r*C + c).
Bytes: 326 (0)
Tried enumerate with tuple unpacking - same size.
Bytes: 321 (-5)
Used set intersection (e&{...}) instead of any() for adjacency check.
Bytes: 297 (-24)
Major optimization: Used zip(*e) to separate row and column indices, then min/max directly on those.
Bytes: 296 (-1)
Changed v==8 to v>7 (works since only 0, 1, 8 appear in the grid).
Bytes: 278 (-18)
Replaced nested for loops with list comprehension using o[r].__setitem__(c,3).
Bytes: 275 (-3)
Multiple variations tested:
- Changed
[r[:] for r in g]to[*map(list,g)](-2 bytes) - Used
if...ifsyntax for chained conditions (-1 byte)
Bytes: 274 (-1)
Removed trailing newline from solution file.
- Single-letter variables: g, o, e, r, c, a, b, I, J
- Set comprehension for 8-positions:
{(i,j) for...if v>7} - *zip(e) for separating coordinates: Unzips set of tuples into (rows, cols)
- Set intersection for adjacency:
e&{neighbors}instead ofany() - setitem in comprehension: Side-effect mutation without explicit loop
- Chained if:
if X if Yinstead ofif X and Y(saves 3 chars) - v>7 instead of v==8: Works when 8 is the only value >7
- [*map(list,g)]: Shorter than
[r[:] for r in g]for deep copy
1. Create output copy of grid
2. Find all positions with value 8 (the marker cells)
3. Get bounding box from min/max of 8-positions
4. For each cell in bounding box:
- If value is 1 AND any neighbor is 8
- Set output cell to 3
5. Return output