Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 194
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Suppose you are given two lists of n points, one list `p1, p2, ..., pn` on the line y = 0 and the other list `q1, q2, ..., qn` on the line y = 1. Imagine a set of n line segments connecting each point pi to qi. Write an algorithm to determine how many pairs of the line segments intersect.

Pull request description:
Day194
Resolves #400.

Patch:
diff --git a/README.md b/README.md
index 02f9827..d78dd61 100644
--- a/README.md
+++ b/README.md
@@ -203,4 +203,5 @@ problems from
 * [Day 190](https://github.com/vaskoz/dailycodingproblem-go/issues/393)
 * [Day 191](https://github.com/vaskoz/dailycodingproblem-go/issues/395)
 * [Day 192](https://github.com/vaskoz/dailycodingproblem-go/issues/397)
+* [Day 194](https://github.com/vaskoz/dailycodingproblem-go/issues/400)
 * [Day 195](https://github.com/vaskoz/dailycodingproblem-go/issues/401)
diff --git a/day194/problem.go b/day194/problem.go
new file mode 100644
index 0000000..f912152
--- /dev/null
+++ b/day194/problem.go
@@ -0,0 +1,17 @@
+package day194
+
+// CountIntersectionsBrute does a brute force check for intersections.
+// Runs in O(N^2) time.
+func CountIntersectionsBrute(p, q []int) int {
+	intersections := 0
+	for i := range p {
+		for j := i + 1; j < len(q); j++ {
+			diffP := p[j] - p[i]
+			diffQ := q[j] - q[i]
+			if (diffP < 0 && diffQ > 0) || (diffP > 0 && diffQ < 0) {
+				intersections++
+			}
+		}
+	}
+	return intersections
+}


Test patch:
diff --git a/day194/problem_test.go b/day194/problem_test.go
new file mode 100644
index 0000000..82e06fc
--- /dev/null
+++ b/day194/problem_test.go
@@ -0,0 +1,28 @@
+package day194
+
+import "testing"
+
+var testcases = []struct {
+	p, q          []int
+	intersections int
+}{
+	{[]int{3, 1}, []int{1, 3}, 1},
+	{[]int{0, 10, 7, 4, 12, 9, 2}, []int{3, 5, 11, 6, 1, 13, 8}, 11},
+}
+
+func TestCountIntersectionsBrute(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := CountIntersectionsBrute(tc.p, tc.q); result != tc.intersections {
+			t.Errorf("Expected %v, got %v", tc.intersections, result)
+		}
+	}
+}
+
+func BenchmarkCountIntersectionsBrute(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			CountIntersectionsBrute(tc.p, tc.q)
+		}
+	}
+}