-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package day187 | ||
|
||
// Rectangle is a representation of a rectangle. | ||
type Rectangle struct { | ||
TopLeftX, TopLeftY int | ||
Width, Height int | ||
} | ||
|
||
// DoesRectanglePairOverlap returns true if any rectangles | ||
// overlap. | ||
// Runs in O(N^2) time. | ||
func DoesRectanglePairOverlap(rects []Rectangle) bool { | ||
for i, left := range rects { | ||
for j, right := range rects { | ||
if j > i { | ||
if left.TopLeftX > right.TopLeftX { | ||
left, right = right, left | ||
} | ||
if right.TopLeftX < left.TopLeftX+left.Width { | ||
up, dn := left, right | ||
if up.TopLeftY < dn.TopLeftY { | ||
up, dn = dn, up | ||
} | ||
if dn.TopLeftY > up.TopLeftY-up.Height { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package day187 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
rects []Rectangle | ||
expected bool | ||
}{ | ||
{[]Rectangle{{1, 4, 3, 3}, {-1, 3, 2, 1}, {0, 5, 4, 3}}, true}, | ||
{[]Rectangle{{1, 4, 1, 1}, {-1, 3, 1, 1}, {0, 5, 4, 3}}, false}, | ||
{[]Rectangle{{1, 1, 1, 1}, {2, 2, 1, 1}, {3, 3, 1, 1}}, false}, | ||
{[]Rectangle{{1, 1, 2, 1}, {2, 2, 1, 2}, {3, 3, 1, 1}}, true}, | ||
{[]Rectangle{{0, 9, 1, 1}, {1, 1, 1, 1}, {2, 2, 1, 1}, {0, 10, 2, 2}}, true}, | ||
{[]Rectangle{{1, 1, 1, 1}, {2, 2, 1, 1}, {0, 10, 2, 2}}, false}, | ||
} | ||
|
||
func TestDoesRectanglePairOverlap(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := DoesRectanglePairOverlap(tc.rects); result != tc.expected { | ||
t.Errorf("Given %v, expected %v, got %v", tc.rects, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkDoesRectanglePairOverlap(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
DoesRectanglePairOverlap(tc.rects) | ||
} | ||
} | ||
} |