Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 2.54 KB

File metadata and controls

59 lines (47 loc) · 2.54 KB

ARC Task 025d127b - Code Golf Solution

Task Description

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

Evolution History

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

Final Solution

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

Key Golf Tricks

  1. Set comprehension for colors: {c for r in g for c in r if c} - finds all non-zero colors
  2. Walrus operator: := for inline assignment saves characters
  3. Boolean arithmetic: (j<M)-(j+1in b) uses True=1, False=0 for compact conditionals
  4. Trailing comma tuple: b+=n, is shorter than b+=[n] or b.append(n)
  5. Reversed list comprehension: [...][::-1] instead of sorted(...,reverse=1)
  6. Single-space indent: Python allows 1-space indentation for minimal code
  7. Semicolon chaining: Multiple statements on one line save newlines and indentation

Algorithm

  1. For each distinct color, find the maximum right position (M)
  2. Process each row's cells from right to left
  3. Each cell shifts right by 1 if: j < M (not at max) AND j+1 not already occupied
  4. Track occupied positions in list b to prevent collisions