4
You live in a rectangular neighborhood which is completely partitioned into N rectangular plots, i.e. there are no gaps or overlaps. Plots do not necessarily have the same width/height as other plots. All widths/heights are positive integers. Your goal is to output all shared edges between plots.
Inputs
The input may come from any source desired (stdio, file, function parameters, etc.). The input is a list-like sequence of rectangles. Inputs for rectangles may be any form desired, for example (min_x, max_x, min_y, max_y)
, (min_x, min_y, width, height)
, (min_x, min_y, max_x, max_y)
, etc.
The neighborhood is bounded by min_x = 0
and min_y = 0
. You may assume max_x
and max_y
of the neighborhood both fit in your language's native integer type.
You may flatten the input data into a 1D sequence if desired.
The id of a plot is a sequentially incremented integer based on the order of input, where the first plot may have any id desired.
Outputs
The output may be to any desired target (stdio, file, return value, output parameter, etc.).
The output is a list of lists of neighbors for each plot. A neighbor is defined as the id of the neighboring plot and the min/max bounds of the shared corner/edge. Shared corners/edges with the corner/edge of the neighborhood are not considered a neighbor relation and should not be outputted.
When printing out make some distinction between each list, for example using a newline to separate the neighbors of plot i
from the neighbors of plot i+1
.
The order of the neighbors of plot i
is not important.
Example Test Cases
All listed test case inputs are of the format min_x, max_x, min_y, max_y
and plot IDs start with 0. Outputs are formatted as (id, min_x, max_x, min_y, max_y)
.
Input:
0, 1, 0, 1
Output:
no output, empty list, list containing a single empty list, or anything similar such as null
Input:
0, 1, 0, 1
0, 1, 1, 2
Output:
(1, 0, 1, 1, 1)
(0, 0, 1, 1, 1)
Input:
0, 2, 1, 2
0, 2, 0, 1
Output:
(1, 0, 2, 1, 1)
(0, 0, 2, 1, 1)
Input:
0, 1, 0, 1
0, 1, 1, 2
0, 1, 2, 3
Output:
Regions with no neighbor relationships may be omitted.
(1, 0, 1, 1, 1)
(0, 0, 1, 1, 1), (2, 0, 1, 2, 2)
(1, 0, 1, 2, 2)
It is also ok to output the id of the other region with some indication of "no shared boundary".
(1, 0, 1, 1, 1), (2)
(0, 0, 1, 1, 1), (2, 0, 1, 2, 2)
(1, 0, 1, 2, 2), (0, falsy)
Input:
0, 1, 0, 1
0, 1, 1, 2
1, 2, 0, 2
Output:
(1, 0, 1, 1, 1), (2, 1, 1, 0, 1)
(0, 0, 1, 1, 1), (2, 1, 1, 1, 2)
(0, 1, 1, 0, 1), (1, 1, 1, 1, 2)
One possible alternative output (switched order of neighbors for plot 0):
(2, 1, 1, 0, 1), (1, 0, 1, 1, 1)
(0, 0, 1, 1, 1), (2, 1, 1, 1, 2)
(0, 1, 1, 0, 1), (1, 1, 1, 1, 2)
Input:
You may have corner neighbors.
0, 1, 0, 1
0, 1, 1, 2
1, 2, 0, 1
1, 2, 1, 2
Output:
(1, 0, 1, 1, 1), (2, 1, 1, 0, 1), (3, 1, 1, 1, 1)
(0, 0, 1, 1, 1), (2, 1, 1, 1, 1), (3, 1, 1, 1, 2)
(0, 1, 1, 0, 1), (1, 1, 1, 1, 1), (3, 1, 2, 1, 1)
(0, 1, 1, 1, 1), (1, 1, 1, 1, 2), (2, 1, 2, 1, 1)
Scoring
This is code golf; shortest code wins.
edit clarification that corner neighbors should be outputted as well. Also clarified that if two regions don't share any corners/edges the relation may be completely omitted, or a relation with a falsy edge bounds may be outputted. Both have test cases added.
1Wouldn't the output format be simpler if it was the two neighbours and their shared edge together, instead of repeating each shared edge twice? Also are plots considered touching across corners? – Martin Ender – 2016-01-24T10:20:07.420
@MartinBüttner Yes, corners count as neighbors. See edit for new test case. I had considered outputting a single neighbor relation instead of duplicating them, but at the time for the larger problem I was solving it was much more convenient to have them separate so this is what you get as well. – helloworld922 – 2016-01-24T20:32:40.020