Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 68
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces.

You are given N bishops, represented as (row, column) tuples on a M by M chessboard. Write a function to count the number of pairs of bishops that attack each other. The ordering of the pair doesn't matter: (1, 2) is considered the same as (2, 1).

For example, given M = 5 and the list of bishops:
```
(0, 0)
(1, 2)
(2, 2)
(4, 0)
```
The board would look like this:
```
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
```
You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3 and 4.

Pull request:
Day68
Resolves #145 .

Patch:
diff --git a/README.md b/README.md
index 42bc6d4..a723b30 100644
--- a/README.md
+++ b/README.md
@@ -77,3 +77,4 @@ problems from
 * [Day 65](https://github.com/vaskoz/dailycodingproblem-go/issues/139)
 * [Day 66](https://github.com/vaskoz/dailycodingproblem-go/issues/141)
 * [Day 67](https://github.com/vaskoz/dailycodingproblem-go/issues/143)
+* [Day 68](https://github.com/vaskoz/dailycodingproblem-go/issues/145)
diff --git a/day68/problem.go b/day68/problem.go
new file mode 100644
index 0000000..0b7bc22
--- /dev/null
+++ b/day68/problem.go
@@ -0,0 +1,26 @@
+package day68
+
+// BoardPosition represents a row and column on a square chessboard.
+type BoardPosition struct {
+	Row, Col int
+}
+
+// CountAttackingBishopPairs returns the count of the number of attacking
+// pairs of Bishops.
+// M is the dimension of the chessboard. M x M
+// Runtime is O(N^2) where N is the number of bishops.
+func CountAttackingBishopPairs(bishops []BoardPosition, M int) int {
+	count := 0
+	for i := 0; i < len(bishops)-1; i++ {
+		first := bishops[i]
+		for j := i + 1; j < len(bishops); j++ {
+			second := bishops[j]
+			rowdiff := second.Row - first.Row
+			coldiff := second.Col - first.Col
+			if rowdiff == coldiff || rowdiff == -coldiff {
+				count++
+			}
+		}
+	}
+	return count
+}


Test patch:
diff --git a/day68/problem_test.go b/day68/problem_test.go
new file mode 100644
index 0000000..e4dcbda
--- /dev/null
+++ b/day68/problem_test.go
@@ -0,0 +1,28 @@
+package day68
+
+import "testing"
+
+var testcases = []struct {
+	bishops  []BoardPosition
+	m        int
+	expected int
+}{
+	{[]BoardPosition{{0, 0}, {1, 2}, {2, 2}, {4, 0}}, 5, 2},
+}
+
+func TestCountAttackingBishopPairs(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := CountAttackingBishopPairs(tc.bishops, tc.m); result != tc.expected {
+			t.Errorf("Expected %v got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkCountAttackingBishopPairs(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			CountAttackingBishopPairs(tc.bishops, tc.m)
+		}
+	}
+}