Part 1
In this puzzle, we have a list of rectangles describing patches. Starting from an empty grid, the rectangular patches are added to the grid. Sometimes two or more patches overlap and we should count the number of cells where patches overlap.
We start with a numpy array full of zeroes. Since the maximum size is known in advance, we do not need to parse the patches for maximal dimensions. For each patch, we add 1 to all cells described by the patch to count the number of patches for this cell. The easy syntax for this was why numpy arrays were used. Finally we count the number of cells with more than 1 patches applied to it:
grid = np.zeros((1000, 1000), dtype=np.uint8)
for patch in patches:
claim_num, start_x, start_y, x_size, y_size = patch
grid[start_x:start_x+x_size, start_y:start_y+y_size] += 1
print((grid > 1).sum())
Part 2
In part 2, we should return the ID of the patch which does not overlap with other patches.
After part 1, we have a grid of how often cells overlap. We iterate again over the patches and find a patch where no cells are larger than 1, which means no overlap:
for patch in patches:
claim_num, start_x, start_y, x_size, y_size = patch
if (grid[start_x:start_x + x_size, start_y:start_y + y_size] > 1).sum() == 0:
print(claim_num)
No comments:
Post a Comment