This task involves transforming parallelogram-like shapes into rectangles by aligning their edges vertically. Each colored shape in the grid has a diagonal left edge that needs to be "straightened" while preserving the overall shape.
Transformation Rule:
- Each cell of a colored shape shifts right by 1 position
- Cells are blocked from shifting if they would collide with another cell of the same color
- The rightmost edge of each shape becomes vertical (fixed at max right position)
- Cell count per row is preserved
| Gen | Bytes | Score | Key Change |
|---|---|---|---|
| 0 | 927 | 1573 | Initial working solution |
| 1 | 390 | 2110 | Variable name compression, inline expressions |
| 2 | 301 | 2199 | Single-space indentation, semicolon chaining |
| 3 | 288 | 2212 | Boolean multiplication instead of and |
| 4 | 281 | 2219 | List += instead of append |
| 5 | 279 | 2221 | Removed redundant () in boolean |
| 6 | 278 | 2222 | Walrus operator for assignment |
| 7 | 271 | 2229 | Removed sorted() (list comp already ordered) |
| 8 | 270 | 2230 | Trailing comma tuple syntax b+=n, |
| 9 | 266 | 2234 | Simplified boolean subtraction |
| 10 | 266 | 2234 | No further improvement found |
266 bytes | Score: 2234 | Fitness: 0.8936
def solve(g):
R=[[0]*len(r)for r in g]
for k in{c for r in g for c in r if c}:
M=max(j for r in g for j,c in enumerate(r)if c==k)
for i,r in enumerate(g):
b=[]
for j in[j for j,c in enumerate(r)if c==k][::-1]:R[i][n:=j+(j<M)-(j+1in b)]=k;b+=n,
return R- Set comprehension for colors:
{c for r in g for c in r if c}- finds all non-zero colors - Walrus operator:
:=for inline assignment saves characters - Boolean arithmetic:
(j<M)-(j+1in b)uses True=1, False=0 for compact conditionals - Trailing comma tuple:
b+=n,is shorter thanb+=[n]orb.append(n) - Reversed list comprehension:
[...][::-1]instead ofsorted(...,reverse=1) - Single-space indent: Python allows 1-space indentation for minimal code
- Semicolon chaining: Multiple statements on one line save newlines and indentation
- For each distinct color, find the maximum right position (M)
- Process each row's cells from right to left
- Each cell shifts right by 1 if:
j < M(not at max) ANDj+1not already occupied - Track occupied positions in list
bto prevent collisions