-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_21.py
58 lines (48 loc) · 1.86 KB
/
task_21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import os
from arc_tools import logger
from arc_tools.grid import Color, Grid, SubGrid, detect_objects, place_object_on_new_grid
from arc_tools.plot import plot_grid, plot_grids
import numpy as np
# logger.setLevel(10)
def is_hollow(obj: SubGrid) -> bool:
"""Check if a square has any holes in it."""
for row in range(obj.region.height):
for col in range(obj.region.width):
if obj[row][col] == obj.parent_grid.background_color:
return True
return False
def sort_by_size(grid: Grid):
"""
stack holed squares on left side and remaining on right side.
"""
# Detect all objects
objects = detect_objects(grid)
# Separate hollow and solid squares
hollow_squares = []
solid_squares = []
for obj in objects:
if is_hollow(obj):
hollow_squares.append(obj)
else:
solid_squares.append(obj)
# Create output grid
max_pairs = max(len(hollow_squares), len(solid_squares))
output_height = max_pairs * 4 # Each square is 4x4
output_width = 8 # Two squares side by side
output = Grid([[0] * output_width for _ in range(output_height)])
# Place squares in output grid
for i in range(max_pairs):
# Place hollow square if available
if i < len(hollow_squares):
hollow = hollow_squares[i]
place_object_on_new_grid(hollow, 0, i*4, output)
# Place solid square if available
if i < len(solid_squares):
solid = solid_squares[i]
place_object_on_new_grid(solid, 4, i*4, output)
return output
if __name__ == "__main__":
# Set up environment
os.environ['initial_file'] = os.path.splitext(os.path.basename(__file__))[0]
os.system("python main.py 2ba387bc sort_by_size")