Pattern: Fill grid segments (Medium)
The 11x11 grid is divided into a 3x3 grid of 3x3 segments by separator lines (rows 3, 7 and columns 3, 7 filled with value 5). One segment is the "key" segment containing exactly 5 unique values (including 0). The key segment acts as a template: for each non-zero value at position (r,c) in the key, fill segment (r,c) with that value. Segments not mapped in the key are filled with 0.
170 bytes | Score: 2330
solve=lambda g,R=range:[[(5,[y for a in R(9)if 6>len({*(y:=[g[a//3*4+i][a%3*4+j]for i in R(3)for j in R(3)])})][0][i//4*3+j//4])[i%4<3>j%4]for j in R(11)]for i in R(11)]1137 bytes | Score: 1363
- Basic implementation with explicit loops and comments
- Full variable names, verbose structure
526 bytes | Score: 1974
- Removed comments, shortened variable names
- Combined nested loops where possible
400 bytes | Score: 2100
- Used single index with //3 and %3 for segment iteration
- Flattened segment value collection
333 bytes | Score: 2167
- Used set unpacking
{*s}instead ofset(x for x in s if x) - Simplified key segment storage to flat list
235 bytes | Score: 2265
- Combined output generation into single list comprehension
- Used conditional expression for separator handling
208 bytes | Score: 2292
- Reduced indentation (1 space)
- Removed unnecessary variable assignments
204 bytes | Score: 2296
- Used tuples
(0,1,2)instead ofrange(3)for inner loops
202 bytes | Score: 2298
- Changed condition to
5>len({*s}-{0})format
199 bytes | Score: 2301
- Used tuple indexing
[s[...],5][condition]instead of ternary
195 bytes | Score: 2305
- Discovered chained comparison
i%4<3>j%4for condition
170 bytes | Score: 2330
- Key insight: key segment has 5 unique values total (4 non-zero + 0)
- Changed condition to
6>len({*y})(simpler than-{0}) - Converted to single lambda expression
- Used default argument
R=rangefor repeated range calls - Inlined key segment detection into list comprehension
- Lambda conversion:
def solve(g):->solve=lambda g: - Default arg aliasing:
R=rangesaves bytes on repeated calls - Chained comparisons:
i%4<3>j%4is shorter thani%4<3 and j%4<3 - Tuple indexing:
(a,b)[condition]instead ofb if condition else a - Walrus operator:
:=to capture values in comprehensions - Set unpacking:
{*list}instead ofset(list) - Condition simplification: Counting total unique values (6) instead of non-zero unique values (5)